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

/**
 * Add featured content options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_events_options( $wp_customize ) {
	$wp_customize->add_section( 'signify_events', array(
			'panel' => 'signify_theme_options',
			'title' => esc_html__( 'Events', 'signify-pro' ),
		)
	);

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

	    signify_register_option( $wp_customize, array(
            'name'              => 'signify_events_style',
            'default'           => 'style-one',
            'sanitize_callback' => 'signify_sanitize_select',
            'active_callback'   => 'signify_is_events_active',
            'choices'           => array(
                'style-one' => esc_html__( 'Style One', 'signify-pro' ),
                'style-two' => esc_html__( 'Style Two', 'signify-pro' ),
            ),
            'label'             => esc_html__( 'Style', 'signify-pro' ),
            'section'           => 'signify_events',
            'type'              => 'select',
        )
    );

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_events_bg_image',
			'active_callback'   => 'signify_is_events_active',
			'sanitize_callback' => 'esc_url_raw',
			'custom_control'    => 'WP_Customize_Image_Control',
			'label'             => esc_html__( 'Background Image', 'signify-pro' ),
			'section'           => 'signify_events',
		)
	);

	$choices = signify_section_type_options();

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_events_layout',
			'default'           => 'layout-three',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_events_style_two',
			'choices'           => signify_sections_layout_options(),
			'label'             => esc_html__( 'Select Layout', 'signify-pro' ),
			'section'           => 'signify_events',
			'type'              => 'select',
		)
	);

	signify_section_header_options( 'signify_events', 'events', 'signify_is_events_active' );

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_events_number',
			'default'           => 3,
			'sanitize_callback' => 'signify_sanitize_number_range',
			'active_callback'   => 'signify_is_events_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_events',
			'type'              => 'number',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_events_display_date',
			'default'           => 1,
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_events_active',
			'label'             => esc_html__( 'Display Event Date', 'signify-pro' ),
			'section'           => 'signify_events',
			'type'              => 'checkbox',
		)
	);

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

	$number = get_theme_mod( 'signify_events_number', 3 );

	for ( $i=1; $i <= $number; $i++ ) {
		//for post events
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_events_post_'. $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_post_events_active',
				'input_attrs'       => array(
					'style' => 'width: 100px;'
				),
				'label'             => esc_html__( 'Post', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_events',
				'choices'           => signify_generate_post_array(),
				'type'              => 'select',
			)
		);

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

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

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

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

		signify_register_option( $wp_customize, array(
				'custom_control'    => 'WP_Customize_Image_Control',
				'name'              => 'signify_events_image_'. $i,
				'sanitize_callback' => 'sanitize_text_field',
				'active_callback'   => 'signify_is_custom_events_style_two_active',
				'label'             => esc_html__( 'Image', 'signify-pro' ),
				'section'           => 'signify_events',
			)
		);

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

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_events_date_day_'. $i,
				'sanitize_callback' => 'signify_sanitize_number_range',
				'active_callback'   => 'signify_is_custom_events_date_active',
				'label'             => esc_html__( 'Date Day', 'signify-pro' ),
				'section'           => 'signify_events',
				'type'              => 'number',
				'input_attrs'       => array(
					'style' => 'width: 45px;',
					'min'   => 1,
					'max'   => 31
				),
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_events_date_month_'. $i,
				'sanitize_callback' => 'signify_sanitize_number_range',
				'active_callback'   => 'signify_is_custom_events_date_active',
				'label'             => esc_html__( 'Date Month', 'signify-pro' ),
				'section'           => 'signify_events',
				'type'              => 'number',
				'input_attrs'       => array(
					'style' => 'width: 45px;',
					'min'   => 1,
					'max'   => 12
				),
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_events_date_year_'. $i,
				'sanitize_callback' => 'signify_sanitize_number_range',
				'active_callback'   => 'signify_is_custom_events_date_active',
				'label'             => esc_html__( 'Date Year', 'signify-pro' ),
				'section'           => 'signify_events',
				'type'              => 'number',
				'input_attrs'       => array(
					'style' => 'width: 100px;',
					'min'   => 1000,
					'max'   => 9999
				),
			)
		);

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

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_events_individual_text_' . $i,
				'sanitize_callback' => 'sanitize_text_field',
				'active_callback'   => 'signify_is_events_active',
				'label'             => esc_html__( 'More Text', 'signify-pro' ) . ' ' . $i,
				'section'           => 'signify_events',
				'type'              => 'text',
			)
		);
	}

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_events_text',
			'sanitize_callback' => 'sanitize_text_field',
			'active_callback'   => 'signify_is_events_active',
			'label'             => esc_html__( 'Button Text', 'signify-pro' ),
			'section'           => 'signify_events',
			'type'              => 'text',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_events_link',
			'default'           => '#',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_events_active',
			'label'             => esc_html__( 'Button Link', 'signify-pro' ),
			'section'           => 'signify_events',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_events_target',
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_events_active',
			'label'             => esc_html__( 'Open Link in New Window/Tab', 'signify-pro' ),
			'section'           => 'signify_events',
			'type'              => 'checkbox',
		)
	);
}
add_action( 'customize_register', 'signify_events_options', 10 );

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

		return signify_check_section( $enable );
	}
endif;

if ( ! function_exists( 'signify_is_events_style_two' ) ) :
	/**
	* Return true if style is twl
	*
	* @since  Signify Pro 1.0
	*/
	function signify_is_events_style_two( $control ) {
		$style = $control->manager->get_setting( 'signify_events_style' )->value();

		return ( signify_is_events_active( $control ) && 'style-two' === $style );
	}
endif;

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

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

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

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

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

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

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

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

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

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

if ( ! function_exists( 'signify_is_custom_events_date_active' ) ) :
	/**
	* Return true if image events is active
	*
	* @since  Signify Pro 1.0
	*/
	function signify_is_custom_events_date_active( $control ) {
		$type = $control->manager->get_setting( 'signify_events_type' )->value();
		$date = $control->manager->get_setting( 'signify_events_display_date' )->value();

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

if ( ! function_exists( 'signify_is_custom_events_style_two_active' ) ) :
	/**
	* Return true if image events is active
	*
	* @since  Signify Pro 1.0
	*/
	function signify_is_custom_events_style_two_active( $control ) {
		$type  = $control->manager->get_setting( 'signify_events_type' )->value();
		$style = $control->manager->get_setting( 'signify_events_style' )->value();

		return ( signify_is_events_active( $control ) && 'style-two' === $style && 'custom' === $type );
	}
endif;
