<?php
/**
 * Hero Content Options
 *
 * @package Signify
 */

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

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_style',
			'default'           => 'style-one',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_hero_content_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_hero_content_options',
			'type'              => 'select',
		)
	);

	$types = signify_section_type_options();

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

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_post',
			'default'           => 0,
			'sanitize_callback' => 'signify_sanitize_post',
			'active_callback'   => 'signify_is_hero_post_content_active',
			'label'             => esc_html__( 'Post', 'signify-pro' ),
			'section'           => 'signify_hero_content_options',
			'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_hero_content_category',
			'default'           => '0',
			'sanitize_callback' => 'absint',
			'active_callback'   => 'signify_is_hero_category_content_active',
			'label'             => esc_html__( 'Category', 'signify-pro' ),
			'type'              => 'select',
			'choices'           => $cats,
			'section'           => 'signify_hero_content_options',
		)
	);

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

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_img_align',
			'default'           => 'content-align-right',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_hero_content_active',
			'choices'           => array(
				'content-align-right' => esc_html__( 'Right', 'signify-pro' ),
				'content-align-left'  => esc_html__( 'Left', 'signify-pro' ),
			),
			'label'             => esc_html__( 'Content Alignment', 'signify-pro' ),
			'section'           => 'signify_hero_content_options',
			'type'              => 'radio',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_txt_align',
			'default'           => 'text-align-left',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_hero_content_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_hero_content_options',
			'type'              => 'radio',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_sub_title',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_hero_content_active',
			'label'             => esc_html__( 'Sub Title', 'signify-pro' ),
			'section'           => 'signify_hero_content_options',
			'type'              => 'textarea',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_image',
			'sanitize_callback' => 'signify_sanitize_image',
			'custom_control'    => 'WP_Customize_Image_Control',
			'active_callback'   => 'signify_is_hero_custom_content_active',
			'label'             => esc_html__( 'Image', 'signify-pro' ),
			'section'           => 'signify_hero_content_options',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_link',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_hero_custom_content_active',
			'label'             => esc_html__( 'Image Link', 'signify-pro' ),
			'section'           => 'signify_hero_content_options',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_more_text',
			'sanitize_callback' => 'sanitize_text_field',
			'active_callback'   => 'signify_is_hero_custom_content_active',
			'label'             => esc_html__( 'Button Text', 'signify-pro' ),
			'section'           => 'signify_hero_content_options',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_hero_content_more_link',
			'sanitize_callback' => 'esc_url_raw',
			'active_callback'   => 'signify_is_hero_custom_content_active',
			'label'             => esc_html__( 'Button Link', 'signify-pro' ),
			'section'           => 'signify_hero_content_options',
		)
	);

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

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

		return signify_check_section( $enable );
	}
endif;

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

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

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

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

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

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

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

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

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

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