<?php
/**
 * Featured Grid Options
 *
 * @package Stygian
 */

class Stygian_Featured_Grid_Options {
	public function __construct() {
		// Register Featured Grid Options.
		add_action( 'customize_register', array( $this, 'register_options' ), 99 );

		// Add default options.
		add_filter( 'stygian_customizer_defaults', array( $this, 'add_defaults' ) );
	}

	/**
	 * Add options to defaults
	 */
	public function add_defaults( $default_options ) {
		$defaults = array(
			'stygian_featured_grid_visibility'  => 'disabled',
			'stygian_featured_grid_number'      => 3,
			'stygian_featured_grid_button_link' => '#',
		);

		$updated_defaults = wp_parse_args( $defaults, $default_options );

		return $updated_defaults;
	}

	/**
	 * Add layouts section and its controls
	 */
	public function register_options( $wp_customize ) {
		Stygian_Customizer_Utilities::register_option(
			array(
				'settings'          => 'stygian_featured_grid_visibility',
				'type'              => 'select',
				'sanitize_callback' => 'stygian_sanitize_select',
				'label'             => esc_html__( 'Visible On', 'stygian' ),
				'section'           => 'stygian_ss_featured_grid',
				'choices'           => Stygian_Customizer_Utilities::section_visibility(),
			)
		);

		// Add Edit Shortcut Icon.
		$wp_customize->selective_refresh->add_partial( 'stygian_featured_grid_visibility', array(
			'selector' => '#featured-grid-section',
		) );

		Stygian_Customizer_Utilities::register_option(
			array(
				'type'              => 'text',
				'sanitize_callback' => 'stygian_text_sanitization',
				'settings'          => 'stygian_featured_grid_section_top_subtitle',
				'label'             => esc_html__( 'Section Top Sub-title', 'stygian' ),
				'section'           => 'stygian_ss_featured_grid',
				'active_callback'   => array( $this, 'is_featured_grid_visible' ),
			)
		);

		Stygian_Customizer_Utilities::register_option(
			array(
				'settings'          => 'stygian_featured_grid_section_title',
				'type'              => 'text',
				'sanitize_callback' => 'stygian_text_sanitization',
				'label'             => esc_html__( 'Section Title', 'stygian' ),
				'section'           => 'stygian_ss_featured_grid',
				'active_callback'   => array( $this, 'is_featured_grid_visible' ),
			)
		);

		Stygian_Customizer_Utilities::register_option(
			array(
				'settings'          => 'stygian_featured_grid_section_subtitle',
				'type'              => 'text',
				'sanitize_callback' => 'stygian_text_sanitization',
				'label'             => esc_html__( 'Section Subtitle', 'stygian' ),
				'section'           => 'stygian_ss_featured_grid',
				'active_callback'   => array( $this, 'is_featured_grid_visible' ),
			)
		);

		Stygian_Customizer_Utilities::register_option(
			array(
				'settings'          => 'stygian_featured_grid_number',
				'type'              => 'number',
				'label'             => esc_html__( 'Number', 'stygian' ),
				'description'       => esc_html__( 'Please refresh the customizer page once the number is changed.', 'stygian' ),
				'section'           => 'stygian_ss_featured_grid',
				'sanitize_callback' => 'absint',
				'input_attrs'       => array(
					'min'   => 1,
					'max'   => 80,
					'step'  => 1,
					'style' => 'width:100px;',
				),
				'active_callback'   => array( $this, 'is_featured_grid_visible' ),
			)
		);

		$numbers = stygian_gtm( 'stygian_featured_grid_number' );

		for( $i = 0, $j = 1; $i < $numbers; $i++, $j++ ) {
			Stygian_Customizer_Utilities::register_option(
				array(
					'custom_control'    => 'Stygian_Dropdown_Posts_Custom_Control',
					'sanitize_callback' => 'absint',
					'settings'          => 'stygian_featured_grid_page_' . $i,
					'label'             => esc_html__( 'Select Page', 'stygian' ),
					'section'           => 'stygian_ss_featured_grid',
					'active_callback'   => array( $this, 'is_featured_grid_visible' ),
					'input_attrs' => array(
						'post_type'      => 'page',
						'posts_per_page' => -1,
						'orderby'        => 'name',
						'order'          => 'ASC',
					),
				)
			);
		}

		Stygian_Customizer_Utilities::register_option(
			array(
				'type'              => 'text',
				'sanitize_callback' => 'stygian_text_sanitization',
				'settings'          => 'stygian_featured_grid_button_text',
				'label'             => esc_html__( 'Button Text', 'stygian' ),
				'section'           => 'stygian_ss_featured_grid',
				'active_callback'   => array( $this, 'is_featured_grid_visible' ),
			)
		);

		Stygian_Customizer_Utilities::register_option(
			array(
				'type'              => 'url',
				'sanitize_callback' => 'esc_url_raw',
				'settings'          => 'stygian_featured_grid_button_link',
				'label'             => esc_html__( 'Button Link', 'stygian' ),
				'section'           => 'stygian_ss_featured_grid',
				'active_callback'   => array( $this, 'is_featured_grid_visible' ),
			)
		);

		Stygian_Customizer_Utilities::register_option(
			array(
				'custom_control'    => 'Stygian_Toggle_Switch_Custom_control',
				'settings'          => 'stygian_featured_grid_button_target',
				'sanitize_callback' => 'stygian_switch_sanitization',
				'label'             => esc_html__( 'Open link in new tab?', 'stygian' ),
				'section'           => 'stygian_ss_featured_grid',
				'active_callback'   => array( $this, 'is_featured_grid_visible' ),
			)
		);
	}

	/**
	 * Featured Grid visibility active callback.
	 */
	public function is_featured_grid_visible( $control ) {
		return ( stygian_display_section( $control->manager->get_setting( 'stygian_featured_grid_visibility' )->value() ) );
	}
}

/**
 * Initialize class
 */
$stygian_ss_featured_grid = new Stygian_Featured_Grid_Options();
