<?php
/**
 * Playlist Options
 *
 * @package Signify
 */

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

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

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

	$types = signify_section_type_options();

	unset( $types['custom'] );

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

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_playlist_post',
			'default'           => 0,
			'sanitize_callback' => 'signify_sanitize_post',
			'active_callback'   => 'signify_is_post_playlist_active',
			'label'             => esc_html__( 'Post', 'signify-pro' ),
			'section'           => 'signify_playlist',
			'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_playlist_category',
			'default'           => '0',
			'sanitize_callback' => 'absint',
			'active_callback'   => 'signify_is_category_playlist_active',
			'label'             => esc_html__( 'Category', 'signify-pro' ),
			'type'              => 'select',
			'choices'           => $cats,
			'section'           => 'signify_playlist',
		)
	);

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_playlist_section_sub_title',
			'sanitize_callback' => 'wp_kses_post',
			'active_callback'   => 'signify_is_playlist_active',
			'label'             => esc_html__( 'Sub Title', 'signify-pro' ),
			'section'           => 'signify_playlist',
			'type'              => 'text',
		)
	);

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

	signify_register_option( $wp_customize, array(
			'name'              => 'signify_playlist_image_alignment',
			'default'           => 'content-align-right',
			'sanitize_callback' => 'signify_sanitize_select',
			'active_callback'   => 'signify_is_playlist_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_playlist',
			'type'              => 'radio',
		)
	);
}
add_action( 'customize_register', 'signify_playlist', 12 );

/** Active Callback Functions **/
if ( ! function_exists( 'signify_is_playlist_active' ) ) :
	/**
	* Return true if playlist is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_playlist_active( $control ) {
		$enable = $control->manager->get_setting( 'signify_playlist_visibility' )->value();

		return signify_check_section( $enable );
	}
endif;

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

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

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

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

if ( ! function_exists( 'signify_is_category_playlist_active' ) ) :
	/**
	* Return true if category playlist is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_category_playlist_active( $control ) {
		$type = $control->manager->get_setting( 'signify_playlist_type' )->value();

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

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

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

if ( ! function_exists( 'signify_is_playlist_custom_active' ) ) :
	/**
	* Return true if custom playlist is active
	*
	* @since Signify Pro 1.0
	*/
	function signify_is_playlist_custom_active( $control ) {
		$type = $control->manager->get_setting( 'signify_playlist_type' )->value();

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