<?php

class Ignition_Widgets_Settings extends \Elementor\Settings {

	public function __construct() {
		add_action( 'admin_menu', [ $this, 'add_admin_menu' ], 502 );
		add_action( 'admin_init', [ $this, 'settings_init' ] );
	}

	public function add_admin_menu() {

		add_submenu_page( \Elementor\Settings::PAGE_ID, __( 'Ignition Widgets', 'ignition-elementor-widgets' ), __( 'Ignition Widgets', 'ignition-elementor-widgets' ), 'manage_options', 'ignition_widgets', [ $this, 'options_page' ] );
	}

	function settings_init() {

		register_setting(
			'ignition_widgets_settings_group',
			'ignition_widgets_settings',
			array(
				'sanitize_callback' => array( $this, 'sanitize' ),
				'default'           => array(),
			)
		);

		add_settings_section(
			'ignition_widgets_settings_section',
			esc_html__( 'Theme Settings', 'ignition-elementor-widgets' ),
			null,
			'ignition_widgets_settings'
		);

		add_settings_field(
			'ignition_widgets_custom_setting',
			esc_html__( 'Theme Custom Setting', 'ignition-elementor-widgets' ),
			array( $this, 'custom_settings_html' ),
			'ignition_widgets_settings',
			'ignition_widgets_settings_section'
		);

	}

	/**
	 * Returns an array of all available Ignition Elementor elements.
	 *
	 * @return array
	 *
	 */
	private static function available_elements() {

		$available_elements = \CSSIgniter\Ignition\Elementor\Ignition_Elementor_Widgets()->ignition_elementor_get_available_elements();

		return $available_elements;
	}

	/**
	 * Get the settings.
	 *
	 * @param string $option Option name.
	 *
	 * @return bool|mixed
	 */
	public static function get_setting( $option = '' ) {

		$defaults = self::get_default_settings();

		$settings = wp_parse_args( get_option( 'ignition_widgets_settings', $defaults ), $defaults );

		return isset( $settings[ $option ] ) ? $settings[ $option ] : false;

	}

	/**
	 * Generate default settings.
	 */
	public static function get_default_settings() {

		$elements = self::available_elements();

		$default = array();
		$default_on = array(
			'post_types',
			'food_menu',
			'taxonomy_terms',
			'icon_box',
		);

		foreach ( $elements as $key => $val ) {
			if ( in_array( $key, $default_on, true ) ) {
				$default[ $key ] = 1;
			} else {
				$default[ $key ] = 0;
			}
		}

		return apply_filters( 'ignition_widgets_settings_defaults', $default );

	}

	/**
	 * Sanitize data from custom settings form.
	 */
	public function sanitize( $input ) {
		$sanitized_input = array();

		$elements = self::available_elements();

		foreach ( $elements as $key => $value ) {
			if ( isset( $input[$key] ) && $input[$key] ) {
				$sanitized_input[$key] = 1;
			} else {
				$sanitized_input[$key] = 0;
			}
		}

		return $sanitized_input;
	}

	/**
	 * HTML for the theme's custom settings.
	 */
	public function custom_settings_html() {
		$panels = self::available_elements();

		foreach ( $panels as $key => $value ) {

			$setting = self::get_setting( $key );
			?>
			<div class="ignition-elementor-setting">
				<h2>
				<?php
				echo wp_kses(
					$value['title'],
					array()
				);
				?>
				</h2>
				<label for="<?php echo esc_attr( $key ); ?>">

				<?php if ( isset( $value['plugin'] ) && ! class_exists( $value['plugin'] ) ) : ?>
					<p>
					<?php
					echo wp_kses(
						$value['warning'],
						array(
							'a' => array(
								'href'   => true,
								'target' => true,
							),
						)
					);
					?>
					</p>
				<?php else : ?>

					<input <?php checked( $setting, 1 ); ?> id="<?php echo esc_attr( $key ); ?>" name="ignition_widgets_settings[<?php echo esc_attr( $key ); ?>]" value="1" type="checkbox" />
					<?php
					echo wp_kses(
						$value['label'],
						array(
							'a' => array(
								'href'   => true,
								'target' => true,
							),
						)
					);
					?>

				<?php endif; ?>
				</label>
			</div>
			<?php
		}
	}

	/**
	 * Prints out one specified field from settings section.
	 *
	 * Based on:
	 *
	 * @see do_settings_sections
	 *
	 * as seen in https://wordpress.stackexchange.com/a/316096
	 *
	 * @global array $wp_settings_sections Storage array of all settings sections added to admin pages.
	 * @global array $wp_settings_fields   Storage array of settings fields and info about their pages/sections.
	 *
	 * @param string $page                 The slug name of the page whose settings sections you want to output.
	 * @param string $field_id             Field ID for output.
	 */
	public static function do_settings_section_field( $page, $field_id ) {
		global $wp_settings_sections, $wp_settings_fields;

		if ( ! isset( $wp_settings_sections[ $page ] ) ) {
			return;
		}

		foreach ( (array) $wp_settings_sections[ $page ] as $section ) {

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

			if ( ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) {
				continue;
			}

			foreach ( (array) $wp_settings_fields[ $page ][ $section['id'] ] as $field ) {
				if ( $field['id'] !== $field_id ) {
					continue;
				}

				call_user_func( $field['callback'], $field['args'] );
			}
		}
	}

	public function options_page() {

		?>
		<div class="ignition-elementor-container">

			<div class="ignition-elementor-content">
				<h2 class="page-title"><?php esc_html_e( 'Ignition Widgets', 'ignition-elementor-widgets' ); ?></h2>
				<?php settings_errors(); ?>
				<p class="page-subtitle"><?php esc_html_e( 'Use the checkboxes below to enable or disable the custom elements.', 'ignition-elementor-widgets' ); ?></p>

				<form action='options.php' method='post' class="ignition-elementor-form">
					<div class="ignition-elementor-settings">
						<?php
							settings_fields( 'ignition_widgets_settings_group' );
							$this->do_settings_section_field( 'ignition_widgets_settings', 'ignition_widgets_custom_setting' );
						?>
					</div>

					<?php submit_button(); ?>
				</form>
			</div><!-- /ignition-elementor-content -->
		</div><!-- /ignition-elementor-container -->
		<?php

	}

}

new Ignition_Widgets_Settings();
