<?php
/**
 * Collection options
 *
 * @package Catch_Store
 */

/**
 * Add collection options to theme options
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function signify_collection_options( $wp_customize ) {
	$wp_customize->add_section( 'signify_collection', array(
			'title' => esc_html__( 'Collection', 'signify-pro' ),
			'panel' => 'signify_theme_options',
		)
	);

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

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

	signify_section_header_options( 'signify_collection', 'collection', 'signify_is_collection_active' );

	$type = signify_section_type_options();
	
	if ( signify_is_woocommerce_activated() ) {
		$type['product'] 	= esc_html__( 'Product', 'signify-pro' );
	}

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_collection_type',
			'default'           => 'category',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_collection_active',
            'choices'           => $type,
			'label'             => esc_html__( 'Type', 'signify-pro' ),
			'section'           => 'signify_collection',
			'type'              => 'select',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_collection_show',
			'default'           => 'hide-content',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_post_product_category_collection_active',
			'choices'           => signify_content_show(),
			'label'             => esc_html__( 'Display Content', 'signify-pro' ),
			'section'           => 'signify_collection',
			'type'              => 'select',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_collection_meta_show',
			'default'           => 'show-meta',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_post_product_category_collection_active',
			'choices'           => signify_meta_show(),
			'label'             => esc_html__( 'Display Meta', 'signify-pro' ),
			'section'           => 'signify_collection',
			'type'              => 'select',
		)
	);


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

	$number = get_theme_mod( 'signify_collection_number', 6 );

	for ( $i = 1; $i <= $number ; $i++ ) {
		signify_register_option( $wp_customize, array(
				'name'              => 'signify_collection_post_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'default'           => 0,
				'active_callback'   => 'signify_is_post_collection_active',
				'input_attrs'       => array(
					'style'             => 'width: 40px;'
				),
				'label'             => esc_html__( 'Post', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_collection',
				'choices'           => signify_generate_post_array(),
				'type'              => 'select'
			)
		);

		if ( signify_is_woocommerce_activated() ) {
			signify_register_option( $wp_customize, array(
					'name'              => 'signify_collection_product_' . $i,
					'sanitize_callback' => 'signify_sanitize_post',
					'active_callback'   => 'signify_is_product_collection_active',
					'label'             => esc_html__( 'Product', 'signify-pro' ) . ' ' . $i ,
					'section'           => 'signify_collection',
					'type'              => 'select',
					'choices'           => signify_generate_products_array(),
				)
			);
		}

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_collection_page_' . $i,
				'sanitize_callback' => 'signify_sanitize_post',
				'active_callback'   => 'signify_is_page_collection_active',
				'label'             => esc_html__( 'Page', 'signify-pro' ) . ' ' . $i ,
				'section'           => 'signify_collection',
				'type'              => 'dropdown-pages',
			)
		);

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_collection_note_' . $i,
				'sanitize_callback' => 'sanitize_text_field',
				'custom_control'    => 'Signify_Note_Control',
				'active_callback'   => 'signify_is_custom_collection_active',
				'label'             => esc_html__( 'Item #', 'signify-pro' ) .  $i,
				'section'           => 'signify_collection',
				'type'              => 'description',
			)
		);

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

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

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

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

		signify_register_option( $wp_customize, array(
				'name'              => 'signify_collection_content_' . $i,
				'sanitize_callback' => 'wp_kses_post',
				'active_callback'   => 'signify_is_custom_collection_active',
				'label'             => esc_html__( 'Content', 'signify-pro' ),
				'section'           => 'signify_collection',
				'type'              => 'textarea',
			)
		);
	} // End for().
}
add_action( 'customize_register', 'signify_collection_options', 10 );

/** Active Callback Functions **/
if( ! function_exists( 'signify_is_collection_active' ) ) :
	/**
	* Return true if collection is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_collection_active( $control ) {
		global $wp_query;

		$page_id = $wp_query->get_queried_object_id();

		// Front page display in Reading Settings
		$page_for_posts = get_option('page_for_posts');

		$enable = $control->manager->get_setting( 'signify_collection_option' )->value();

		//return true only if previwed page on customizer matches the type of content option selected
		return ( 'entire-site' == $enable || ( ( is_front_page() || ( is_home() && $page_for_posts != $page_id ) ) && 'homepage' == $enable )
	);
	}
endif;

if ( ! function_exists( 'signify_is_post_collection_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_post_collection_active( $control ) {
		$type = $control->manager->get_setting( 'signify_collection_type' )->value();

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

if ( ! function_exists( 'signify_is_page_collection_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_page_collection_active( $control ) {
		$type = $control->manager->get_setting( 'signify_collection_type' )->value();

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

if ( ! function_exists( 'signify_is_cat_collection_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_cat_collection_active( $control ) {
		$type = $control->manager->get_setting( 'signify_collection_type' )->value();

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

if ( ! function_exists( 'signify_is_post_product_category_collection_active' ) ) :
	/**
	* Return true if page/post/category/image content is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_post_product_category_collection_active( $control ) {
		$type = $control->manager->get_setting( 'signify_collection_type' )->value();

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

if ( ! function_exists( 'signify_is_custom_collection_active' ) ) :
	/**
	* Return true if page content is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_custom_collection_active( $control ) {
		$type = $control->manager->get_setting( 'signify_collection_type' )->value();

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

if ( ! function_exists( 'signify_is_product_collection_active' ) ) :
	/**
	* Return true if product content is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_product_collection_active( $control ) {
		$type = $control->manager->get_setting( 'signify_collection_type' )->value();

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