<?php

/**
 * Adore Themes Customizer
 *
 * @package Adore News
 *
 * Banner Section
 */

$wp_customize->add_section(
	'adore_news_banner_section',
	array(
		'title'    => esc_html__( 'Banner Section', 'adore-news' ),
		'panel'    => 'adore_news_frontpage_panel',
		'priority' => 10,
	)
);

// Banner enable setting.
$wp_customize->add_setting(
	'adore_news_banner_section_enable',
	array(
		'default'           => false,
		'sanitize_callback' => 'adore_news_sanitize_checkbox',
	)
);

$wp_customize->add_control(
	new Adore_News_Toggle_Checkbox_Custom_control(
		$wp_customize,
		'adore_news_banner_section_enable',
		array(
			'label'    => esc_html__( 'Enable Banner Section', 'adore-news' ),
			'type'     => 'checkbox',
			'settings' => 'adore_news_banner_section_enable',
			'section'  => 'adore_news_banner_section',
		)
	)
);

// banner content type settings.
$wp_customize->add_setting(
	'adore_news_banner_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'adore_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'adore_news_banner_content_type',
	array(
		'label'           => esc_html__( 'Content type:', 'adore-news' ),
		'description'     => esc_html__( 'Choose where you want to render the content from.', 'adore-news' ),
		'section'         => 'adore_news_banner_section',
		'type'            => 'select',
		'active_callback' => 'adore_news_if_banner_enabled',
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'adore-news' ),
			'category' => esc_html__( 'Category', 'adore-news' ),
		),
	)
);

for ( $i = 1; $i <= 5; $i++ ) {
	// banner post setting.
	$wp_customize->add_setting(
		'adore_news_banner_post_' . $i,
		array(
			'sanitize_callback' => 'adore_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'adore_news_banner_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'adore-news' ), $i ),
			'section'         => 'adore_news_banner_section',
			'type'            => 'select',
			'choices'         => adore_news_get_post_choices(),
			'active_callback' => 'adore_news_banner_section_content_type_post_enabled',
		)
	);
}

// banner category setting.
$wp_customize->add_setting(
	'adore_news_banner_category',
	array(
		'sanitize_callback' => 'adore_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'adore_news_banner_category',
	array(
		'label'           => esc_html__( 'Category', 'adore-news' ),
		'section'         => 'adore_news_banner_section',
		'type'            => 'select',
		'choices'         => adore_news_get_post_cat_choices(),
		'active_callback' => 'adore_news_banner_section_content_type_category_enabled',
	)
);

/*========================Active Callback==============================*/
function adore_news_if_banner_enabled( $control ) {
	return $control->manager->get_setting( 'adore_news_banner_section_enable' )->value();
}
function adore_news_banner_section_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'adore_news_banner_content_type' )->value();
	return adore_news_if_banner_enabled( $control ) && ( 'post' === $content_type );
}
function adore_news_banner_section_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'adore_news_banner_content_type' )->value();
	return adore_news_if_banner_enabled( $control ) && ( 'category' === $content_type );
}
