<?php
/**
 * Admin settings page.
 *
 * @package CookieJar
 */

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

class CookieJar_Settings_Page {

	const OPTION = 'cookiejar_settings';

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'menu' ) );
		add_action( 'admin_init', array( $this, 'register' ) );
	}

	public function menu() {
		add_options_page(
			__( 'CookieJar Settings', 'cookiejar' ),
			__( 'CookieJar', 'cookiejar' ),
			'manage_options',
			'cookiejar',
			array( $this, 'render_page' )
		);
	}

	public function register() {
		register_setting(
			'cookiejar_group',
			self::OPTION,
			array( $this, 'sanitize' )
		);

		add_settings_section(
			'cookiejar_main',
			__( 'Banner Configuration', 'cookiejar' ),
			'__return_false',
			'cookiejar'
		);

		$fields = array(
			'message'         => __( 'Banner Message', 'cookiejar' ),
			'policy_url'      => __( 'Policy URL', 'cookiejar' ),
			'dismiss_text'    => __( 'Accept Button Text', 'cookiejar' ),
			'learn_more_text' => __( 'Learn More Text', 'cookiejar' ),
			'position'        => __( 'Position', 'cookiejar' ),
			'theme'           => __( 'Theme', 'cookiejar' ),
			'expiration_days' => __( 'Consent Expiration (days)', 'cookiejar' ),
		);

		foreach ( $fields as $field => $label ) {
			add_settings_field(
				$field,
				$label,
				array( $this, 'render_field_' . $field ),
				'cookiejar',
				'cookiejar_main'
			);
		}
	}

	public function sanitize( $input ) {
		$input = is_array( $input ) ? $input : array();
		$out   = array();

		$out['message']         = wp_kses_post( $input['message'] ?? '' );
		$out['policy_url']      = esc_url_raw( $input['policy_url'] ?? '' );
		$out['dismiss_text']    = sanitize_text_field( $input['dismiss_text'] ?? '' );
		$out['learn_more_text'] = sanitize_text_field( $input['learn_more_text'] ?? '' );

		$pos             = $input['position'] ?? 'bottom';
		$out['position'] = in_array( $pos, array( 'top', 'bottom' ), true ) ? $pos : 'bottom';

		$theme        = $input['theme'] ?? 'light';
		$out['theme'] = in_array( $theme, array( 'light', 'dark' ), true ) ? $theme : 'light';

		$out['expiration_days'] = max( 1, absint( $input['expiration_days'] ?? 180 ) );

		return $out;
	}

	private function value( $key ) {
		$opt = get_option( self::OPTION, array() );
		return $opt[ $key ] ?? '';
	}

	public function render_field_message() {
		printf(
			'<textarea name="%1$s[message]" rows="4" cols="60" class="large-text code">%2$s</textarea><p class="description">%3$s</p>',
			esc_attr( self::OPTION ),
			esc_textarea( $this->value( 'message' ) ),
			esc_html__( 'Basic HTML allowed.', 'cookiejar' )
		);
	}

	public function render_field_policy_url() {
		printf(
			'<input type="url" name="%1$s[policy_url]" value="%2$s" class="regular-text" placeholder="https://example.com/cookie-policy" />',
			esc_attr( self::OPTION ),
			esc_attr( $this->value( 'policy_url' ) )
		);
	}

	public function render_field_dismiss_text() {
		printf(
			'<input type="text" name="%1$s[dismiss_text]" value="%2$s" class="regular-text" />',
			esc_attr( self::OPTION ),
			esc_attr( $this->value( 'dismiss_text' ) )
		);
	}

	public function render_field_learn_more_text() {
		printf(
			'<input type="text" name="%1$s[learn_more_text]" value="%2$s" class="regular-text" />',
			esc_attr( self::OPTION ),
			esc_attr( $this->value( 'learn_more_text' ) )
		);
	}

	public function render_field_position() {
		$val = $this->value( 'position' );
		?>
		<select name="<?php echo esc_attr( self::OPTION ); ?>[position]">
			<option value="bottom" <?php selected( $val, 'bottom' ); ?>><?php esc_html_e( 'Bottom', 'cookiejar' ); ?></option>
			<option value="top" <?php selected( $val, 'top' ); ?>><?php esc_html_e( 'Top', 'cookiejar' ); ?></option>
		</select>
		<?php
	}

	public function render_field_theme() {
		$val = $this->value( 'theme' );
		?>
		<select name="<?php echo esc_attr( self::OPTION ); ?>[theme]">
			<option value="light" <?php selected( $val, 'light' ); ?>><?php esc_html_e( 'Light', 'cookiejar' ); ?></option>
			<option value="dark" <?php selected( $val, 'dark' ); ?>><?php esc_html_e( 'Dark', 'cookiejar' ); ?></option>
		</select>
		<?php
	}

	public function render_field_expiration_days() {
		printf(
			'<input type="number" min="1" name="%1$s[expiration_days]" value="%2$s" class="small-text" />',
			esc_attr( self::OPTION ),
			esc_attr( $this->value( 'expiration_days' ) )
		);
	}

	public function render_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		?>
		<div class="wrap">
			<h1><?php esc_html_e( 'CookieJar Settings', 'cookiejar' ); ?></h1>
			<form method="post" action="options.php">
				<?php
				settings_fields( 'cookiejar_group' );
				do_settings_sections( 'cookiejar' );
				submit_button();
				?>
			</form>
		</div>
		<?php
	}
}

new CookieJar_Settings_Page();