<?php
/**
 * Optikit Settings Page Class
 *
 * @package Optikit
 */

namespace Optikit;

/**
 * Class for registering settings page under Settings.
 */
final class Optikit_Options extends \Optikit\DediData\Singleton {

	/**
	 * Page Title
	 *
	 * @var string $page_title
	 */
	protected $page_title;

	/**
	 * Menu Title
	 *
	 * @var string $menu_title
	 */
	protected $menu_title;

	/**
	 * Plugin Slug
	 *
	 * @var string $plugin_slug
	 */
	protected $plugin_slug;

	/**
	 * Plugin Hook
	 *
	 * @var string $plugin_hook
	 */
	protected $plugin_hook;

	/**
	 * Constructor
	 *
	 * @param mixed $plugin_slug Plugin Slug String.
	 */
	public function __construct( $plugin_slug = null ) {
		$this->plugin_slug = $plugin_slug;
		$this->page_title  = esc_html__( 'Optikit', 'optikit' );
		$this->menu_title  = esc_html__( 'Optikit', 'optikit' );
		add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
		// Here you can check if plugin is configured (e.g. check if some option is set). If not, add new hook.
		if ( '' === get_option( $this->plugin_slug ) ) {
			add_action( 'admin_notices', array( $this, 'add_admin_notices' ) );
		}
		add_action( 'admin_init',  array( $this, 'register_settings' ) );
	}

	/**
	 * Sanitize each setting field as needed
	 *
	 * @param array<bool> $input Contains all settings fields as array keys.
	 * @return array<bool>
	 */
	public function process_inputs( $input ) {
		// sanitize functions:
		// sanitize_email(), sanitize_file_name(), sanitize_html_class(), sanitize_key(), sanitize_meta(), sanitize_mime_type(),
		// sanitize_option(), sanitize_sql_orderby(), sanitize_text_field(), sanitize_textarea_field(), sanitize_title(),
		// sanitize_title_for_query(), sanitize_title_with_dashes(), sanitize_user()
		$options = array();

		$options['global']       = isset( $input['global'] ) && true === boolval( $input['global'] );
		$options['font-awesome'] = isset( $input['font-awesome'] ) && true === boolval( $input['font-awesome'] );
		$options['bootstrap']    = isset( $input['bootstrap'] ) && true === boolval( $input['bootstrap'] );

		// add error/update messages
		// check if the user have submitted the settings

		/*
		add_settings_error(
			$this->plugin_slug . '_messages',
			// Slug title of setting
			'wporg_message',
			// Slug-name , Used as part of 'id' attribute in HTML output.
			esc_html__('The entered information is not correct.', 'optikit'),
			// message text, will be shown inside styled <div> and <p> tags
			'error'
			// Message type, controls HTML class. Accepts 'error' or 'updated'.
		);
		*/

		return $options;
	}

	/**
	 * Registers a new settings page under Settings.
	 *
	 * @return void
	 */
	public function add_admin_menu() {
		$this->plugin_hook = add_options_page(
			$this->page_title,
			$this->menu_title,
			// Capability
			'manage_options',
			$this->plugin_slug,
			// Output the content for this page
			array( $this, 'settings_page_content' )
		);

		if ( ! $this->plugin_hook ) {
			return;
		}
		add_action( 'load-' . $this->plugin_hook, array( $this, 'on_plugin_page_load' ) );
	}

	/**
	 * Adds a help tab and a help sidebar to the current screen in the WordPress admin area.
	 *
	 * @return void
	 */
	public function on_plugin_page_load() {
		remove_action( 'admin_notices', array( $this, 'add_admin_notices' ) );
		// We are in the correct screen because we are taking advantage of the load-* action (below)
		$help_content = '<p>' . esc_html__( 'Use this page to configure Optikit plugin', 'optikit' ) . '</p>';
		$screen       = get_current_screen();
		// $screen->remove_help_tabs();
		$screen->add_help_tab(
			array(
				'id'      => $this->plugin_slug . '-default',
				'title'   => esc_html__( 'Help', 'optikit' ),
				'content' => $help_content,
			)
		);
		// add more help tabs as needed with unique id's

		// Help sidebars are optional
		$screen->set_help_sidebar(
			'<p><strong>' . esc_html__( 'For more information:', 'optikit' ) . '</strong></p>'
			. '<p><a href="' . esc_url( esc_html__( 'https://dedidata.com', 'optikit' ) ) . '" target="_blank">' . esc_html__( 'Visit Our Website!', 'optikit' ) . '</a></p>'
		);
	}

	/**
	 * Display an admin notice on the WordPress admin dashboard if the plugin is not configured.
	 *
	 * @return void
	 */
	public function add_admin_notices() {
		?>
		<div id="notice" class="notice notice-warning is-dismissible">
		<!--div id="notice" class="update-nag updated"-->
			<p>
				<?php esc_html_e( 'Optikit plugin is not configured', 'optikit' ); ?>
				<a href="<?php menu_page_url( $this->plugin_slug, true ); ?>"><?php esc_html_e( 'Please configure it now!', 'optikit' ); ?></a>
			</p>
		</div>
		<?php
	}

	/**
	 * Render a specific settings section (like do_settings_sections but for one section)
	 *
	 * @param string $section_id Section ID.
	 * @return void
	 */
	public function render_section_fields( string $section_id ) {
		global $wp_settings_sections, $wp_settings_fields;

		if ( ! isset( $wp_settings_sections[ $this->plugin_slug ][ $section_id ] ) ) {
			return;
		}

		$section = $wp_settings_sections[ $this->plugin_slug ][ $section_id ];

		// before_section
		if ( isset( $section['before_section'] ) ) {
			if ( isset( $section['section_class'] ) ) {
				echo wp_kses_post( sprintf( $section['before_section'], esc_attr( $section['section_class'] ) ) );
			} else {
				echo wp_kses_post( $section['before_section'] );
			}
		}

		// title
		if ( '' !== $section['title'] ) {
			echo '<h2>' . esc_html( $section['title'] ) . '</h2>';
		}

		// callback
		if ( is_callable( $section['callback'] ) ) {
			call_user_func( $section['callback'], $section );
		}

		// fields
		if ( isset( $wp_settings_fields[ $this->plugin_slug ][ $section_id ] ) ) {
			echo '<table class="form-table" role="presentation">';
			do_settings_fields( $this->plugin_slug, $section_id );
			echo '</table>';
		}

		// after_section
		if ( isset( $section['after_section'] ) ) {
			echo wp_kses_post( $section['after_section'] );
		}
	}

	/**
	 * Register the settings for the plugin.
	 *
	 * @return void
	 */
	public function register_settings() {
		// whitelist options
		register_setting(
			// option_group
			$this->plugin_slug,
			// option_name, for name property of tags
			$this->plugin_slug,
			// sanitize_callback
			array( $this, 'process_inputs' )
		);
		add_settings_section(
			// Slug-name to identify the section. Used in the 'id' attribute of tags
			'global',
			// title heading for the section
			'',
			// Function that echos out any content at the top of the section (between heading and fields).
			'',
			// The slug-name of the settings page on which to show the section. Built-in pages include 'general', 'reading', 'writing', 'discussion', 'media', etc. ( Plugin slug, created by add_options_page() )
			$this->plugin_slug,
			// Arguments used to create the settings section.
			array(
				// HTML content to prepend to the section’s HTML output. Receives the section’s class name as %s.
				// $before_section,
				// HTML content to append to the section’s HTML output.
				// $after_section,
				// The class name to use for the section.
				// $section_class,
			)
		);
			add_settings_field(
				// id attribute of tag
				'global',
				// Title as label for field
				esc_html__( 'Enable Optikit Globally', 'optikit' ),
				// Callback function to echo input tag
				function () {
					$optikit      = get_option( $this->plugin_slug );
					$check_global = $optikit['global'] ?? true;
					?>
					<input type="checkbox" name="optikit[global]" id="global" value="true" <?php checked( true, $check_global ); ?> />
					<?php
				},
				// plugin slug, created by add_options_page()
				$this->plugin_slug,
				// slug-name of the section
				'global',
				array(
					// label for => tag id
					'label_for' => 'global',
					// class for <tr>
					'class'     => 'global',
				)
			);
		add_settings_section(
			// Slug-name to identify the section. Used in the 'id' attribute of tags
			'load-font-awesome-libraries',
			// title heading for the section
			esc_html__( 'Settings for loading font awesome libraries', 'optikit' ),
			// Function that echos out any content at the top of the section (between heading and fields).
			static function ( $args ) {
				?>
				<p id="<?php echo esc_attr( $args['id'] ); ?>"><?php echo esc_html__( 'Please specify which libraries to load in your web site front-end', 'optikit' ); ?></p>
				<?php
			},
			// The slug-name of the settings page on which to show the section. Built-in pages include 'general', 'reading', 'writing', 'discussion', 'media', etc. ( Plugin slug, created by add_options_page() )
			$this->plugin_slug,
			// Arguments used to create the settings section.
			array(
				// HTML content to prepend to the section’s HTML output. Receives the section’s class name as %s.
				// $before_section,
				// HTML content to append to the section’s HTML output.
				// $after_section,
				// The class name to use for the section.
				// $section_class,
			)
		);
				add_settings_field(
					// id attribute of tag
					'font-awesome',
					// Title as label for field
					esc_html__( 'Load Font Awesome library in your site frontend', 'optikit' ),
					// Callback function to echo input tag
					function () {
						$optikit            = get_option( $this->plugin_slug );
						$check_font_awesome = $optikit['font-awesome'] ?? false;
						?>
						<input type="checkbox" name="optikit[font-awesome]" id="font-awesome" value="true" <?php checked( true, $check_font_awesome ); ?> />
						<?php
					},
					// plugin slug, created by add_options_page()
					$this->plugin_slug,
					// slug-name of the section
					'load-font-awesome-libraries',
					array(
						// label for => tag id
						'label_for' => 'font-awesome',
						// class for <tr>
						'class'     => 'font-awesome',
					)
				);
		add_settings_section(
			// Slug-name to identify the section. Used in the 'id' attribute of tags
			'load-bootstrap-libraries',
			// title heading for the section
			esc_html__( 'Settings for loading bootstrap libraries', 'optikit' ),
			// Function that echos out any content at the top of the section (between heading and fields).
			static function ( $args ) {
				?>
				<p id="<?php echo esc_attr( $args['id'] ); ?>"><?php echo esc_html__( 'Please specify which libraries to load in your web site front-end', 'optikit' ); ?></p>
				<?php
			},
			// The slug-name of the settings page on which to show the section. Built-in pages include 'general', 'reading', 'writing', 'discussion', 'media', etc. ( Plugin slug, created by add_options_page() )
			$this->plugin_slug,
			// Arguments used to create the settings section.
			array(
				// HTML content to prepend to the section’s HTML output. Receives the section’s class name as %s.
				// $before_section,
				// HTML content to append to the section’s HTML output.
				// $after_section,
				// The class name to use for the section.
				// $section_class,
			)
		);
				add_settings_field(
					// id attribute of tag
					'bootstrap',
					// Title as label for field
					esc_html__( 'Load Bootstrap in your site frontend', 'optikit' ),
					// Callback function to echo input tag
					function () {
						$optikit         = get_option( $this->plugin_slug );
						$check_bootstrap = $optikit['bootstrap'] ?? false;
						?>
						<input type="checkbox" name="optikit[bootstrap]" id="bootstrap" value="true" <?php checked( true, $check_bootstrap ); ?> />
						<?php
					},
					// plugin slug, created by add_options_page()
					$this->plugin_slug,
					// slug-name of the section
					'load-bootstrap-libraries',
					array(
						// label for => tag id
						'label_for' => 'bootstrap',
						// class for <tr>
						'class'     => 'bootstrap',
					)
				);
	}

	/**
	 * Settings page display callback.
	 *
	 * @return void
	 */
	public function settings_page_content() {
		// check user capabilities
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		// var_dump( wp_load_alloptions() ); // print all options

		// show error/update messages
		// Uncomment if this screen isn't added with add_options_page() otherwise no need, WordPress automatically call this
		// settings_errors( $this->plugin_slug . '_messages' );
		?>
		<div class="wrap">
			<h1 class="wp-heading-inline"><?php echo esc_html( $this->page_title ); ?></h1>
			<form method="post" action="options.php">
				<?php
				// This prints out all hidden setting fields
				settings_fields( $this->plugin_slug );
				submit_button();
				$this->render_section_fields( 'global' );
				?>
				<!-- span class="badge green">New!</span-->
				<div id="tabs-container">
					<h2 class="nav-tab-wrapper">
						<a class="nav-tab nav-tab-active" href="#General"><?php esc_html_e( 'General', 'optikit' ); ?></a>
						<a class="nav-tab" href="#Assets"><?php esc_html_e( 'Assets', 'optikit' ); ?></a>
					</h2>
					<div id="tab">
						<div id="General" class="tab-content" style="display:block;">

							<div class="dedidata-accordion">

								<h3 class="dedidata-accordion-heading">
									<button aria-expanded="false" class="dedidata-accordion-trigger" type="button">
										<span class="title"><?php echo esc_html__( 'Bootstrap', 'optikit' ); ?></span>
										<span class="icon"></span>
									</button>
								</h3>
								<div class="dedidata-accordion-panel" hidden="hidden">

									<?php $this->render_section_fields( 'load-bootstrap-libraries' ); ?>

								</div>
								<!--
								<h3 class="dedidata-accordion-heading">
									<button aria-expanded="false" class="dedidata-accordion-trigger" type="button">
										<span class="title"><?php echo esc_html__( 'Assets', 'optikit' ); ?></span>
										<span class="icon"></span>
									</button>
								</h3>
								<div class="dedidata-accordion-panel" hidden="hidden">

									<?php // $this->render_section_fields( 'load-bootstrap-libraries' ); ?>

								</div>
								-->
							</div>

						</div>
						<div id="Assets" class="tab-content">
							<?php $this->render_section_fields( 'load-font-awesome-libraries' ); ?>
						</div>
						<?php submit_button(); ?>
					</div>
				</div>
			</form>
		</div>
		<?php
	}
}
