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

$wp_customize->add_section(
	'terminal_news_breaking_news_section',
	array(
		'title'    => esc_html__( 'Breaking News Section', 'terminal-news' ),
		'panel'    => 'terminal_news_frontpage_panel',
		'priority' => 10,
	)
);

// Breaking News section enable settings.
$wp_customize->add_setting(
	'terminal_news_breaking_news_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_breaking_news_section_enable',
		array(
			'label'    => esc_html__( 'Enable Breaking News Section', 'terminal-news' ),
			'type'     => 'checkbox',
			'settings' => 'terminal_news_breaking_news_section_enable',
			'section'  => 'terminal_news_breaking_news_section',
		)
	)
);

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

$wp_customize->add_control(
	'terminal_news_breaking_news_title',
	array(
		'label'           => esc_html__( 'Title', 'terminal-news' ),
		'section'         => 'terminal_news_breaking_news_section',
		'active_callback' => 'terminal_news_if_breaking_news_enabled',
	)
);

// Abort if selective refresh is not available.
if ( isset( $wp_customize->selective_refresh ) ) {
	$wp_customize->selective_refresh->add_partial(
		'terminal_news_breaking_news_title',
		array(
			'selector'            => '.breaking-news-section h3.breaking-news-label',
			'settings'            => 'terminal_news_breaking_news_title',
			'container_inclusive' => false,
			'fallback_refresh'    => true,
			'render_callback'     => 'terminal_news_breaking_news_title_text_partial',
		)
	);
}

// Flash News Section - Speed Controller.
$wp_customize->add_setting(
	'terminal_news_breaking_news_speed_controller',
	array(
		'default'           => 200,
		'sanitize_callback' => 'terminal_news_sanitize_number_range',
	)
);

$wp_customize->add_control(
	'terminal_news_breaking_news_speed_controller',
	array(
		'label'           => esc_html__( 'Speed Controller', 'terminal-news' ),
		'description'     => esc_html__( 'Note: Default speed value is 200.', 'terminal-news' ),
		'section'         => 'terminal_news_breaking_news_section',
		'settings'        => 'terminal_news_breaking_news_speed_controller',
		'type'            => 'number',
		'input_attrs'     => array(
			'min' => 1,
		),
		'active_callback' => 'terminal_news_if_breaking_news_enabled',
	)
);

// breaking_news content type settings.
$wp_customize->add_setting(
	'terminal_news_breaking_news_content_type',
	array(
		'default'           => 'post',
		'sanitize_callback' => 'terminal_news_sanitize_select',
	)
);

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

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

	$wp_customize->add_control(
		'terminal_news_breaking_news_post_' . $i,
		array(
			'label'           => sprintf( esc_html__( 'Post %d', 'terminal-news' ), $i ),
			'section'         => 'terminal_news_breaking_news_section',
			'type'            => 'select',
			'choices'         => terminal_news_get_post_choices(),
			'active_callback' => 'terminal_news_breaking_news_section_content_type_post_enabled',
		)
	);

}

// breaking_news category setting.
$wp_customize->add_setting(
	'terminal_news_breaking_news_category',
	array(
		'sanitize_callback' => 'terminal_news_sanitize_select',
	)
);

$wp_customize->add_control(
	'terminal_news_breaking_news_category',
	array(
		'label'           => esc_html__( 'Category', 'terminal-news' ),
		'section'         => 'terminal_news_breaking_news_section',
		'type'            => 'select',
		'choices'         => terminal_news_get_post_cat_choices(),
		'active_callback' => 'terminal_news_breaking_news_section_content_type_category_enabled',
	)
);

/*========================Active Callback==============================*/
function terminal_news_if_breaking_news_enabled( $control ) {
	return $control->manager->get_setting( 'terminal_news_breaking_news_section_enable' )->value();
}
function terminal_news_breaking_news_section_content_type_post_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'terminal_news_breaking_news_content_type' )->value();
	return terminal_news_if_breaking_news_enabled( $control ) && ( 'post' === $content_type );
}
function terminal_news_breaking_news_section_content_type_category_enabled( $control ) {
	$content_type = $control->manager->get_setting( 'terminal_news_breaking_news_content_type' )->value();
	return terminal_news_if_breaking_news_enabled( $control ) && ( 'category' === $content_type );
}

/*========================Partial Refresh==============================*/
if ( ! function_exists( 'terminal_news_breaking_news_title_text_partial' ) ) :
	// Title.
	function terminal_news_breaking_news_title_text_partial() {
		return esc_html( get_theme_mod( 'terminal_news_breaking_news_title' ) );
	}
endif;
