<?php
/**
 * Featured Slider Options
 *
 * @package Signify
 */

/**
 * Add hero content options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_slider_options( $wp_customize ) {
	$wp_customize->add_section( 'signify_featured_slider', array(
			'panel' => 'signify_theme_options',
			'title' => esc_html__( 'Featured Slider', 'signify-pro' ),
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_option',
			'default'           => 'disabled',
			'sanitize_callback' => 'signify_sanitize_select',
			'choices'           => signify_section_visibility_options(),
			'label'             => esc_html__( 'Enable on', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_transition_in',
			'default'           => 'default',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_slider_active',
			'choices'           => signify_transition_effects(),
			'label'             => esc_html__( 'Transition In', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_transition_out',
			'default'           => 'default',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_slider_active',
			'choices'           => signify_transition_effects(),
			'label'             => esc_html__( 'Transition Out', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_nav',
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_slider_active',
			'label'             => esc_html__( 'Display nav arrows', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_dots',
			'default'           => 1,
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_slider_active',
			'label'             => esc_html__( 'Display nav dots', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_autoplay',
			'default'           => 1,
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_slider_active',
			'label'             => esc_html__( 'Autoplay', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_loop',
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_slider_active',
			'label'             => esc_html__( 'Loop (Last to first)', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_transition_timeout',
			'default'           => 4,
			'sanitize_callback' => 'signify_sanitize_number_range',
			'active_callback'   => 'signify_is_slider_active',
			'input_attrs'       => array(
				'style'       => 'width: 100px;',
				'min'         => 0,
			),
			'label'             => esc_html__( 'Transition timeout', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'              => 'number',
		)
	);

	$type = signify_section_type_options();

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_type',
			'default'           => 'category',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_slider_active',
			'choices'           => $type,
			'label'             => esc_html__( 'Select Slider Type', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_number',
			'default'           => 2,
			'sanitize_callback' => 'signify_sanitize_number_range',

			'active_callback'   => 'signify_is_slider_active',
			'description'       => esc_html__( 'Save and refresh the page if No. of Slides is changed (Max no of slides is 20)', 'signify-pro' ),
			'input_attrs'       => array(
				'style' => 'width: 100px;',
				'min'   => 0,
				'max'   => 20,
				'step'  => 1,
			),
			'label'             => esc_html__( 'No of Slides', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'              => 'number',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_content_show',
			'default'           => 'hide-content',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_image_slider_inactive',
			'choices'           => signify_content_show(),
			'label'             => esc_html__( 'Display Content', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_meta_show',
			'default'           => 'show-meta',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_image_slider_inactive',
			'choices'           => signify_meta_show(),
			'label'             => esc_html__( 'Display Meta', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_exclude_slider_post',
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_post_slider_active',
			'label'             => esc_html__( 'Exclude Slider post from Homepage posts', 'signify-pro' ),
			'section'           => 'signify_featured_slider',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_slider_select_category',
			'sanitize_callback' => 'signify_sanitize_category_list',
			'custom_control'    => 'Signify_Multi_Cat',
			'active_callback'   => 'signify_is_category_slider_active',
			'label'             => esc_html__( 'Select Categories', 'signify-pro' ),
			'name'              => 'signify_slider_select_category',
			'section'           => 'signify_featured_slider',
			'type'              => 'dropdown-categories',
		)
	);

	$slider_number = get_theme_mod( 'signify_slider_number', 2 );

	for ( $i = 1; $i <= $slider_number ; $i++ ) {
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_note_' . $i,
				'sanitize_callback' => 'sanitize_text_field',
				'custom_control'    => 'Signify_Note_Control',
				'active_callback'   => 'signify_is_slider_active',
				'label'             => esc_html__( 'Slide #', 'signify-pro' ) . $i,
				'section'           => 'signify_featured_slider',
				'type'              => 'description',
			)
		);

		// Post Sliders
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_post_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_post_slider_active',
				'input_attrs'       => array(
					'style' => 'width: 80px;',
				),
				'label'             => esc_html__( 'Post', 'signify-pro' ) . ' # ' . $i,
				'section'           => 'signify_featured_slider',
				'choices'           => signify_generate_post_array(),
				'type'              => 'select',
			)
		);

		// Page Sliders
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_page_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_page_slider_active',
				'label'             => esc_html__( 'Page', 'signify-pro' ) . ' # ' . $i,
				'section'           => 'signify_featured_slider',
				'type'              => 'dropdown-pages',
			)
		);

		// Image Sliders
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_image_' . $i,
				'sanitize_callback' => 'signify_sanitize_image',
				'custom_control'    => 'WP_Customize_Image_Control',
				'active_callback'   => 'signify_is_image_slider_active',
				'label'             => esc_html__( 'Image', 'signify-pro' ),
				'section'           => 'signify_featured_slider',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_link_' . $i,
				'sanitize_callback' => 'esc_url_raw',
				'active_callback'   => 'signify_is_image_slider_active',
				'label'             => esc_html__( 'Link', 'signify-pro' ),
				'section'           => 'signify_featured_slider',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_target_' . $i,
				'sanitize_callback' => 'signify_sanitize_checkbox',
				'active_callback'   => 'signify_is_image_slider_active',
				'label'             => esc_html__( 'Open Link in New Window/Tab', 'signify-pro' ),
				'section'           => 'signify_featured_slider',
				'type'    			=> 'checkbox',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_title_image_' . $i,
				'sanitize_callback' => 'signify_sanitize_image',
				'custom_control'    => 'WP_Customize_Image_Control',
				'active_callback'   => 'signify_is_slider_active',
				'label'             => esc_html__( 'Title Image', 'signify-pro' ),
				'section'           => 'signify_featured_slider',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_title_' . $i,
				'sanitize_callback' => 'wp_kses_post',
				'active_callback'   => 'signify_is_image_slider_active',
				'label'             => esc_html__( 'Title', 'signify-pro' ),
				'section'           => 'signify_featured_slider',
				'type'              => 'text',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_slider_content_' . $i,
				'sanitize_callback' => 'wp_kses_post',
				'active_callback'   => 'signify_is_image_slider_active',
				'label'             => esc_html__( 'Content', 'signify-pro' ),
				'section'           => 'signify_featured_slider',
				'type'              => 'textarea',
			)
		);

		signify_register_option( $wp_customize, array(
	            'name'              => 'signify_slider_text_' . $i,
	            'sanitize_callback' => 'sanitize_text_field',
	            'active_callback'   => 'signify_is_image_slider_active',
	            'label'             => esc_html__( 'Button Text', 'signify-pro' ) . $i,
	            'section'           => 'signify_featured_slider',
	            'type'              => 'text',
	        )
	    );

	    signify_register_option( $wp_customize, array(
	            'name'              => 'signify_slider_link_' . $i,
	            'sanitize_callback' => 'esc_url_raw',
	            'active_callback'   => 'signify_is_image_slider_active',
	            'label'             => esc_html__( 'Button Link', 'signify-pro' ) . $i,
	            'section'           => 'signify_featured_slider',
	        )
	    );

	    signify_register_option( $wp_customize, array(
	            'name'              => 'signify_slider_target_' . $i,
	            'sanitize_callback' => 'signify_sanitize_checkbox',
	            'active_callback'   => 'signify_is_image_slider_active',
	            'label'             => esc_html__( 'Open Link in New Window/Tab', 'signify-pro' ),
	            'section'           => 'signify_featured_slider',
	            'type'    			=> 'checkbox',
	        )
	    );

	} // End for().
}
add_action( 'customize_register', 'signify_slider_options' );

/** Active Callback Functions */

if ( ! function_exists( 'signify_is_slider_active' ) ) :
	/**
	* Return true if slider is active
	*
	* @since 1.0.0
	*/
	function signify_is_slider_active( $control ) {
		$enable = $control->manager->get_setting( 'signify_slider_option' )->value();

		//return true only if previwed page on customizer matches the type option selected
		return signify_check_section( $enable );
	}
endif;

if ( ! function_exists( 'signify_is_post_slider_active' ) ) :
	/**
	* Return true if page slider is active
	*
	* @since 1.0.0
	*/
	function signify_is_post_slider_active( $control ) {
		$type = $control->manager->get_setting( 'signify_slider_type' )->value();

		//return true only if previwed page on customizer matches the type of slider option selected and is or is not selected type
		return ( signify_is_slider_active( $control ) && 'post' === $type );
	}
endif;

if ( ! function_exists( 'signify_is_page_slider_active' ) ) :
	/**
	* Return true if page slider is active
	*
	* @since 1.0.0
	*/
	function signify_is_page_slider_active( $control ) {
		$type = $control->manager->get_setting( 'signify_slider_type' )->value();

		//return true only if previwed page on customizer matches the type of slider option selected and is or is not selected type
		return ( signify_is_slider_active( $control ) && 'page' === $type );
	}
endif;

if ( ! function_exists( 'signify_is_category_slider_active' ) ) :
	/**
	* Return true if page slider is active
	*
	* @since 1.0.0
	*/
	function signify_is_category_slider_active( $control ) {
		$type = $control->manager->get_setting( 'signify_slider_type' )->value();

		//return true only if previwed page on customizer matches the type of slider option selected and is or is not selected type
		return ( signify_is_slider_active( $control ) && 'category' === $type );
	}
endif;

if ( ! function_exists( 'signify_is_image_slider_active' ) ) :
	/**
	* Return true if image slider is active
	*
	* @since 1.0.0
	*/
	function signify_is_image_slider_active( $control ) {
		$type = $control->manager->get_setting( 'signify_slider_type' )->value();

		//return true only if previwed page on customizer matches the type of slider option selected and is or is not selected type
		return ( signify_is_slider_active( $control ) && 'custom' === $type );
	}
endif;

if ( ! function_exists( 'signify_is_image_slider_inactive' ) ) :
	/**
	* Return true if image slider is inactive
	*
	* @since 1.0.0
	*/
	function signify_is_image_slider_inactive( $control ) {
		$type = $control->manager->get_setting( 'signify_slider_type' )->value();

		//return true only if previwed page on customizer matches the type of slider option selected and is or is not selected type
		return ( signify_is_slider_active( $control ) && 'custom' !== $type );
	}
endif;
