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

/**
 * Add hero content options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function personaltrainer_slider_options( $wp_customize ) {
	$wp_customize->add_section( 'personaltrainer_featured_slider', array(
			'panel' => 'personaltrainer_theme_options',
			'title' => esc_html__( 'Featured Slider', 'personal-trainer' ),
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_option',
			'default'           => 'disabled',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'choices'           => personaltrainer_section_visibility_options(),
			'label'             => esc_html__( 'Enable on', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
			'type'              => 'select',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_transition_effect',
			'default'           => 'fade',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_slider_transition_effect_enabled',
			'choices'           => personaltrainer_slider_transition_effects(),
			'label'             => esc_html__( 'Transition Effect', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
			'type'              => 'select',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_transition_delay',
			'default'           => '4',
			'sanitize_callback' => 'absint',
			'active_callback'   => 'personaltrainer_is_slider_transition_delay_enabled',
			'description'       => esc_html__( 'seconds(s)', 'personal-trainer' ),
			'input_attrs'       => array(
				'style' => 'width: 40px;',
			),
			'label'             => esc_html__( 'Transition Delay', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_transition_length',
			'default'           => '1',
			'sanitize_callback' => 'absint',

			'active_callback'   => 'personaltrainer_is_slider_transition_length_enabled',
			'description'       => esc_html__( 'seconds(s)', 'personal-trainer' ),
			'input_attrs'       => array(
				'style' => 'width: 100px;',
			),
			'label'             => esc_html__( 'Transition Length', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_image_loader',
			'default'           => 'false',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_slider_image_loader_enabled',
			'choices'           => personaltrainer_slider_image_loader(),
			'label'             => esc_html__( 'Image Loader', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
			'type'              => 'select',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_disable_pager',
			'sanitize_callback' => 'personaltrainer_sanitize_checkbox',
			'active_callback'   => 'personaltrainer_is_slider_disable_pager_enabled',
			'label'             => esc_html__( 'Check to disable pager/navigation', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
			'type'              => 'checkbox',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_type',
			'default'           => 'demo',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_slider_active',
			'choices'           => personaltrainer_section_type_options(),
			'label'             => esc_html__( 'Select Slider Type', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
			'type'              => 'select',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_number',
			'default'           => '4',
			'sanitize_callback' => 'personaltrainer_sanitize_number_range',

			'active_callback'   => 'personaltrainer_is_page_slider_active',
			'description'       => esc_html__( 'Save and refresh the page if this option is changed', 'personal-trainer' ),
			'input_attrs'       => array(
				'style' => 'width: 45px;',
				'min'   => 0,
				'max'   => 20,
				'step'  => 1,
			),
			'label'             => esc_html__( 'No of items', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
			'type'              => 'number',
			'transport'         => 'postMessage',
		)
	);

	$content_options = personaltrainer_content_show();

	unset( $content_options['full-content'] );

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_slider_content_show',
			'default'           => 'hide-content',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_slider_content_show_enabled',
			'choices'           => personaltrainer_content_show(),
			'label'             => esc_html__( 'Display Content', 'personal-trainer' ),
			'section'           => 'personaltrainer_featured_slider',
			'type'              => 'select',
		)
	);

	$slider_number = get_theme_mod( 'personaltrainer_slider_number', 4 );

	for ( $i = 1; $i <= $slider_number ; $i++ ) {
		// Page Sliders
		personaltrainer_register_option( $wp_customize, array(
				'name'              =>'personaltrainer_slider_page_' . $i,
				'sanitize_callback' => 'personaltrainer_sanitize_post',
				'active_callback'   => 'personaltrainer_is_page_slider_active',
				'label'             => esc_html__( 'Page', 'personal-trainer' ) . ' # ' . $i,
				'section'           => 'personaltrainer_featured_slider',
				'type'              => 'dropdown-pages',
			)
		);
	} // End for().
}
add_action( 'customize_register', 'personaltrainer_slider_options' );


/**
 * Returns an array of feature slider transition effects
 *
 * @since Personal Trainer 0.1
 */
function personaltrainer_slider_transition_effects() {
	$options = array(
		'fade'       => esc_html__( 'Fade', 'personal-trainer' ),
		'fadeout'    => esc_html__( 'Fade Out', 'personal-trainer' ),
		'none'       => esc_html__( 'None', 'personal-trainer' ),
		'scrollHorz' => esc_html__( 'Scroll Horizontal', 'personal-trainer' ),
		'scrollVert' => esc_html__( 'Scroll Vertical', 'personal-trainer' ),
		'flipHorz'   => esc_html__( 'Flip Horizontal', 'personal-trainer' ),
		'flipVert'   => esc_html__( 'Flip Vertical', 'personal-trainer' ),
		'tileSlide'  => esc_html__( 'Tile Slide', 'personal-trainer' ),
		'tileBlind'  => esc_html__( 'Tile Blind', 'personal-trainer' ),
	);

	return apply_filters( 'personaltrainer_slider_transition_effects', $options );
}


/**
 * Returns an array of featured slider image loader options
 *
 * @since Personal Trainer 0.1
 */
function personaltrainer_slider_image_loader() {
	$options = array(
		'true'  => esc_html__( 'True', 'personal-trainer' ),
		'wait'  => esc_html__( 'Wait', 'personal-trainer' ),
		'false' => esc_html__( 'False', 'personal-trainer' ),
	);

	return apply_filters( 'personaltrainer_slider_image_loader', $options );
}


/**
 * Returns an array of featured content show registered for Lucida.
 *
 * @since Personal Trainer 0.1
 */
function personaltrainer_content_show() {
	$options = array(
		'excerpt'      => esc_html__( 'Show Excerpt', 'personal-trainer' ),
		'full-content' => esc_html__( 'Full Content', 'personal-trainer' ),
		'hide-content' => esc_html__( 'Hide Content', 'personal-trainer' ),
	);
	return apply_filters( 'personaltrainer_content_show', $options );
}

/**
 * Returns an array of featured content show registered for Lucida.
 *
 * @since Personal Trainer 0.1
 */
function personaltrainer_meta_show() {
	$options = array(
		'show-meta'      => esc_html__( 'Show Meta', 'personal-trainer' ),
		'hide-meta' => esc_html__( 'Hide Meta', 'personal-trainer' ),
	);
	return apply_filters( 'personaltrainer_content_show', $options );
}

/** Active Callback Functions */

if( ! function_exists( 'personaltrainer_is_slider_active' ) ) :
	/**
	* Return true if slider is active
	*
	* @since Personal Trainer 0.1
	*/
	function personaltrainer_is_slider_active( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_slider_option' )->value();

		//return true only if previewed page on customizer matches the type of slider option selected
		return ( personaltrainer_check_section( $enable ) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_page_slider_active' ) ) :
	/**
	* Return true if page slider is active
	*
	* @since Personal Trainer 0.1
	*/
	function personaltrainer_is_page_slider_active( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_slider_type' )->value();

		//return true only if previewed page on customizer matches the type of slider option selected
		return ( personaltrainer_is_slider_active( $control ) && 'page' === $enable );
	}
endif;

if( ! function_exists( 'personaltrainer_is_slider_transition_effect_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_slider_transition_effect_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_slider_transition_effect' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_slider_transition_effect' )->default) && (personaltrainer_is_slider_active($control)) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_slider_transition_delay_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_slider_transition_delay_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_slider_transition_delay' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_slider_transition_delay' )->default) && (personaltrainer_is_slider_active($control)) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_slider_transition_length_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_slider_transition_length_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_slider_transition_length' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_slider_transition_length' )->default) && (personaltrainer_is_slider_active($control)) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_slider_image_loader_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_slider_image_loader_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_slider_image_loader' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_slider_image_loader' )->default) && (personaltrainer_is_slider_active($control)) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_slider_disable_pager_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_slider_disable_pager_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_slider_disable_pager' )->value();
		return ( ($enable != '') && (personaltrainer_is_slider_active($control)) );
	}
endif;

if( ! function_exists( 'personaltrainer_is_slider_content_show_enabled' ) ) :
	/**
	* @since Personal Trainer 1.2
	*/
	function personaltrainer_is_slider_content_show_enabled( $control ) {
		$enable = $control->manager->get_setting( 'personaltrainer_slider_content_show' )->value();
		return ( ($enable != $control->manager->get_setting( 'personaltrainer_slider_content_show' )->default) && (personaltrainer_is_page_slider_active($control)) );
	}
endif;
