<?php
/**
 * Masterpiece Theme Customizer
 *
 * @package masterpiece
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Add postMessage support for site title and description for the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
if ( ! function_exists( 'masterpiece_customize_register' ) ) {
	/**
	 * Register basic customizer support.
	 *
	 * @param object $wp_customize Customizer reference.
	 */
	function masterpiece_customize_register( $wp_customize ) {
		$wp_customize->get_setting( 'blogname' )->transport         = 'postMessage';
		$wp_customize->get_setting( 'blogdescription' )->transport  = 'postMessage';
		$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
	}
}
add_action( 'customize_register', 'masterpiece_customize_register' );

if ( ! function_exists( 'masterpiece_theme_customize_register' ) ) {
	/**
	 * Register individual settings through customizer's API.
	 *
	 * @param WP_Customize_Manager $wp_customize Customizer reference.
	 */
	function masterpiece_theme_customize_register( $wp_customize ) {

		// Theme layout settings.
		$wp_customize->add_section( 'masterpiece_theme_layout_options', array(
			'title'       => esc_html__( 'Theme Layout Settings', 'masterpiece' ),
			'capability'  => 'edit_theme_options',
			'description' => esc_html__( 'Container width and sidebar defaults', 'masterpiece' ),
			'priority'    => 160,
		) );

		// Header layout section.
		$wp_customize->add_section( 'masterpiece_theme_header', array(
			'title'       => esc_html__( 'Header Settings', 'masterpiece' ),
			'capability'  => 'edit_theme_options',
			'description' => esc_html__( 'Customize your header.', 'masterpiece' ),
			'priority'    => 20,
		) );

		// Blog section.
		$wp_customize->add_section( 'masterpiece_blog_section', array(
			'title'       => esc_html__( 'Blog', 'masterpiece' ),
			'capability'  => 'edit_theme_options',
			'description' => esc_html__( 'Customize your blog.', 'masterpiece' ),
			'priority'    => 21,
		) );

		/**
		 * Select sanitization function
		 *
		 * @param string               $input   Slug to sanitize.
		 * @param WP_Customize_Setting $setting Setting instance.
		 * @return string Sanitized slug if it is a valid choice; otherwise, the setting default.
		 */
        	function masterpiece_theme_slug_sanitize_select( $input, $setting ){

            	// Ensure input is a slug (lowercase alphanumeric characters, dashes and underscores are allowed only).
            	$input = sanitize_key( $input );

           		// Get the list of possible select options.
           		$choices = $setting->manager->get_control( $setting->id )->choices;

            	// If the input is a valid key, return it; otherwise, return the default.
            	return ( array_key_exists( $input, $choices ) ? $input : $setting->default );                

        	}

		$wp_customize->add_setting( 'masterpiece_container_type', array(
			'default'           => 'container',
			'type'              => 'theme_mod',
			'sanitize_callback' => 'masterpiece_theme_slug_sanitize_select',
			'capability'        => 'edit_theme_options',
		) );

		$wp_customize->add_control(
			new WP_Customize_Control(
				$wp_customize,
				'masterpiece_container_type', array(
					'label'       => esc_html__( 'Container Width', 'masterpiece' ),
					'description' => esc_html__( 'Choose between Bootstrap\'s container and container-fluid', 'masterpiece' ),
					'section'     => 'masterpiece_theme_layout_options',
					'settings'    => 'masterpiece_container_type',
					'type'        => 'select',
					'choices'     => array(
						'container-fluid' => esc_html__( 'Full width container', 'masterpiece' ),
						'container'       => esc_html__( 'Fixed width container', 'masterpiece' ),
					),
					'priority'    => '10',
				)
			) );

		$wp_customize->add_setting( 'masterpiece_sidebar_position', array(
			'default'           => 'right',
			'type'              => 'theme_mod',
			'sanitize_callback' => 'sanitize_text_field',
			'capability'        => 'edit_theme_options',
		) );

		$wp_customize->add_control(
			new WP_Customize_Control(
				$wp_customize,
				'masterpiece_sidebar_position', array(
					'label'       => esc_html__( 'Sidebar Positioning', 'masterpiece' ),
					'description' => esc_html__( 'Set sidebar\'s default position. Can either be: right, left, both or none. Note: this can be overridden on individual pages.',
					'masterpiece' ),
					'section'     => 'masterpiece_theme_layout_options',
					'settings'    => 'masterpiece_sidebar_position',
					'type'        => 'select',
					'sanitize_callback' => 'masterpiece_theme_slug_sanitize_select',
					'choices'     => array(
						'right' => esc_html__( 'Right sidebar', 'masterpiece' ),
						'left'  => esc_html__( 'Left sidebar', 'masterpiece' ),
						'both'  => esc_html__( 'Left & Right sidebars', 'masterpiece' ),
						'none'  => esc_html__( 'No sidebar', 'masterpiece' ),
					),
					'priority'    => '20',
				)
			) );

		/** ----- Color Options ----- **/
		// Primary
		$wp_customize->add_setting( 'primary_color', array(
			'default' => '#e84855',
			'type'              => 'theme_mod',
			'sanitize_callback' => 'sanitize_text_field',
			'capability'        => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'primary_color', array(
			'label' => esc_html__('Primary Color', 'masterpiece'),
			'description' => esc_html__('Base color of the website. A bright vibrant color is suggest.', 'masterpiece'),
			'section' => 'colors',
			'settings' => 'primary_color',
		) ) );


		// Secondary Color
		$wp_customize->add_setting( 'secondary_color', array(
			'default' => '#372248',
			'type'              => 'theme_mod',
			'sanitize_callback' => 'sanitize_text_field',
			'capability'        => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'secondary_color', array(
			'label' => esc_html__('Secondary Color', 'masterpiece'),
			'description' => esc_html__('Secondary or auxilary color of the website. Helps to decorate the primary color. A dark and matching with primary color is suggested.', 'masterpiece'),
			'section' => 'colors',
			'settings' => 'secondary_color',
		) ) );


		// Tertiary Color
		$wp_customize->add_setting( 'tertiary_color', array(
			'default' => '#e8d7f1',
			'type'              => 'theme_mod',
			'sanitize_callback' => 'sanitize_text_field',
			'capability'        => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'tertiary_color', array(
			'label' => esc_html__('Tertiary Color', 'masterpiece'),
			'description' => esc_html__('Mostly used for background purpose. A matching with Secondary color and lighter color is suggested.', 'masterpiece'),
			'section' => 'colors',
			'settings' => 'tertiary_color',
		) ) );


		// Logo Text Color
		$wp_customize->add_setting( 'logo_text_color', array(
			'default'              => get_theme_mod('primary_color', '#e84855'),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'logo_text_color', array(
			'label'        => esc_html__('Logo text color', 'masterpiece'),
			'description' => esc_html__('Change color of your logo text.', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'logo_text_color',
		) ) );


		// Link Color
		$wp_customize->add_setting( 'link_color', array(
			'default'              => get_theme_mod('primary_color', '#e84855'),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link_color', array(
			'label'        => esc_html__('Link Color', 'masterpiece'),
			'description' => esc_html__('General link color for your theme', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'link_color',
		) ) );


		// Link Hover Color
		$wp_customize->add_setting( 'link_hover_color', array(
			'default'              => get_theme_mod('secondary_color', '#372248'),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link_hover_color', array(
			'label'        => esc_html__('Link Hover Color', 'masterpiece'),
			'description' => esc_html__('General link hover color for your theme', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'link_hover_color',
		) ) );


		// Menu Link Color
		$wp_customize->add_setting( 'menu_link_color', array(
			'default'              => get_theme_mod('link_color', get_theme_mod('primary_color', '#e84855')),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'menu_link_color', array(
			'label'        => esc_html__('Menu Link Color', 'masterpiece'),
			'description' => esc_html__('Change menu link color.', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'menu_link_color',
		) ) );

		// Menu Link Hover Color
		$wp_customize->add_setting( 'menu_link_hover_color', array(
			'default'              => get_theme_mod('link_hover_color', '#372248'),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'menu_link_hover_color', array(
			'label'        => esc_html__('Menu link hover color', 'masterpiece'),
			'description' => esc_html__('Change menu link color.', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'menu_link_hover_color',
		) ) );
		
		// Sub Menu Link Color
		$wp_customize->add_setting( 'submenu_link_color', array(
			'default'              => get_theme_mod('menu_link_color', get_theme_mod('link_color', get_theme_mod('primary_color', '#e84855'))),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'submenu_link_color', array(
			'label'        => esc_html__('Submenu link color', 'masterpiece'),
			'description' => esc_html__('Change submenu link color.', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'submenu_link_color',
		) ) );

		// Sub Menu Link Hover Color
		$wp_customize->add_setting( 'submenu_link_hover_color', array(
			'default'              => get_theme_mod('menu_link_hover_color', get_theme_mod('link_hover_color', get_theme_mod('secondary_color', '#372248'))),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'submenu_link_hover_color', array(
			'label'        => esc_html__('Submenu link hover color', 'masterpiece'),
			'description' => esc_html__('Change submenu link color.', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'submenu_link_hover_color',
		) ) );
		
		// Sub Menu Background Color
		$wp_customize->add_setting( 'submenu_background_color', array(
			'default'              => '#fff',
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'submenu_background_color', array(
			'label'        => esc_html__('Submenu background color', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'submenu_background_color',
		) ) );

		// Sub Menu Background Hover Color
		$wp_customize->add_setting( 'submenu_background_hover_color', array(
			'default'              => '#fff',
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'submenu_background_hover_color', array(
			'label'        => esc_html__('Submenu background hover color', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'submenu_background_hover_color',
		) ) );

		// Post Title Color
		$wp_customize->add_setting( 'post_title_color', array(
			'default'              => get_theme_mod('primary_color', '#e84855'),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'post_title_color', array(
			'label'        => esc_html__('Post Title Color', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'post_title_color',
		) ) );

		// Post Title Hover Color
		$wp_customize->add_setting( 'post_title_hover_color', array(
			'default'              => get_theme_mod('secondary_color', '#372248'),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'sanitize_text_field',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'post_title_hover_color', array(
			'label'        => esc_html__('Post Title Hover Color', 'masterpiece'),
			'section'      => 'colors',
			'settings'     => 'post_title_hover_color',
		) ) );

		// Header settings 
		$wp_customize->add_setting( 'mp_logo_position', array(
			'default'              => get_theme_mod('logo_position', 'left'),
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'masterpiece_theme_slug_sanitize_select',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'mp_logo_position', array(
			'label'        => esc_html__('Logo position', 'masterpiece'),
			'section'      => 'masterpiece_theme_header',
			'settings'     => 'mp_logo_position',
			'type'        => 'select',
			'choices'     => array(
				'left' => esc_html__( 'Logo Left', 'masterpiece' ),
				'middle' => esc_html__( 'Logo Middle', 'masterpiece' ),
			),
			
		) ) );

		$wp_customize->add_setting( 'mp_show_top_bar',
			array(
				'default' => 0,
				'transport' => 'refresh',
				'sanitize_callback' => 'skyrocket_switch_sanitization'
			)
		);
		$wp_customize->add_control( new Skyrocket_Toggle_Switch_Custom_control( $wp_customize, 'mp_show_top_bar',
			array(
				'label' => esc_html__( 'Show topbar', 'masterpiece' ),
				'section' => 'masterpiece_theme_header'
			)
		) );
		
		// Blog Section masterpiece_blog_section card, list
		$wp_customize->add_setting( 'mp_blog_layout', array(
			'default'              => 'card',
			'type'                 => 'theme_mod',
			'sanitize_callback'  => 'masterpiece_theme_slug_sanitize_select',
			'capability'          => 'edit_theme_options',
		) );

		$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'mp_blog_layout', array(
			'label'        => esc_html__('Blog Layout', 'masterpiece'),
			'section'      => 'masterpiece_blog_section',
			'settings'     => 'mp_blog_layout',
			'type'        => 'select',
			'choices'     => array(
				'card' => esc_html__( 'Card', 'masterpiece' ),
				'list' => esc_html__( 'List', 'masterpiece' ),
			),
			
		) ) );

		$wp_customize->add_setting( 'mp_highlight_first_post',
			array(
				'default' => 0,
				'transport' => 'refresh',
				'sanitize_callback' => 'skyrocket_switch_sanitization'
			)
		);
		$wp_customize->add_control( new Skyrocket_Toggle_Switch_Custom_control( $wp_customize, 'mp_highlight_first_post',
			array(
				'label' => esc_html__( 'Highlight first post', 'masterpiece' ),
				'section' => 'masterpiece_blog_section'
			)
		) );

		$wp_customize->add_setting('mp_post_columns', array(
			'default' => '1',
			'transport' => 'refresh',
			'sanitize_callback' => 'skyrocket_text_sanitization'
		));

		$wp_customize->add_control( new Skyrocket_Text_Radio_Button_Custom_Control( $wp_customize, 'mp_post_columns',
			array(
				'label' => __( 'Post Columns', 'masterpiece' ),
				'description' => esc_html__( 'Number of column for post columns.', 'masterpiece' ),
				'section' => 'masterpiece_blog_section',
				'choices' => array(
					'1' => __( 'One', 'masterpiece' ),
					'2' => __( 'Two', 'masterpiece' ),
					'3' => __( 'Three', 'masterpiece')
				)
			)
		) );

		// Excerpt
		$wp_customize->add_setting( 'mp_excerpt_length',
			array(
				'default' => 55,
				'transport' => 'refresh',
				'sanitize_callback' => 'skyrocket_sanitize_integer'
			)
		);
		$wp_customize->add_control( new Skyrocket_Slider_Custom_Control( $wp_customize, 'mp_excerpt_length',
			array(
				'label' => esc_html__( 'Excerpt Length (word)', 'masterpiece' ),
				'section' => 'masterpiece_blog_section',
				'input_attrs' => array(
					'min' => 10, // Required. Minimum value for the slider
					'max' => 100, // Required. Maximum value for the slider
					'step' => 5, // Required. The size of each interval or step the slider takes between the minimum and maximum values
				),
			)
		) );

	}
} // endif function_exists( 'masterpiece_theme_customize_register' ).
add_action( 'customize_register', 'masterpiece_theme_customize_register' );

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
if ( ! function_exists( 'masterpiece_customize_preview_js' ) ) {
	/**
	 * Setup JS integration for live previewing.
	 */
	function masterpiece_customize_preview_js() {
		wp_enqueue_script( 'masterpiece_customizer', get_template_directory_uri() . '/js/customizer.js',
			array( 'customize-preview' ), '20130508', true
		);
	}
}
add_action( 'customize_preview_init', 'masterpiece_customize_preview_js' );