<?php
/**
 * Promotion Headline Options
 *
 * @package Signify
 */

/**
 * Add promotion headline options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_promo_head_options( $wp_customize ) {
	$wp_customize->add_section( 'signify_promotion_headline', array(
			'title' => esc_html__( 'Promotion Headline', 'signify-pro' ),
			'panel' => 'signify_theme_options',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promotion_headline_sub_title',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_promotion_headline_active',
			'label'             => esc_html__( 'Section Tagline', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'              => 'text',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promotion_headline_description',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_promotion_headline_active',
			'label'             => esc_html__( 'Section Description', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'              => 'textarea',
		)
	);

	$types = signify_section_type_options();

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_type',
			'default'           => 'page',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback' 	=> 'signify_is_promotion_headline_active',
			'choices'         	=> $types,
			'label'           	=> esc_html__( 'Type', 'signify-pro' ),
			'section'         	=> 'signify_promotion_headline',
			'type'            	=> 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_image_position',
			'default'           => 'content-align-left',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_promotion_headline_active',
			'choices'           => array(
				'content-align-right'  => esc_html__( 'Right Align', 'signify-pro' ),
				'content-align-center' => esc_html__( 'Center Align', 'signify-pro' ),
				'content-align-left'   => esc_html__( 'Left Align', 'signify-pro' ),
			),
			'label'             => esc_html__( 'Content Position', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'              => 'radio',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_text_alignment',
			'default'           => 'text-align-center',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_promotion_headline_active',
			'choices'           => array(
				'text-align-center' => esc_html__( 'Center', 'signify-pro' ),
				'text-align-right'  => esc_html__( 'Right', 'signify-pro' ),
				'text-align-left'   => esc_html__( 'Left', 'signify-pro' ),
			),
			'label'             => esc_html__( 'Text Alignment', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'              => 'radio',
		)
	);

	/*Overlay Option for Promotion Headline Background Image */
	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_background_image_opacity',
			'default'           => '0',
			'sanitize_callback' => 'signify_sanitize_number_range',
			'active_callback'   => 'signify_is_promotion_headline_active',
			'label'             => esc_html__( 'Background Image Overlay', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'              => 'number',
			'input_attrs'       => array(
				'style' => 'width: 60px;',
				'min'   => 0,
				'max'   => 100,
			),
		)
	);

	/* Promotion Headline Image */
	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_logo_image',
			'sanitize_callback' => 'signify_sanitize_image',
			'custom_control'    => 'WP_Customize_Image_Control',
			'active_callback'   => 'signify_is_promotion_headline_active',
			'label'             => esc_html__( 'Promo Logo', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_content_frame',
			'default'           => 1,
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_promotion_headline_active',
			'label'             => esc_html__( 'Display in Content Frame', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_text_color',
			'default'           => 0,
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_promotion_headline_active',
			'label'             => esc_html__( 'Display text in white color', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promotion_headline',
			'default'           => '0',
			'sanitize_callback' => 'signify_sanitize_post',
			'active_callback'   => 'signify_is_promotion_headline_page_content_active',
			'label'             => esc_html__( 'Page', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'              => 'dropdown-pages',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_post',
			'default'           => 0,
			'sanitize_callback' => 'signify_sanitize_post',
			'active_callback'   => 'signify_is_promotion_headline_post_content_active',
			'label'             => esc_html__( 'Post', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'choices'           => signify_generate_post_array(),
			'type'              => 'select',
		)
	);

	// create an empty array.
	$cats = array();

	$cats['0'] = esc_html__( '-- Select --', 'signify-pro' );

	// we loop over the categories and set the names and
	// labels we need.
	foreach ( get_categories() as $categories => $category ) {
		$cats[ $category->term_id ] = $category->name;
	}

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_category',
			'default'           => '0',
			'sanitize_callback' => 'absint',
			'active_callback'   => 'signify_is_promotion_headline_category_content_active',
			'label'             => esc_html__( 'Category', 'signify-pro' ),
			'type'              => 'select',
			'choices'           => $cats,
			'section'           => 'signify_promotion_headline',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_display_promotion_headline_title',
			'default'           => '1',
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_promotion_headline_post_page_category_product_content_active',
			'label'             => esc_html__( 'Display title', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
	            'name'              => 'signify_featured_video_link',
	            'sanitize_callback' => 'esc_url_raw',
	            'active_callback'   => 'signify_is_promotion_headline_active',
	            'label'             => esc_html__( 'Video Url', 'signify-pro' ),
	            'description'       => esc_html__( 'Provide a valid Video URL like a Youtube Video Link', 'signify-pro' ),
	            'section'           => 'signify_promotion_headline',
	        )
	);

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

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_content',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_promotion_headline_custom_content_active',
			'label'             => esc_html__( 'Content', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'              => 'textarea',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_image',
			'sanitize_callback' => 'signify_sanitize_image',
			'custom_control'    => 'WP_Customize_Image_Control',
			'active_callback'   => 'signify_is_promotion_headline_custom_content_active',
			'label'             => esc_html__( 'Background Image', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_link',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_promotion_headline_custom_content_active',
			'label'             => esc_html__( 'Background Image Link', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_target',
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_promotion_headline_custom_content_active',
			'label'             => esc_html__( 'Open Link in New Window/Tab', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'    			=> 'checkbox',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_more_text',
			'sanitize_callback' => 'sanitize_text_field',
			'active_callback'   => 'signify_is_promotion_headline_custom_content_active',
			'label'             => esc_html__( 'Button Text', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_more_link',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_promotion_headline_custom_content_active',
			'label'             => esc_html__( 'Button Link', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_promo_head_more_target',
			'sanitize_callback' => 'signify_sanitize_checkbox',
			'active_callback'   => 'signify_is_promotion_headline_custom_content_active',
			'label'             => esc_html__( 'Check to Open Button Link in New Window/Tab', 'signify-pro' ),
			'section'           => 'signify_promotion_headline',
			'type'    			=> 'checkbox',
		)
	);
}
add_action( 'customize_register', 'signify_promo_head_options' );

/** Active Callback Functions **/
if ( ! function_exists( 'signify_is_promotion_headline_active' ) ) :
	/**
	* Return true if promotion headline is active
	*
	* @since 1.0.0
	*/
	function signify_is_promotion_headline_active( $control ) {
		$enable = $control->manager->get_setting( 'signify_promo_head_visibility' )->value();

		return signify_check_section( $enable );
	}
endif;

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

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

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

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

if ( ! function_exists( 'signify_is_promotion_headline_category_content_active' ) ) :
	/**
	* Return true if promotion headline category content is active
	*
	* @since 1.0.0
	*/
	function signify_is_promotion_headline_category_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_promo_head_type' )->value();

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

if ( ! function_exists( 'signify_is_promotion_headline_post_page_category_product_content_active' ) ) :
	/**
	* Return true if promotion headline post/page/category content is active
	*
	* @since 1.0.0
	*/
	function signify_is_promotion_headline_post_page_category_product_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_promo_head_type' )->value();

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

if ( ! function_exists( 'signify_is_promotion_headline_custom_content_active' ) ) :
	/**
	* Return true if promotion headline custom content is active
	*
	* @since 1.0.0
	*/
	function signify_is_promotion_headline_custom_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_promo_head_type' )->value();

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

if ( ! function_exists( 'signify_is_promotion_headline_product_content_active' ) ) :
	/**
	* Return true if promotion headline product content is active
	*
	* @since 1.0.0
	*/
	function signify_is_promotion_headline_product_content_active( $control ) {
		$type = $control->manager->get_setting( 'signify_promo_head_type' )->value();

		return ( signify_is_promotion_headline_active( $control ) && ( 'product' === $type ) );
	}
endif;
