<?php
if ( ! function_exists( 'kayla_sanitize_checkbox' ) ) {
    /**
     * Checkbox sanitization function
     */
    function kayla_sanitize_checkbox( $checked ) {
    	return ( ( isset($checked) && ! empty($checked) ) ? 1 : 0 );
    }
}

if ( ! function_exists( 'kayla_sanitize_select' ) ) {
    /**
     * Select sanitization function
     */
    function kayla_sanitize_select( $input, $setting ) {
    	// Ensure input is a slug.
    	$input = sanitize_key( $input );
    
    	// Get list of choices from the control associated with the setting.
    	$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 );
    }
}

function kayla_sanitize_default( $value ) {
	return $value;
}

/** Natalie - Customizer - Add Settings */
function kayla_register_theme_customizer( $wp_customize ) {
    /**
     * Pro Theme customizer section.
     *
     * @since  1.0.0
     * @access public
     */
    class Kayla_Section_Pro_Theme extends WP_Customize_Section {
    	/**
    	 * The type of customize section being rendered.
    	 *
    	 * @since  1.0.0
    	 * @access public
    	 * @var    string
    	 */
    	public $type = 'kayla-pro';
    	/**
    	 * Custom button text to output.
    	 *
    	 * @since  1.0.0
    	 * @access public
    	 * @var    string
    	 */
    	public $pro_text = '';
    	/**
    	 * Custom pro section URL.
    	 *
    	 * @since  1.0.0
    	 * @access public
    	 * @var    string
    	 */
    	public $pro_url = '';
    	/**
    	 * Custom section ID.
    	 *
    	 * @since  1.0.0
    	 * @access public
    	 * @var    string
    	 */
    	public $id = '';
    	/**
    	 * Add custom parameters to pass to the JS via JSON.
    	 *
    	 * @since  1.0.0
    	 * @access public
    	 * @return void
    	 */
    	public function json() {
    		$json = parent::json();
    		$json['pro_text'] = $this->pro_text;
    		$json['pro_url']  = $this->pro_url;
    		$json['id'] = $this->id;
    		return $json;
    	}
    	/**
    	 * Outputs the Underscore.js template.
    	 *
    	 * @since  1.0.0
    	 * @access public
    	 * @return void
    	 */
    	protected function render_template() { ?>
    
    		<li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }} cannot-expand">
    			<h3><a href="{{ data.pro_url }}" target="_blank">{{{ data.pro_text }}}</a></h3>
    		</li>
    	<?php }
    }
    $wp_customize->register_section_type( 'Kayla_Section_Pro_Theme' );
    $wp_customize->add_section(
    	new Kayla_Section_Pro_Theme(
    		$wp_customize,
    		'kayla-pro',
    		array(
    			'priority'  => 1,
    			'pro_text' => __( 'Pro Version Available! Take a look', 'kayla' ),
    			'pro_url'  => 'https://az-theme.net/downloads/sarah-premium-wordpress-theme/'
    		)
    	)
    );
	/** Add Sections -----------------------------------------------------------------------------------------------------------*/
    $wp_customize->add_section( 'kayla_section_social_media', array(
        'title' => __('Social Media Links', 'kayla'),
        'priority' => 30,
        'description' => __('Enter your social media URL.', 'kayla'),
    ) );

    $wp_customize->add_section( 'kayla_section_footer', array(
        'title' => __('Site Footer', 'kayla'),
        'priority' => 40
    ) ); 

    /** Accent Color */
    $wp_customize->add_setting( 'kayla_text_color', array( 'default' => '#555', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'kayla_primary_color', array( 'default' => '#0a0a0a', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'kayla_meta_color', array( 'default' => '#999', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'kayla_border_color', array( 'default' => '#eee', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'kayla_accent_color', array( 'default' => '#f37e7e', 'sanitize_callback' => 'sanitize_hex_color' ) );

	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kayla_text_color',
			array(
				'label'      => __( 'Text Color', 'kayla' ),
				'section'    => 'colors'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kayla_primary_color',
			array(
				'label'      => __( 'Primary Color', 'kayla' ),
				'section'    => 'colors'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kayla_meta_color',
			array(
				'label'      => __( 'Meta Color', 'kayla' ),
				'section'    => 'colors'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kayla_border_color',
			array(
				'label'      => __( 'Border Color', 'kayla' ),
				'section'    => 'colors'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kayla_accent_color',
			array(
				'label'      => __( 'Accent Color', 'kayla' ),
				'section'    => 'colors'
			)
		)
	);

    /** Social Links */
    $wp_customize->add_setting( 'kayla_facebook_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'kayla_twitter_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'kayla_instagram_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'kayla_pinterest_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'kayla_youtube_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'kayla_tiktok_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'kayla_linkedin_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'kayla_soundcloud_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'kayla_vimeo_url', array( 'sanitize_callback' => 'esc_url_raw' ) );

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_facebook_url',
			array(
				'label'      => __('Facebook URL', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_twitter_url',
			array(
				'label'      => __('Twitter URL', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_instagram_url',
			array(
				'label'      => __('Instagram URL', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_pinterest_url',
			array(
				'label'      => __('Pinterest URL', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_youtube_url',
			array(
				'label'      => __('Youtube URL', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_tiktok_url',
			array(
				'label'      => __('Tiktok URL', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

    $wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_linkedin_url',
			array(
				'label'      => __('Linkedin (Use full URL to your Linkedin profile)', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_soundcloud_url',
			array(
				'label'      => __('Soundcloud URL', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_vimeo_url',
			array(
				'label'      => __('Vimeo URL', 'kayla'),
				'section'    => 'kayla_section_social_media'
			)
		)
	);

    /**
     * Footer Settings
     */
    $wp_customize->add_setting( 'kayla_footer_copyright', array(
        'default'           => __( 'Your copyright text here...', 'kayla' ),
        'sanitize_callback' => 'wp_kses_post'
    ) );

    $wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'kayla_footer_copyright',
			array(
				'label'      => __('Copyright Text', 'kayla'),
				'section'    => 'kayla_section_footer',
				'type'		 => 'textarea'
			)
		)
	);
}

add_action( 'customize_register', 'kayla_register_theme_customizer' );

function kayla_custom_css()
{
    $custom_css = '';
    if ( get_theme_mod( 'kayla_text_color' ) ) {
        $custom_css .= '
            :root{
                --text-color: ' . esc_attr( get_theme_mod( 'kayla_text_color' ) ) . ';
            }
        ';
    }

    if ( get_theme_mod( 'kayla_primary_color' ) ) {
        $custom_css .= '
            :root{
                --primary-color: ' . esc_attr( get_theme_mod( 'kayla_primary_color' ) ) . ';
            }
        ';
    }
    
    if ( get_theme_mod( 'kayla_meta_color' ) ) {
        $custom_css .= '
            :root{
                --meta-color: ' . esc_attr( get_theme_mod( 'kayla_meta_color' ) ) . ';
            }
        ';
    }
    
    if ( get_theme_mod( 'kayla_border_color' ) ) {
        $custom_css .= '
            :root{
                --border-color: ' . esc_attr( get_theme_mod( 'kayla_border_color' ) ) . ';
            }
        ';
    }

    if ( get_theme_mod( 'kayla_accent_color' ) ) {
        $custom_css .= '
            :root{
                --accent-color: ' . esc_attr( get_theme_mod( 'kayla_accent_color' ) ) . ';
            }
        ';
    }

    wp_add_inline_style( 'kayla-theme', $custom_css );
}
add_action( 'wp_enqueue_scripts', 'kayla_custom_css', 15 );
