<?php
/**
* The template for adding Timeline Settings in Customizer
*
* @package Signify
*/

/**
 * Add timeline options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_timeline_options( $wp_customize ) {
	$wp_customize->add_section( 'signify_timeline', array(
			'panel' => 'signify_theme_options',
			'title' => esc_html__( 'Timeline', 'signify-pro' ),
		)
	);

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

	$choices = signify_section_type_options();

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_type',
			'default'           => 'category',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_timeline_active',
			'choices'           => $choices,
			'label'             => esc_html__( 'Select Content Type', 'signify-pro' ),
			'section'           => 'signify_timeline',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_subtitle',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_timeline_active',
			'label'             => esc_html__( 'Tagline', 'signify-pro' ),
			'section'           => 'signify_timeline',
			'type'              => 'text',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_title',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_timeline_active',
			'label'             => esc_html__( 'Title', 'signify-pro' ),
			'section'           => 'signify_timeline',
			'type'              => 'text',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_description',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_timeline_active',
			'label'             => esc_html__( 'Description', 'signify-pro' ),
			'section'           => 'signify_timeline',
			'type'              => 'textarea',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_image',
			'sanitize_callback' => 'signify_sanitize_image',
			'custom_control'    => 'WP_Customize_Image_Control',
			'active_callback'   => 'signify_is_timeline_active',
			'label'             => esc_html__( 'Story Image', 'signify-pro' ),
			'section'           => 'signify_timeline',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_number',
			'default'           => 4,
			'sanitize_callback' => 'signify_sanitize_number_range',
			'active_callback'   => 'signify_is_timeline_active',
			'description'       => esc_html__( 'Save and refresh the page if No. of items', 'signify-pro' ),
			'input_attrs'       => array(
				'style' => 'width: 45px;',
				'min'   => 0,
			),
			'label'             => esc_html__( 'No of items', 'signify-pro' ),
			'section'           => 'signify_timeline',
			'type'              => 'number',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_display_title',
			'default'           => 1,
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_post_page_cagetory_timeline_active',
			'label'             => esc_html__( 'Display Title', 'signify-pro' ),
			'section'           => 'signify_timeline',
			'type'   			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_display_date',
			'default'           => 1,
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_post_page_cagetory_timeline_active',
			'label'             => esc_html__( 'Display Date', 'signify-pro' ),
			'section'           => 'signify_timeline',
			'type'   			=> 'checkbox',
		)
	);

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

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

	$number = get_theme_mod( 'signify_timeline_number', 4 );

	for ( $i=1; $i <= $number; $i++ ) {

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_timeline_note_'. $i,
				'sanitize_callback' => 'sanitize_text_field',
				'custom_control'    => 'Signify_Note_Control',
				'active_callback'   => 'signify_is_custom_timeline_active',
				'label'             => esc_html__( 'Event #', 'signify-pro' ) .  $i,
				'section'           => 'signify_timeline',
				'type'              => 'description',
			)
		);

		//for post timeline
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_timeline_post_'. $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_post_timeline_active',
				'input_attrs'       => array(
					'style' => 'width: 100px;'
				),
				'label'             => esc_html__( 'Post', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_timeline',
				'choices'           => signify_generate_post_array(),
				'type'              => 'select',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_timeline_page_'. $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_page_timeline_active',
				'label'             => esc_html__( 'Page', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_timeline',
				'type'              => 'dropdown-pages',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_events_timeline_image_' . $i,
				'sanitize_callback' => 'signify_sanitize_image',
				'custom_control'    => 'WP_Customize_Image_Control',
				'active_callback'   => 'signify_is_custom_timeline_active',
				'label'             => esc_html__( 'Image', 'signify-pro' ),
				'section'           => 'signify_timeline',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_timeline_link_'. $i,
				'default'           => '#',
				'sanitize_callback' => 'esc_url_raw',
				'active_callback'   => 'signify_is_custom_timeline_active',
				'label'             => esc_html__( 'Link', 'signify-pro' ),
				'section'           => 'signify_timeline',
			)
		);

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

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_timeline_title_'. $i,
				'sanitize_callback' => 'sanitize_text_field',
				'active_callback'   => 'signify_is_custom_timeline_active',
				'label'             => esc_html__( 'Title', 'signify-pro' ),
				'section'           => 'signify_timeline',
				'type'              => 'text',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_events_timeline_date_'. $i,
				'sanitize_callback' => 'sanitize_text_field',
				'active_callback'   => 'signify_is_custom_timeline_active',
				'label'             => esc_html__( 'Date', 'signify-pro' ),
				'section'           => 'signify_timeline',
				'type'              => 'text',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_timeline_content_'. $i,
				'sanitize_callback' => 'wp_kses_post',
				'active_callback'   => 'signify_is_custom_timeline_active',
				'label'             => esc_html__( 'Content', 'signify-pro' ),
				'section'           => 'signify_timeline',
				'type'              => 'textarea',
			)
		);
	}

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_timeline_footer_text',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_timeline_active',
			'label'             => esc_html__( 'Footer Text', 'signify-pro' ),
			'section'           => 'signify_timeline',
			'type'              => 'text',
		)
	);
}
add_action( 'customize_register', 'signify_timeline_options', 10 );

/** Active Callbacks **/
if ( ! function_exists( 'signify_is_timeline_active' ) ) :
	/**
	* Return true if timeline is active
	*
	* @since  Signify Pro Pro 1.0
	*/
	function signify_is_timeline_active( $control ) {
		$enable = $control->manager->get_setting( 'signify_timeline_option' )->value();

		return ( signify_check_section( $enable ) );
	}
endif;

if ( ! function_exists( 'signify_is_post_timeline_active' ) ) :
	/**
	* Return true if page timeline is active
	*
	* @since  Signify Pro Pro 1.0
	*/
	function signify_is_post_timeline_active( $control ) {
		$type = $control->manager->get_setting( 'signify_timeline_type' )->value();

		return ( signify_is_timeline_active( $control ) && 'post' === $type );
	}
endif;


if ( ! function_exists( 'signify_is_page_timeline_active' ) ) :
	/**
	* Return true if page timeline is active
	*
	* @since  Signify Pro Pro 1.0
	*/
	function signify_is_page_timeline_active( $control ) {
		$type = $control->manager->get_setting( 'signify_timeline_type' )->value();

		return ( signify_is_timeline_active( $control ) && 'page' === $type );
	}
endif;


if ( ! function_exists( 'signify_is_category_timeline_active' ) ) :
	/**
	* Return true if page timeline is active
	*
	* @since  Signify Pro Pro 1.0
	*/
	function signify_is_category_timeline_active( $control ) {
		$type = $control->manager->get_setting( 'signify_timeline_type' )->value();

		return ( signify_is_timeline_active( $control ) && 'category' === $type );
	}
endif;


if ( ! function_exists( 'signify_is_post_page_cagetory_timeline_active' ) ) :
	/**
	* Return true if post/page/category timeline is active
	*
	* @since  Signify Pro Pro 1.0
	*/
	function signify_is_post_page_cagetory_timeline_active( $control ) {
		$type = $control->manager->get_setting( 'signify_timeline_type' )->value();

		return ( signify_is_timeline_active( $control ) && ( 'post' == $type || 'page' == $type || 'category' == $type ) );
	}
endif;


if ( ! function_exists( 'signify_is_custom_timeline_active' ) ) :
	/**
	* Return true if image timeline is active
	*
	* @since  Signify Pro Pro 1.0
	*/
	function signify_is_custom_timeline_active( $control ) {
		$type = $control->manager->get_setting( 'signify_timeline_type' )->value();

		return ( signify_is_timeline_active( $control ) && 'custom' === $type );
	}
endif;
