<?php
/**
 * Stats options
 *
 * @package Signify Pro
 */

/**
 * Add stats content options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_stats_options( $wp_customize ) {

    $wp_customize->add_section( 'signify_stats', array(
			'title' => esc_html__( 'Stats', 'signify-pro' ),
			'panel' => 'signify_theme_options',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_stats_option',
			'default'           => 'disabled',
			'sanitize_callback' => 'signify_sanitize_select',
			'choices'           => signify_section_visibility_options(),
			'label'             => esc_html__( 'Enable on', 'signify-pro' ),
			'section'           => 'signify_stats',
			'type'              => 'select',
		)
	);
	
	signify_register_option( $wp_customize, array(
			'name'              => 'signify_stats_layout',
			'default'           => 'layout-four',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_stats_active',
			'choices'           => signify_sections_layout_options(),
			'label'             => esc_html__( 'Layout', 'signify-pro' ),
			'section'           => 'signify_stats',
			'type'              => 'select',
		)
	);

	signify_section_header_options( 'signify_stats', 'stats', 'signify_is_cpt_stats_inactive' );

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_stats_type',
			'default'           => 'category',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_stats_active',
            'choices'           => signify_section_type_options(),
			'label'             => esc_html__( 'Type', 'signify-pro' ),
			'section'           => 'signify_stats',
			'type'              => 'select',
		)
	);

    signify_register_option( $wp_customize, array(
			'name'              => 'signify_stats_number',
			'default'           => 4,
			'sanitize_callback' => 'signify_sanitize_number_range',
			'active_callback'   => 'signify_is_stats_active',
			'description'       => esc_html__( 'Save and refresh the page if No. of Items is changed', 'signify-pro' ),
			'input_attrs'       => array(
				'style' => 'width: 100px;',
				'min'   => 0,
			),
			'label'             => esc_html__( 'No of Items', 'signify-pro' ),
			'section'           => 'signify_stats',
			'type'              => 'number',
			'transport'         => 'postMessage',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_stats_show',
			'default'           => 'excerpt',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_stats_post_page_category_cpt_content_active',
			'choices'           => signify_content_show(),
			'label'             => esc_html__( 'Display Content', 'signify-pro' ),
			'section'           => 'signify_stats',
			'type'              => 'select',
		)
	);

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

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

	//loop for stats post content
	for ( $i = 1; $i <= $number ; $i++ ) {
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_stats_post_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'default'           => 0,
				'active_callback'   => 'signify_is_stats_post_content_active',
				'input_attrs'       => array(
					'style'             => 'width: 40px;'
				),
				'label'             => esc_html__( 'Post', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_stats',
				'choices'           => signify_generate_post_array(),
				'type'              => 'select'
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_stats_page_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_stats_page_content_active',
				'label'             => esc_html__( 'Stats Page', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_stats',
				'type'              => 'dropdown-pages',
			)
		);


		signify_register_option( $wp_customize, array(
				'name'              => 'signify_stats_note_' . $i,
				'sanitize_callback' => 'sanitize_text_field',
				'custom_control'    => 'Signify_Note_Control',
				'active_callback'   => 'signify_is_stats_image_content_active',
				'label'             => esc_html__( 'Stats #', 'signify-pro' ) .  $i,
				'section'           => 'signify_stats',
				'type'              => 'description',
			)
		);

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

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

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

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

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

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_stats_more_button_text_' . $i,
				'sanitize_callback' => 'sanitize_text_field',
				'active_callback'   => 'signify_is_stats_image_content_active',
				'label'             => esc_html__( 'More Button Text', 'signify-pro' ),
				'section'           => 'signify_stats',
				'type'              => 'text',
			)
		);
	} // End for().

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_stats_link',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_stats_active',
			'label'             => esc_html__( 'Button Link', 'signify-pro' ),
			'section'           => 'signify_stats',
		)
	);

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

/** Active Callback Functions **/
if ( ! function_exists( 'signify_is_stats_active' ) ) :
	/**
	* Return true if stats content is active
	*
	* @since 1.0.0
	*/
	function signify_is_stats_active( $control ) {
		$enable = $control->manager->get_setting( 'signify_stats_option' )->value();

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

if ( ! function_exists( 'signify_is_cpt_stats_inactive' ) ) :
	/**
	* Return true if CPT stats content is inactive
	*
	* @since 1.0.0
	*/

	function signify_is_cpt_stats_inactive( $control ) {
		$type = $control->manager->get_setting( 'signify_stats_type' )->value();

		return ( signify_is_stats_active( $control ) && 'ect-service' !== $type );
	}
endif;

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

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

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

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

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

		//return true only if previewed page on customizer matches the type of content option selected and is or is not selected type
		return ( signify_is_stats_active( $control ) && ( 'ect-service' === $type ) );
	}
endif;

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

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

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

		//return true only if previewed page on customizer matches the type of content option selected and is or is not selected type
		return ( signify_is_stats_active( $control ) && ( 'category' === $type || 'page' === $type || 'post' === $type ) );
	}
endif;

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

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