<?php
/**
 * Class for the Sunflower settings page.
 *
 * @package Sunflower 26
 */

/**
 * The class itself.
 */
class SunflowerSettingsPage {

	/**
	 * Holds the values to be used in the fields callbacks
	 *
	 * @var $options
	 */
	private $options;

	/**
	 * Start up
	 */
	public function __construct() {
		add_action( 'admin_menu', $this->sunflower_add_plugin_page( ... ) );
		add_action( 'admin_init', $this->sunflower_settings_page_init( ... ) );
	}

	/**
	 * Add options page
	 */
	public function sunflower_add_plugin_page(): void {
		add_submenu_page(
			'sunflower_admin',
			__( 'Settings', 'sunflower' ),
			__( 'Settings', 'sunflower' ),
			'edit_pages',
			'sunflower_settings',
			$this->create_sunflower_settings_page( ... )
		);
	}

	/**
	 * Options page callback
	 */
	public function create_sunflower_settings_page(): void {
		// Set class properties from options.
		$this->options = get_option( 'sunflower_options' );

		if ( ! is_array( $this->options ) ) {
			$this->options                         = array();
			$this->options['sunflower_schema_org'] = $this->options['sunflower_schema_org'] ?? 'checked';
		}

		// Set default values.
		$this->options['excerpt_length'] = isset( $this->options['excerpt_length'] ) && '' !== $this->options['excerpt_length']
			? absint( $this->options['excerpt_length'] )
			: 15;

		$this->options['sunflower_categories_archive'] = $this->options['sunflower_categories_archive'] ?? 'main-categories';

		?>
		<div class="wrap sunflower-setting-admin">
			<h1><?php esc_html_e( 'Sunflower Settings', 'sunflower' ); ?></h1>
			<form method="post" action="options.php">
			<?php
				// This prints out all hidden setting fields.
				settings_fields( 'sunflower_option_group' );
				do_settings_sections( 'sunflower-setting-admin' );
				submit_button();
			?>
			</form>

			<script>
			( () => {
				const switcher = document.getElementById( 'sunflower_design_switcher' );
				const panelRow = document.getElementById( 'sunflower_design_switcher_panel' )?.closest( 'tr' );
				const panelInput = document.getElementById( 'sunflower_design_switcher_panel' );
				if ( ! switcher || ! panelRow || ! panelInput ) return;

				const toggle = () => {
					const isChecked = switcher.checked;
					panelRow.style.display = isChecked ? '' : 'none';
					panelInput.disabled = ! isChecked;
				};

				toggle();
				switcher.addEventListener( 'change', toggle );
			} )();
			</script>
		</div>
		<?php
	}

	/**
	 * Register and add settings
	 */
	public function sunflower_settings_page_init(): void {
		register_setting(
			'sunflower_option_group',
			'sunflower_options',
			$this->sanitize( ... )
		);

		add_settings_section(
			'sunflower_theme_variant',
			__( 'Theme variant', 'sunflower' ),
			$this->print_section_info_theme_variant( ... ),
			'sunflower-setting-admin'
		);

		add_settings_field(
			'sunflower_form_style',
			__( 'Shape Style', 'sunflower' ),
			array( $this, 'sunflower_form_style_callback' ),
			'sunflower-setting-admin',
			'sunflower_theme_variant'
		);

		add_settings_field(
			'sunflower_color_scheme',
			__( 'Color Mood', 'sunflower' ),
			array( $this, 'sunflower_color_scheme_callback' ),
			'sunflower-setting-admin',
			'sunflower_theme_variant'
		);

		add_settings_field(
			'sunflower_post_image_format',
			__( 'Post Image Format', 'sunflower' ),
			array( $this, 'sunflower_post_image_format_callback' ),
			'sunflower-setting-admin',
			'sunflower_theme_variant'
		);

		add_settings_field(
			'sunflower_footer_layout',
			__( 'Footer Color Variant', 'sunflower' ),
			array( $this, 'sunflower_footer_layout_callback' ),
			'sunflower-setting-admin',
			'sunflower_theme_variant'
		);

		add_settings_field(
			'sunflower_design_switcher',
			__( 'Frontend Design Switcher', 'sunflower' ),
			$this->sunflower_checkbox_callback( ... ),
			'sunflower-setting-admin',
			'sunflower_theme_variant',
			array(
				'field'       => 'sunflower_design_switcher',
				'label'       => __( 'show frontend design switcher', 'sunflower' ),
				'description' => __( 'The design switcher in the frontend is intended for testing and selecting the desired design. It is not meant to remain permanently active in the frontend for site visitors.', 'sunflower' ),
			)
		);

		add_settings_field(
			'sunflower_design_switcher_panel',
			__( 'Design Switcher Menu', 'sunflower' ),
			$this->sunflower_checkbox_callback( ... ),
			'sunflower-setting-admin',
			'sunflower_theme_variant',
			array(
				'field'       => 'sunflower_design_switcher_panel',
				'label'       => __( 'show design switcher menu in the sidebar', 'sunflower' ),
				'description' => __( 'When disabled, the design can still be changed via buttons with design-switcher CSS classes.', 'sunflower' ),
			)
		);

		add_settings_section(
			'sunflower_layout',
			__( 'Layout', 'sunflower' ),
			$this->print_section_info_layout( ... ),
			'sunflower-setting-admin'
		);

		add_settings_field(
			'excerpt_length',
			__( 'Excerpt length (words)', 'sunflower' ),
			$this->excerpt_length_callback( ... ),
			'sunflower-setting-admin',
			'sunflower_layout'
		);

		add_settings_field(
			'sunflower_show_related_posts',
			__( 'show related posts', 'sunflower' ),
			$this->sunflower_checkbox_callback( ... ),
			'sunflower-setting-admin',
			'sunflower_layout',
			array(
				'field' => 'sunflower_show_related_posts',
				'label' => __( 'show related posts', 'sunflower' ),
			)
		);

		add_settings_field(
			'sunflower_show_author',
			__( 'show author of posts', 'sunflower' ),
			$this->sunflower_checkbox_callback( ... ),
			'sunflower-setting-admin',
			'sunflower_layout',
			array(
				'field' => 'sunflower_show_author',
				'label' => __( 'Show post author on post details and via REST api.', 'sunflower' ),
			)
		);

		add_settings_field(
			'sunflower_hide_prev_next',
			__( 'hide previous and next links', 'sunflower' ),
			$this->sunflower_checkbox_callback( ... ),
			'sunflower-setting-admin',
			'sunflower_layout',
			array(
				'field' => 'sunflower_hide_prev_next',
				'label' => __( 'hide previous and next links', 'sunflower' ),
			)
		);

		add_settings_field(
			'sunflower_contact_form_to',
			__( 'to-field for contact-forms', 'sunflower' ),
			$this->sunflower_contact_form_to( ... ),
			'sunflower-setting-admin',
			'sunflower_layout',
			array( 'sunflower_contact_form_to', __( 'to-field for contact-forms', 'sunflower' ) )
		);

		add_settings_field(
			'sunflower_main_menu_item_is_placeholder',
			__( 'items in menu', 'sunflower' ),
			$this->sunflower_checkbox_callback( ... ),
			'sunflower-setting-admin',
			'sunflower_layout',
			array(
				'field' => 'sunflower_main_menu_item_is_placeholder',
				'label' => __( 'items with href=# in the main menu are placeholders for submenu', 'sunflower' ),
			)
		);

		add_settings_field(
			'sunflower_header_social_media',
			__( 'Show social media icons in header', 'sunflower' ),
			$this->sunflower_header_social_media( ... ),
			'sunflower-setting-admin',
			'sunflower_layout',
			array( 'sunflower_header_social_media', __( 'Show social media icons in header', 'sunflower' ) )
		);

		add_settings_field(
			'sunflower_categories_archive',
			__( 'Show list of categories on category archive', 'sunflower' ),
			$this->sunflower_categories_archive( ... ),
			'sunflower-setting-admin',
			'sunflower_layout'
		);

		add_settings_field(
			'sunflower_schema_org',
			__( 'Enhance SEO', 'sunflower' ),
			$this->sunflower_checkbox_callback( ... ),
			'sunflower-setting-admin',
			'sunflower_layout',
			array(
				'field' => 'sunflower_schema_org',
				'label' => __( 'Set website name in page metadata', 'sunflower' ),
			)
		);

		add_settings_field(
			'sunflower_media_creator',
			__( 'Media creator field', 'sunflower' ),
			$this->sunflower_media_creator( ... ),
			'sunflower-setting-admin',
			'sunflower_layout',
		);
	}

	/**
	 * Sanitize each setting field as needed
	 *
	 * @param array $input Contains all settings fields as array keys.
	 */
	public function sanitize( $input ) {
		$new_input = array();

		// Sanitize everything element of the input array.
		foreach ( $input as $key => $value ) {
			if ( isset( $input[ $key ] ) ) {
				$new_input[ $key ] = sanitize_text_field( $value );
			}
		}

		// Sanitize special values.
		if ( isset( $input['excerpt_length'] ) ) {
			$new_input['excerpt_length'] = absint( $input['excerpt_length'] ) ?? '';
		}

		return $new_input;
	}

	/**
	 * Print the Section text
	 */
	public function print_section_info_theme_variant() {
		printf(
			'<div class="sunflower-section">
				<p class="description">%s</p>
			</div>',
			esc_attr__( 'Select the visual appearance of your theme.', 'sunflower' )
		);
	}

	/**
	 * Print the Section text
	 */
	public function print_section_info_layout() {
		printf(
			'<div class="sunflower-section">
				<p class="description">%s</p>
			</div>',
			esc_attr__( 'Various theme settings', 'sunflower' )
		);
	}

	/**
	 * Checkbox callback
	 *
	 * @param array $args The field arguments.
	 */
	public function sunflower_checkbox_callback( $args ): void {
		$field       = $args['field'];
		$label       = $args['label'];
		$description = $args['description'] ?? '';

		printf(
			'<label>
                    <input type="checkbox" id="%1$s" name="sunflower_options[%1$s]" value="checked" %2$s />
                    %3$s
                </label>',
			esc_attr( $field ),
			isset( $this->options[ $field ] ) ? 'checked' : '',
			esc_attr( $label )
		);

		if ( $description ) {
			printf( '<p class="description">%s</p>', esc_html( $description ) );
		}
	}

	/**
	 * Excerpt length field
	 */
	public function excerpt_length_callback(): void {
		printf(
			'<input type="text" id="excerpt_length" name="sunflower_options[excerpt_length]" value="%s" />',
			isset( $this->options['excerpt_length'] ) ? esc_attr( $this->options['excerpt_length'] ) : ''
		);
	}

	/**
	 * Contact form field "to"
	 */
	public function sunflower_contact_form_to(): void {
		printf(
			'<input type="email" id="sunflower_contact_form_to" name="sunflower_options[sunflower_contact_form_to]" value="%s" />',
			isset( $this->options['sunflower_contact_form_to'] ) ? esc_attr( $this->options['sunflower_contact_form_to'] ) : ''
		);
	}

	/**
	 * Form style layout variant field
	 */
	public function sunflower_form_style_callback(): void {
		echo '<select id="sunflower_form_style" name="sunflower_options[sunflower_form_style]">';

		$options = array(
			array( 'rounded', __( 'Rounded', 'sunflower' ) ),
			array( 'sharp', __( 'Sharp', 'sunflower' ) ),
		);
		foreach ( $options as $option ) {
			$selected = ( isset( $this->options['sunflower_form_style'] ) && $this->options['sunflower_form_style'] === $option[0] ) ? 'selected' : '';
			printf(
				'<option value="%1$s" %2$s>%3$s</option>',
				esc_attr( $option[0] ),
				esc_attr( $selected ),
				esc_attr( $option[1] )
			);
		}

		echo '</select>';
	}

	/**
	 * Color theme layout variant field
	 */
	public function sunflower_color_scheme_callback(): void {
		echo '<select id="sunflower_color_scheme" name="sunflower_options[sunflower_color_scheme]">';

		$options = array(
			array( 'light', __( 'Light', 'sunflower' ) ),
			array( 'green', __( 'Dark', 'sunflower' ) ),
			array( 'auto', __( 'Auto', 'sunflower' ) ),
		);
		foreach ( $options as $option ) {
			$selected = ( isset( $this->options['sunflower_color_scheme'] ) && $this->options['sunflower_color_scheme'] === $option[0] ) ? 'selected' : '';
			printf(
				'<option value="%1$s" %2$s>%3$s</option>',
				esc_attr( $option[0] ),
				esc_attr( $selected ),
				esc_attr( $option[1] )
			);
		}

		echo '</select>';
	}

	/**
	 * Post image format variant field
	 */
	public function sunflower_post_image_format_callback(): void {
		$current = $this->options['sunflower_post_image_format'] ?? 'modern';

		echo '<select id="sunflower_post_image_format" name="sunflower_options[sunflower_post_image_format]">';

		$options = array(
			array( 'modern', __( 'Landscape', 'sunflower' ) ),
			array( 'flexible', __( 'Flexible', 'sunflower' ) ),
		);
		foreach ( $options as $option ) {
			$selected = ( $current === $option[0] ) ? 'selected' : '';
			printf(
				'<option value="%1$s" %2$s>%3$s</option>',
				esc_attr( $option[0] ),
				esc_attr( $selected ),
				esc_html( $option[1] )
			);
		}

		echo '</select>';
		printf(
			'<p class="description">%s</p>',
			esc_html__( 'Nutze Querformate (z.B. 2/1 oder 16/9) als Beitrags- oder Terminbeitrags-Bilder, um einen bestmöglichen Look in den Vorschau-Cards und auf Beitragsseiten zu erzeugen. Wenn du jegliche Proportionen ohne Zuschnitt nutzen möchtest, wähle die Option "Flexibel".', 'sunflower' )
		);
	}

	/**
	 * Footer layout variant field
	 */
	public function sunflower_footer_layout_callback(): void {
		echo '<select id="sunflower_footer_layout" name="sunflower_options[sunflower_footer_layout]">';

		$options = array(
			array( 'sand', __( 'Sand', 'sunflower' ) ),
			array( 'green', __( 'Light Green', 'sunflower' ) ),
		);
		foreach ( $options as $option ) {
			$selected = ( isset( $this->options['sunflower_footer_layout'] ) && $this->options['sunflower_footer_layout'] === $option[0] ) ? 'selected' : '';
			printf(
				'<option value="%1$s" %2$s>%3$s</option>',
				esc_attr( $option[0] ),
				esc_attr( $selected ),
				esc_attr( $option[1] )
			);
		}

		echo '</select>';
	}

	/**
	 * Header layout variant field
	 */
	public function sunflower_categories_archive(): void {
		echo '<select id="sunflower_categories_archive" name="sunflower_options[sunflower_categories_archive]">';

		$options = array(
			array( 'no', __( 'do not show', 'sunflower' ) ),
			array( 'main-categories', __( 'main categories', 'sunflower' ) ),
			array( 'only-subcategories', __( 'only sub-categories', 'sunflower' ) ),
		);
		foreach ( $options as $option ) {
			$selected = ( isset( $this->options['sunflower_categories_archive'] ) && $this->options['sunflower_categories_archive'] === $option[0] ) ? 'selected' : '';
			printf(
				'<option value="%1$s" %2$s>%3$s</option>',
				esc_attr( $option[0] ),
				esc_attr( $selected ),
				esc_attr( $option[1] )
			);
		}

		echo '</select>';
	}

	/**
	 * Usage of the media creator field variantes
	 */
	public function sunflower_media_creator(): void {
		echo '<select id="sunflower_media_creator" name="sunflower_options[sunflower_media_creator]">';

		$options = array(
			array( 'optional', __( 'optional', 'sunflower' ) ),
			array( 'required', __( 'required', 'sunflower' ) ),
			array( 'strict', __( 'strict (The image is blurred if no creator field is filled out.)', 'sunflower' ) ),
			array( 'disabled', __( 'disabled', 'sunflower' ) ),
		);
		foreach ( $options as $option ) {
			$selected = ( isset( $this->options['sunflower_media_creator'] ) && $this->options['sunflower_media_creator'] === $option[0] ) ? 'selected' : '';
			printf(
				'<option value="%1$s" %2$s>%3$s</option>',
				esc_attr( $option[0] ),
				esc_attr( $selected ),
				esc_attr( $option[1] )
			);
		}

		echo '</select>';
	}

	/**
	 * Show social media icons in header, too.
	 *
	 * @param Array $args The forms argument.
	 */
	public function sunflower_header_social_media( $args ): void {
		$field = $args[0];
		$label = $args[1];

		printf(
			'<label>
                    <input type="checkbox" id="%1$s" name="sunflower_options[%1$s]" value="checked" %2$s />
                    %3$s
                </label>',
			esc_attr( $field ),
			isset( $this->options[ $field ] ) ? 'checked' : '',
			esc_attr( $label )
		);
	}
}

if ( is_admin() ) {
	$sunflower_settings_page = new SunflowerSettingsPage();
}
