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

if ( ! function_exists( 'sarahlist_sanitize_select' ) ) {
    /**
     * Select sanitization function
     */
    function sarahlist_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 sarahlist_sanitize_default( $value ) {
	return $value;
}

/** Natalie - Customizer - Add Settings */
function sarahlist_register_theme_customizer( $wp_customize ) {
    /**
     * Pro Theme customizer section.
     *
     * @since  1.0.0
     * @access public
     */
    class Sarahlist_Section_Pro_Theme extends WP_Customize_Section {
    	/**
    	 * The type of customize section being rendered.
    	 *
    	 * @since  1.0.0
    	 * @access public
    	 * @var    string
    	 */
    	public $type = 'sarahlist-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( 'Sarahlist_Section_Pro_Theme' );
    $wp_customize->add_section(
    	new Sarahlist_Section_Pro_Theme(
    		$wp_customize,
    		'sarahlist-pro',
    		array(
    			'priority'  => 1,
    			'pro_text' => __( 'Pro Version Available! Take a look', 'sarahlist' ),
    			'pro_url'  => 'https://az-theme.net/downloads/sarah-premium-wordpress-theme/'
    		)
    	)
    );

    /** Add Sections -----------------------------------------------------------------------------------------------------------*/
    $wp_customize->add_section( 'sarahlist_section_social_media', array(
        'title' => __('Social Media Links', 'sarahlist'),
        'priority' => 30,
        'description' => __('Enter your social media URL.', 'sarahlist'),
    ) );

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

    /** Blog settings ------------------------------------------------------------------------------------------------- */
    $wp_customize->add_setting( 'sarahlist_blog_hide_sidebar', array(
        'default' => false,
        'sanitize_callback' => 'sarahlist_sanitize_checkbox'
    ) );

    $wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_blog_hide_sidebar',
			array(
				'label'      => __('Hide Sidebar?', 'sarahlist'),
				'section'    => 'sarahlist_new_section_blog_settings',
				'type'		 => 'checkbox'
			)
		)
	);

    /** Accent Color */
    $wp_customize->add_setting( 'sarahlist_text_color', array( 'default' => '#555', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'sarahlist_primary_color', array( 'default' => '#0a0a0a', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'sarahlist_meta_color', array( 'default' => '#999', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'sarahlist_border_color', array( 'default' => '#eee', 'sanitize_callback' => 'sanitize_hex_color' ) );
    $wp_customize->add_setting( 'sarahlist_accent_color', array( 'default' => '#f37e7e', 'sanitize_callback' => 'sanitize_hex_color' ) );

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

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

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

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

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

    /** Social Links */
    $wp_customize->add_setting( 'sarahlist_facebook_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'sarahlist_twitter_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'sarahlist_instagram_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'sarahlist_pinterest_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'sarahlist_youtube_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'sarahlist_tiktok_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'sarahlist_linkedin_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'sarahlist_soundcloud_url', array( 'sanitize_callback' => 'esc_url_raw' ) );
    $wp_customize->add_setting( 'sarahlist_vimeo_url', array( 'sanitize_callback' => 'esc_url_raw' ) );

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_facebook_url',
			array(
				'label'      => __('Facebook URL', 'sarahlist'),
				'section'    => 'sarahlist_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_twitter_url',
			array(
				'label'      => __('Twitter URL', 'sarahlist'),
				'section'    => 'sarahlist_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_instagram_url',
			array(
				'label'      => __('Instagram URL', 'sarahlist'),
				'section'    => 'sarahlist_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_pinterest_url',
			array(
				'label'      => __('Pinterest URL', 'sarahlist'),
				'section'    => 'sarahlist_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_youtube_url',
			array(
				'label'      => __('Youtube URL', 'sarahlist'),
				'section'    => 'sarahlist_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_tiktok_url',
			array(
				'label'      => __('Tiktok URL', 'sarahlist'),
				'section'    => 'sarahlist_section_social_media'
			)
		)
	);

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

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_soundcloud_url',
			array(
				'label'      => __('Soundcloud URL', 'sarahlist'),
				'section'    => 'sarahlist_section_social_media'
			)
		)
	);

	$wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_vimeo_url',
			array(
				'label'      => __('Vimeo URL', 'sarahlist'),
				'section'    => 'sarahlist_section_social_media'
			)
		)
	);

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

    $wp_customize->add_control(
		new WP_Customize_Control(
			$wp_customize,
			'sarahlist_footer_copyright',
			array(
				'label'      => __('Copyright Text', 'sarahlist'),
				'section'    => 'sarahlist_section_footer',
				'type'		 => 'textarea'
			)
		)
	);
}

add_action( 'customize_register', 'sarahlist_register_theme_customizer' );

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

    if ( get_theme_mod( 'sarahlist_primary_color' ) ) {
        $custom_css .= '
            :root{
                --primary-color: ' . esc_attr( get_theme_mod( 'sarahlist_primary_color' ) ) . ';
            }
        ';
    }
    
    if ( get_theme_mod( 'sarahlist_meta_color' ) ) {
        $custom_css .= '
            :root{
                --meta-color: ' . esc_attr( get_theme_mod( 'sarahlist_meta_color' ) ) . ';
            }
        ';
    }
    
    if ( get_theme_mod( 'sarahlist_border_color' ) ) {
        $custom_css .= '
            :root{
                --border-color: ' . esc_attr( get_theme_mod( 'sarahlist_border_color' ) ) . ';
            }
        ';
    }

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

    wp_add_inline_style( 'sarahlist-theme', $custom_css );
}
add_action( 'wp_enqueue_scripts', 'sarahlist_custom_css', 15 );
