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

/**
 * Add service content options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_service_options( $wp_customize ) {
	// Add note to Jetpack Testimonial Section
    signify_register_option( $wp_customize, array(
            'name'              => 'signify_service_jetpack_note',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Signify_Note_Control',
            'label'             => sprintf( esc_html__( 'For all Services Options, go %1$shere%2$s', 'signify-pro' ),
                '<a href="javascript:wp.customize.section( \'signify_service\' ).focus();">',
                 '</a>'
            ),
           'section'            => 'service',
            'type'              => 'description',
            'priority'          => 1,
        )
    );

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

	// Add color scheme setting and control.
	signify_register_option( $wp_customize, array(
			'name'              => 'signify_service_option',
			'default'           => 'disabled',
			'sanitize_callback' => 'signify_sanitize_select',
			'choices'           => signify_section_visibility_options(),
			'label'             => esc_html__( 'Enable on', 'signify-pro' ),
			'section'           => 'signify_service',
			'type'              => 'select',
		)
	);

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

	signify_section_header_options( 'signify_service', 'service', 'signify_is_cpt_service_inactive' );

	$type = signify_section_type_options();

	$type['ect-service'] = esc_html__( 'Custom Post Type', 'signify-pro' );

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_service_type',
			'default'           => 'category',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_service_active',
			'description'       => sprintf( esc_html__( 'For Custom Post Type Content, install %1$sEssential Content Types%2$s Plugin with Services Type Enabled', 'signify-pro' ),
                '<a target="_blank" href="https://wordpress.org/plugins/essential-content-types/">',
                '</a>'
            ),
            'choices'           => $type,
			'label'             => esc_html__( 'Type', 'signify-pro' ),
			'section'           => 'signify_service',
			'type'              => 'select',
		)
	);

    signify_register_option( $wp_customize, array(
            'name'              => 'signify_service_cpt_note',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Signify_Note_Control',
            'active_callback'   => 'signify_is_service_cpt_content_active',
            /* translators: 1: <a>/link tag start, 2: </a>/link tag close. */
			'label'             => sprintf( esc_html__( 'For CPT heading and sub-heading, go %1$shere%2$s', 'signify-pro' ),
                 '<a href="javascript:wp.customize.control( \'ect_service_title\' ).focus();">',
                 '</a>'
            ),
            'section'           => 'signify_service',
            'type'              => 'description',
        )
    );

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_service_number',
			'default'           => 3,
			'sanitize_callback' => 'signify_sanitize_number_range',
			'active_callback'   => 'signify_is_service_active',
			'description'       => esc_html__( 'Save and refresh the page if No. of Services is changed (Max no of Services is 20)', 'signify-pro' ),
			'input_attrs'       => array(
				'style' => 'width: 100px;',
				'min'   => 0,
			),
			'label'             => esc_html__( 'No of items', 'signify-pro' ),
			'section'           => 'signify_service',
			'type'              => 'number',
			'transport'         => 'postMessage',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_service_meta_show',
			'default'           => 'hide-meta',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_service_post_page_category_cpt_content_active',
			'choices'           => signify_meta_show(),
			'label'             => esc_html__( 'Display Meta', 'signify-pro' ),
			'section'           => 'signify_service',
			'type'              => 'select',
		)
	);


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

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

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

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_service_page_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_service_page_content_active',
				'label'             => esc_html__( 'Services Page', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_service',
				'type'              => 'dropdown-pages',
				'allow_addition'    => true,
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_service_cpt_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_service_cpt_content_active',
				'label'             => esc_html__( 'Services', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_service',
				'type'              => 'select',
                'choices'           => signify_generate_post_array( 'ect-service' ),
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_service_note_' . $i,
				'sanitize_callback' => 'sanitize_text_field',
				'custom_control'    => 'Signify_Note_Control',
				'active_callback'   => 'signify_is_service_custom_service_active',
				'label'             => esc_html__( 'Services #', 'signify-pro' ) .  $i,
				'section'           => 'signify_service',
				'type'              => 'description',
			)
		);

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

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

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

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

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

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

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_service_link',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_service_active',
			'label'             => esc_html__( 'Button Link', 'signify-pro' ),
			'section'           => 'signify_service',
		)
	);

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

/** Active Callback Functions **/
if ( ! function_exists( 'signify_is_service_active' ) ) :
	/**
	* Return true if service content is active
	*
	* @since 1.0.0
	*/
	function signify_is_service_active( $control ) {
		$enable = $control->manager->get_setting( 'signify_service_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_service_inactive' ) ) :
	/**
	* Return true if cpt service is inactive
	*
	* @since 1.0.0
	*/
	function signify_is_cpt_service_inactive( $control ) {
		$type = $control->manager->get_setting( 'signify_service_type' )->value();

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

if ( ! function_exists( 'signify_is_service_post_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_service_post_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_service_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_service_active( $control ) && ( 'post' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_service_page_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_service_page_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_service_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_service_active( $control ) && ( 'page' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_service_cpt_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_service_cpt_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_service_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_service_active( $control ) && ( 'ect-service' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_service_category_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_service_category_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_service_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_service_active( $control ) && ( 'category' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_service_post_page_category_cpt_content_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_service_post_page_category_cpt_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_service_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_service_active( $control ) && ( 'category' === $type || 'ect-service' === $type || 'page' === $type || 'post' === $type ) );
	}
endif;

if ( ! function_exists( 'signify_is_service_custom_service_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since 1.0.0
	*/
	function signify_is_service_custom_service_active( $control ) {
		$type = $control->manager->get_setting( 'signify_service_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_service_active( $control ) && ( 'custom' === $type ) );
	}
endif;
