<?php
/**
* The template for adding Service Settings in Customizer
*
 * @package Personal_Trainer
*/

function personaltrainer_service_options( $wp_customize ) {
	// Add note to ECT Service Section
    personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_jetpack_portfolio_cpt_note',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Personaltrainer_Note_Control',
            'label'             => sprintf( esc_html__( 'For Service Options for Personal Trainer Theme, go %1$shere%2$s', 'personal-trainer' ),
                 '<a href="javascript:wp.customize.section( \'personaltrainer_service\' ).focus();">',
                 '</a>'
            ),
            'section'           => 'ect_service',
            'type'              => 'description',
            'priority'          => 1,
        )
    );

	$wp_customize->add_section( 'personaltrainer_service', array(
			'panel'           => 'personaltrainer_theme_options',
			'title'           => esc_html__( 'Service', 'personal-trainer' ),
		)
	);

	personaltrainer_register_option( $wp_customize, array(
            'name'              => 'personaltrainer_service_note_1',
            'sanitize_callback' => 'sanitize_text_field',
            'custom_control'    => 'Personaltrainer_Note_Control',
            'active_callback'   => 'personaltrainer_is_ect_service_inactive',
            'label'             => sprintf( esc_html__( 'For Services, install %1$sEssential Content Types%2$s Plugin with Testimonial Content Type Enabled', 'personal-trainer' ),
                '<a target="_blank" href="https://wordpress.org/plugins/essential-content-types/">',
                '</a>'
            ),
            'section'           => 'personaltrainer_service',
            'type'              => 'description',
            'priority'          => 1,
        )
    );

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

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_service_headline',
			'default'           => esc_html__( 'Services', 'personal-trainer' ),
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'personaltrainer_is_service_active',
			'label'             => esc_html__( 'Service Archive Title', 'personal-trainer' ),
			'section'           => 'personaltrainer_service',
			'type'              => 'text',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_service_subheadline',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'personaltrainer_is_service_active',
			'label'             => esc_html__( 'Service Archive Content', 'personal-trainer' ),
			'section'           => 'personaltrainer_service',
			'type'              => 'textarea',
		)
	);

	personaltrainer_register_option( $wp_customize, array(
				'name'              => 'personaltrainer_service_number',
				'default'           => 3,
				'sanitize_callback' => 'personaltrainer_sanitize_number_range',
				'active_callback'   => 'personaltrainer_is_service_active',
				'description'       => esc_html__( 'Save and refresh the page if this option is changed', 'personal-trainer' ),
				'input_attrs'       => array(
					'style' => 'width: 45px;',
					'min'   => 0,
				),
				'label'             => esc_html__( 'No of items', 'personal-trainer' ),
				'section'           => 'personaltrainer_service',
				'type'              => 'number',
		)
	);
	if( 1 == get_theme_mod( 'personaltrainer_service_enable_title' ) )
	{
		personaltrainer_register_option( $wp_customize, array(
				'name'              => 'personaltrainer_service_enable_title',
				'default'           => 1,
				'sanitize_callback' => 'personaltrainer_sanitize_checkbox',
				'active_callback'   => 'personaltrainer_is_service_active',
				'label'             => esc_html__( 'Check to Enable Title', 'personal-trainer' ),
				'section'           => 'personaltrainer_service',
				'type'              => 'checkbox',
			)
		);
	}
	personaltrainer_register_option( $wp_customize, array(
			'name'              => 'personaltrainer_service_show',
			'default'           => 'excerpt',
			'sanitize_callback' => 'personaltrainer_sanitize_select',
			'active_callback'   => 'personaltrainer_is_services_content_show_enabled',
			'choices'           => personaltrainer_content_show(),
			'label'             => esc_html__( 'Display Content', 'personal-trainer' ),
			'section'           => 'personaltrainer_service',
			'type'              => 'select',
		)
	);

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

	for ( $i = 1; $i <= $number ; $i++ ) {
		//for CPT
		personaltrainer_register_option( $wp_customize, array(
				'name'              => 'personaltrainer_service_cpt_' . $i,
				'sanitize_callback' => 'personaltrainer_sanitize_post',
				'default'           => 0,
				'active_callback'   => 'personaltrainer_is_service_active',
				'label'             => esc_html__( 'Service ', 'personal-trainer' ) . ' ' . $i ,
				'section'           => 'personaltrainer_service',
				'type'              => 'select',
				'choices'           => personaltrainer_generate_post_array( 'ect-service' ),
			)
		);
	} // End for().
}
add_action( 'customize_register', 'personaltrainer_service_options' );

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

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

if ( ! function_exists( 'personaltrainer_is_ect_service_active' ) ) :
    /**
    * Return true if service is active
    *
    * @since Personal Trainer 0.1
    */
    function personaltrainer_is_ect_service_active( $control ) {
        return ( class_exists( 'Essential_Content_Service' ) || class_exists( 'Essential_Content_Pro_Service' ) );
    }
endif;

if ( ! function_exists( 'personaltrainer_is_ect_service_inactive' ) ) :
    /**
    * Return true if service is active
    *
    * @since Personal Trainer 0.1
    */
    function personaltrainer_is_ect_service_inactive( $control ) {
        return ! ( class_exists( 'Essential_Content_Service' ) || class_exists( 'Essential_Content_Pro_Service' ) );
    }
endif;

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