<?php
/**
 * Back to Top Tweaks Page handler: rendering and processing.
 *
 * @package FoodOrd
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Handles the Back to Top Tweaks admin page (render + process).
 */
class Foodord_Back_To_Top {
	/**
	 * Hook processors for this page.
	 */
	public function init_hooks() {
		add_action( 'admin_init', array( $this, 'handle_form_submission' ) );
	}

	/**
	 * Process form submissions for Tweaks page.
	 */
	public function handle_form_submission() {
		if ( ! isset( $_POST['foodord_tweaks_nonce'] ) ) {
			return;
		}

		if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['foodord_tweaks_nonce'] ) ), 'foodord_tweaks_save' ) ) {
			return;
		}

		if ( ! isset( $_POST['foodord_tweaks_submit'] ) ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$fields = array(
			'foodord_back_to_top_bg',
			'foodord_back_to_top_hover',
			'foodord_back_to_top_icon',
		);

		$updated = false;

		$defaults = array(
			'foodord_back_to_top_bg'    => '#ffffff',
			'foodord_back_to_top_hover' => '#f5f5f5',
			'foodord_back_to_top_icon'  => '#ea004b',
		);

		foreach ( $fields as $field ) {
			$raw = isset( $_POST[ $field ] ) ? sanitize_text_field( wp_unslash( $_POST[ $field ] ) ) : '';

			// If empty, use default value.
			if ( empty( $raw ) ) {
				$val = isset( $defaults[ $field ] ) ? $defaults[ $field ] : '';
			} else {
				$val = sanitize_hex_color( $raw );
				if ( null === $val ) {
					$val = '';
				}
			}

			$old_val = get_option( $field, '' );
			if ( $old_val !== $val ) {
				update_option( $field, $val );
				$updated = true;
			}
		}

		// Note: Success message is now handled by the main Tweaks page orchestrator.
	}

	/**
	 * Render page content.
	 *
	 * @param bool $show_section Whether to render Back to Top section.
	 */
	public function render( bool $show_section = true ) {
		$bg_color    = get_option( 'foodord_back_to_top_bg', '#ffffff' );
		$hover_color = get_option( 'foodord_back_to_top_hover', '#f5f5f5' );
		$icon_color  = get_option( 'foodord_back_to_top_icon', '#ea004b' );
		?>
		<div class="wrap foodord-tweaks-wrap">
			<h1 class="wr-tweaks-title"><?php echo esc_html__( 'Tweaks Settings', 'foodord' ); ?></h1>
			<?php
			if ( function_exists( 'foodord_admin_render_horizontal_menu' ) ) {
				foodord_admin_render_horizontal_menu();
			}
			?>

			<?php
			// Check for Tweaks general success message.
			if ( get_transient( 'foodord_tweaks_success' ) ) {
				delete_transient( 'foodord_tweaks_success' );
				?>
				<div class="notice notice-success is-dismissible">
					<p class="notice-message"><?php esc_html_e( 'Settings saved successfully.', 'foodord' ); ?></p>
				</div>
				<?php
			}
			?>

			<form method="post" action="" class="foodord-tweaks-form" novalidate>
				<?php wp_nonce_field( 'foodord_tweaks_save', 'foodord_tweaks_nonce' ); ?>
				<?php if ( $show_section ) : ?>
					<div class="wrc-back-to-top-container">
						<div class="wrc-back-to-top-header">
							<h2 class="wrc-section-title"><?php echo esc_html__( 'Back to Top Button', 'foodord' ); ?></h2>
							<p class="wrc-card-sub"><?php echo esc_html__( 'Customize the Back to Top button colors.', 'foodord' ); ?></p>
						</div>

						<div class="wrc-back-to-top-content">
							<div class="wr-pages-bg-wrapper wr-pages-bg-wrapper-three-col">
								<div class="wr-pages-bg-color-group">
									<label for="foodord_back_to_top_bg" class="wr-pages-bg-label">
										<?php echo esc_html__( 'Background Color:', 'foodord' ); ?>
									</label>
									<input type="text" id="foodord_back_to_top_bg" name="foodord_back_to_top_bg" value="<?php echo esc_attr( $bg_color ); ?>" class="wr-color-picker" />
								</div>

								<div class="wr-pages-bg-color-group">
									<label for="foodord_back_to_top_hover" class="wr-pages-bg-label">
										<?php echo esc_html__( 'Hover Background:', 'foodord' ); ?>
									</label>
									<input type="text" id="foodord_back_to_top_hover" name="foodord_back_to_top_hover" value="<?php echo esc_attr( $hover_color ); ?>" class="wr-color-picker" />
								</div>

								<div class="wr-pages-bg-color-group">
									<label for="foodord_back_to_top_icon" class="wr-pages-bg-label">
										<?php echo esc_html__( 'Icon Color:', 'foodord' ); ?>
									</label>
									<input type="text" id="foodord_back_to_top_icon" name="foodord_back_to_top_icon" value="<?php echo esc_attr( $icon_color ); ?>" class="wr-color-picker" />
								</div>
							</div>
						</div>
					</div>
				<?php endif; ?>

				<p class="submit">
					<button type="submit" name="foodord_tweaks_submit" class="button button-primary"><?php echo esc_html__( 'Save Changes', 'foodord' ); ?></button>
				</p>
			</form>
		</div>
		<?php
	}
}
