<?php
/**
 * Adore Themes Customizer
 *
 * @package Terminal News
 *
 * Banner Section
 */

$wp_customize->add_section(
	'terminal_news_banner_section',
	array(
		'title'    => esc_html__( 'Banner Section', 'terminal-news' ),
		'panel'    => 'terminal_news_frontpage_panel',
		'priority' => 20,
	)
);

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

$wp_customize->add_control(
	new Terminal_News_Toggle_Checkbox_Custom_control(
		$wp_customize,
		'terminal_news_banner_section_enable',
		array(
			'label'    => esc_html__( 'Enable Banner Section', 'terminal-news' ),
			'type'     => 'checkbox',
			'settings' => 'terminal_news_banner_section_enable',
			'section'  => 'terminal_news_banner_section',
			'priority' => 10,
		)
	)
);

// Main News Sub Heading.
$wp_customize->add_setting(
	'terminal_news_banner_main_news_sub_heading',
	array(
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	new Terminal_News_Sub_Section_Heading_Custom_Control(
		$wp_customize,
		'terminal_news_banner_main_news_sub_heading',
		array(
			'label'           => esc_html__( 'Main News Section', 'terminal-news' ),
			'settings'        => 'terminal_news_banner_main_news_sub_heading',
			'section'         => 'terminal_news_banner_section',
			'active_callback' => 'terminal_news_if_banner_enabled',
			'priority'        => 15,
		)
	)
);

// Main News title settings.
$wp_customize->add_setting(
	'terminal_news_banner_main_news_title',
	array(
		'default'           => __( 'Main News', 'terminal-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'terminal_news_banner_main_news_title',
	array(
		'label'           => esc_html__( 'Title', 'terminal-news' ),
		'section'         => 'terminal_news_banner_section',
		'active_callback' => 'terminal_news_if_banner_enabled',
		'priority'        => 20,
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'terminal_news_banner_main_news_title',
		array(
			'selector'            => '.main-news-outer h3.widget-title',
			'settings'            => 'terminal_news_banner_main_news_title',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
		)
	);
}

// banner main news content type settings.
$wp_customize->add_setting(
	'terminal_news_banner_main_news_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'terminal_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'terminal_news_banner_main_news_content_type',
	array(
		'label'           => esc_html__( 'Main News Content type:', 'terminal-news' ),
		'description'     => esc_html__( 'Choose where you want to render the content from.', 'terminal-news' ),
		'section'         => 'terminal_news_banner_section',
		'settings'        => 'terminal_news_banner_main_news_content_type',
		'type'            => 'select',
		'active_callback' => 'terminal_news_if_banner_enabled',
		'priority'        => 25,
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'terminal-news' ),
			'category' => esc_html__( 'Category', 'terminal-news' ),
		),
	)
);

for ( $i = 1; $i <= 3; $i++ ) {
	// Main News post setting.
	$wp_customize->add_setting(
		'terminal_news_banner_main_news_post_' . $i,
		array(
			'sanitize_callback' => 'terminal_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'terminal_news_banner_main_news_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'terminal-news' ), $i ),
			'section'         => 'terminal_news_banner_section',
			'type'            => 'select',
			'choices'         => terminal_news_get_post_choices(),
			'active_callback' => 'terminal_news_banner_main_news_content_type_post_enabled',
			'priority'        => 30,
		)
	);

}

// Main News category setting.
$wp_customize->add_setting(
	'terminal_news_banner_main_news_category',
	array(
		'sanitize_callback' => 'terminal_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'terminal_news_banner_main_news_category',
	array(
		'label'           => esc_html__( 'Category', 'terminal-news' ),
		'section'         => 'terminal_news_banner_section',
		'type'            => 'select',
		'choices'         => terminal_news_get_post_cat_choices(),
		'active_callback' => 'terminal_news_banner_main_news_content_type_category_enabled',
		'priority'        => 35,
	)
);

// Editor Pick Sub Heading.
$wp_customize->add_setting(
	'terminal_news_banner_editor_pick_sub_heading',
	array(
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	new Terminal_News_Sub_Section_Heading_Custom_Control(
		$wp_customize,
		'terminal_news_banner_editor_pick_sub_heading',
		array(
			'label'           => esc_html__( 'Editor Pick Section', 'terminal-news' ),
			'settings'        => 'terminal_news_banner_editor_pick_sub_heading',
			'section'         => 'terminal_news_banner_section',
			'active_callback' => 'terminal_news_if_banner_enabled',
			'priority'        => 40,
		)
	)
);

// Editor Pick title settings.
$wp_customize->add_setting(
	'terminal_news_banner_editor_pick_title',
	array(
		'default'           => __( 'Editor Pick', 'terminal-news' ),
		'sanitize_callback' => 'sanitize_text_field',
	)
);

$wp_customize->add_control(
	'terminal_news_banner_editor_pick_title',
	array(
		'label'           => esc_html__( 'Title', 'terminal-news' ),
		'section'         => 'terminal_news_banner_section',
		'active_callback' => 'terminal_news_if_banner_enabled',
		'priority'        => 45,
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'terminal_news_banner_editor_pick_title',
		array(
			'selector'            => '.editor-pick-outer h3.widget-title',
			'settings'            => 'terminal_news_banner_editor_pick_title',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
		)
	);
}

// banner main news content type settings.
$wp_customize->add_setting(
	'terminal_news_banner_editor_pick_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'terminal_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'terminal_news_banner_editor_pick_content_type',
	array(
		'label'           => esc_html__( 'Editor Pick Content type:', 'terminal-news' ),
		'description'     => esc_html__( 'Choose where you want to render the content from.', 'terminal-news' ),
		'section'         => 'terminal_news_banner_section',
		'settings'        => 'terminal_news_banner_editor_pick_content_type',
		'type'            => 'select',
		'active_callback' => 'terminal_news_if_banner_enabled',
		'priority'        => 50,
		'choices'         => array(
			'post'     => esc_html__( 'Post', 'terminal-news' ),
			'category' => esc_html__( 'Category', 'terminal-news' ),
		),
	)
);

for ( $i = 1; $i <= 4; $i++ ) {
	// Editor Pick post setting.
	$wp_customize->add_setting(
		'terminal_news_banner_editor_pick_post_' . $i,
		array(
			'sanitize_callback' => 'terminal_news_sanitize_dropdown_pages',
		)
	);

	$wp_customize->add_control(
		'terminal_news_banner_editor_pick_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'terminal-news' ), $i ),
			'section'         => 'terminal_news_banner_section',
			'type'            => 'select',
			'choices'         => terminal_news_get_post_choices(),
			'active_callback' => 'terminal_news_banner_editor_pick_content_type_post_enabled',
			'priority'        => 55,
		)
	);

}

// Editor Pick category setting.
$wp_customize->add_setting(
	'terminal_news_banner_editor_pick_category',
	array(
		'sanitize_callback' => 'terminal_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'terminal_news_banner_editor_pick_category',
	array(
		'label'           => esc_html__( 'Category', 'terminal-news' ),
		'section'         => 'terminal_news_banner_section',
		'type'            => 'select',
		'choices'         => terminal_news_get_post_cat_choices(),
		'active_callback' => 'terminal_news_banner_editor_pick_content_type_category_enabled',
		'priority'        => 60,
	)
);

/*========================Active Callback==============================*/
function terminal_news_if_banner_enabled( $control ) {
	return $control->manager->get_setting( 'terminal_news_banner_section_enable' )->value();
}
// Banner Main News
function terminal_news_banner_main_news_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'terminal_news_banner_main_news_content_type' )->value();
	return terminal_news_if_banner_enabled( $control ) && ( 'post' === $content_type );
}
function terminal_news_banner_main_news_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'terminal_news_banner_main_news_content_type' )->value();
	return terminal_news_if_banner_enabled( $control ) && ( 'category' === $content_type );
}
// Banner Editor Pick
function terminal_news_banner_editor_pick_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'terminal_news_banner_editor_pick_content_type' )->value();
	return terminal_news_if_banner_enabled( $control ) && ( 'post' === $content_type );
}
function terminal_news_banner_editor_pick_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'terminal_news_banner_editor_pick_content_type' )->value();
	return terminal_news_if_banner_enabled( $control ) && ( 'category' === $content_type );
}
