<?php
/**
 * Wellness Coach Lite Customizer Typography Control
 *
 * @package Wellness_Coach_Lite
 */

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

if( ! class_exists( 'Wellness_Coach_Lite_Typography_Control' ) ) {
    
    class Wellness_Coach_Lite_Typography_Control extends WP_Customize_Control {
    
    	public $tooltip = '';
    	public $js_vars = array();
    	public $output = array();
    	public $option_type = 'theme_mod';
    	public $type = 'wellness-coach-lite-typography';
    
    	/**
    	 * Refresh the parameters passed to the JavaScript via JSON.
    	 *
    	 * @access public
    	 * @return void
    	 */
    	public function to_json() {
    		parent::to_json();
    
    		if ( isset( $this->default ) ) {
    			$this->json['default'] = $this->default;
    		} else {
    			$this->json['default'] = $this->setting->default;
    		}
    		$this->json['js_vars'] = $this->js_vars;
    		$this->json['output']  = $this->output;
    		$this->json['value']   = $this->value();
    		$this->json['choices'] = $this->choices;
    		$this->json['link']    = $this->get_link();
    		$this->json['tooltip'] = $this->tooltip;
    		$this->json['id']      = $this->id;
    		$this->json['l10n']    = apply_filters( 'wellness_coach_lite_il8n_strings', array(
    			'on'                 => esc_attr__( 'ON', 'wellness-coach-lite' ),
    			'off'                => esc_attr__( 'OFF', 'wellness-coach-lite' ),
    			'all'                => esc_attr__( 'All', 'wellness-coach-lite' ),
    			'cyrillic'           => esc_attr__( 'Cyrillic', 'wellness-coach-lite' ),
    			'cyrillic-ext'       => esc_attr__( 'Cyrillic Extended', 'wellness-coach-lite' ),
    			'devanagari'         => esc_attr__( 'Devanagari', 'wellness-coach-lite' ),
    			'greek'              => esc_attr__( 'Greek', 'wellness-coach-lite' ),
    			'greek-ext'          => esc_attr__( 'Greek Extended', 'wellness-coach-lite' ),
    			'khmer'              => esc_attr__( 'Khmer', 'wellness-coach-lite' ),
    			'latin'              => esc_attr__( 'Latin', 'wellness-coach-lite' ),
    			'latin-ext'          => esc_attr__( 'Latin Extended', 'wellness-coach-lite' ),
    			'vietnamese'         => esc_attr__( 'Vietnamese', 'wellness-coach-lite' ),
    			'hebrew'             => esc_attr__( 'Hebrew', 'wellness-coach-lite' ),
    			'arabic'             => esc_attr__( 'Arabic', 'wellness-coach-lite' ),
    			'bengali'            => esc_attr__( 'Bengali', 'wellness-coach-lite' ),
    			'gujarati'           => esc_attr__( 'Gujarati', 'wellness-coach-lite' ),
    			'tamil'              => esc_attr__( 'Tamil', 'wellness-coach-lite' ),
    			'telugu'             => esc_attr__( 'Telugu', 'wellness-coach-lite' ),
    			'thai'               => esc_attr__( 'Thai', 'wellness-coach-lite' ),
    			'serif'              => _x( 'Serif', 'font style', 'wellness-coach-lite' ),
    			'sans-serif'         => _x( 'Sans Serif', 'font style', 'wellness-coach-lite' ),
    			'monospace'          => _x( 'Monospace', 'font style', 'wellness-coach-lite' ),
    			'font-family'        => esc_attr__( 'Font Family', 'wellness-coach-lite' ),
    			'font-size'          => esc_attr__( 'Font Size', 'wellness-coach-lite' ),
    			'font-weight'        => esc_attr__( 'Font Weight', 'wellness-coach-lite' ),
    			'line-height'        => esc_attr__( 'Line Height', 'wellness-coach-lite' ),
    			'font-style'         => esc_attr__( 'Font Style', 'wellness-coach-lite' ),
    			'letter-spacing'     => esc_attr__( 'Letter Spacing', 'wellness-coach-lite' ),
    			'text-align'         => esc_attr__( 'Text Align', 'wellness-coach-lite' ),
    			'text-transform'     => esc_attr__( 'Text Transform', 'wellness-coach-lite' ),
    			'none'               => esc_attr__( 'None', 'wellness-coach-lite' ),
    			'uppercase'          => esc_attr__( 'Uppercase', 'wellness-coach-lite' ),
    			'lowercase'          => esc_attr__( 'Lowercase', 'wellness-coach-lite' ),
    			'top'                => esc_attr__( 'Top', 'wellness-coach-lite' ),
    			'bottom'             => esc_attr__( 'Bottom', 'wellness-coach-lite' ),
    			'left'               => esc_attr__( 'Left', 'wellness-coach-lite' ),
    			'right'              => esc_attr__( 'Right', 'wellness-coach-lite' ),
    			'center'             => esc_attr__( 'Center', 'wellness-coach-lite' ),
    			'justify'            => esc_attr__( 'Justify', 'wellness-coach-lite' ),
    			'color'              => esc_attr__( 'Color', 'wellness-coach-lite' ),
    			'select-font-family' => esc_attr__( 'Select a font-family', 'wellness-coach-lite' ),
    			'variant'            => esc_attr__( 'Variant', 'wellness-coach-lite' ),
    			'style'              => esc_attr__( 'Style', 'wellness-coach-lite' ),
    			'size'               => esc_attr__( 'Size', 'wellness-coach-lite' ),
    			'height'             => esc_attr__( 'Height', 'wellness-coach-lite' ),
    			'spacing'            => esc_attr__( 'Spacing', 'wellness-coach-lite' ),
    			'ultra-light'        => esc_attr__( 'Ultra-Light 100', 'wellness-coach-lite' ),
    			'ultra-light-italic' => esc_attr__( 'Ultra-Light 100 Italic', 'wellness-coach-lite' ),
    			'light'              => esc_attr__( 'Light 200', 'wellness-coach-lite' ),
    			'light-italic'       => esc_attr__( 'Light 200 Italic', 'wellness-coach-lite' ),
    			'book'               => esc_attr__( 'Book 300', 'wellness-coach-lite' ),
    			'book-italic'        => esc_attr__( 'Book 300 Italic', 'wellness-coach-lite' ),
    			'regular'            => esc_attr__( 'Normal 400', 'wellness-coach-lite' ),
    			'italic'             => esc_attr__( 'Normal 400 Italic', 'wellness-coach-lite' ),
    			'medium'             => esc_attr__( 'Medium 500', 'wellness-coach-lite' ),
    			'medium-italic'      => esc_attr__( 'Medium 500 Italic', 'wellness-coach-lite' ),
    			'semi-bold'          => esc_attr__( 'Semi-Bold 600', 'wellness-coach-lite' ),
    			'semi-bold-italic'   => esc_attr__( 'Semi-Bold 600 Italic', 'wellness-coach-lite' ),
    			'bold'               => esc_attr__( 'Bold 700', 'wellness-coach-lite' ),
    			'bold-italic'        => esc_attr__( 'Bold 700 Italic', 'wellness-coach-lite' ),
    			'extra-bold'         => esc_attr__( 'Extra-Bold 800', 'wellness-coach-lite' ),
    			'extra-bold-italic'  => esc_attr__( 'Extra-Bold 800 Italic', 'wellness-coach-lite' ),
    			'ultra-bold'         => esc_attr__( 'Ultra-Bold 900', 'wellness-coach-lite' ),
    			'ultra-bold-italic'  => esc_attr__( 'Ultra-Bold 900 Italic', 'wellness-coach-lite' ),
    			'invalid-value'      => esc_attr__( 'Invalid Value', 'wellness-coach-lite' ),
    		) );
    
    		$defaults = array( 'font-family'=> false );
    
    		$this->json['default'] = wp_parse_args( $this->json['default'], $defaults );
    	}
    
    	/**
    	 * Enqueue scripts and styles.
    	 *
    	 * @access public
    	 * @return void
    	 */
    	public function enqueue() {
    		wp_enqueue_style( 'wellness-coach-lite-typography', get_template_directory_uri() . '/inc/custom-controls/typography/typography.css', null );
            
            wp_enqueue_script( 'jquery-ui-core' );
    		wp_enqueue_script( 'jquery-ui-tooltip' );
    		wp_enqueue_script( 'jquery-stepper-min-js' );
    		wp_enqueue_script( 'wellness-coach-lite-selectize', get_template_directory_uri() . '/inc/js/selectize.js', array( 'jquery' ), false, true );
    		wp_enqueue_script( 'wellness-coach-lite-typography', get_template_directory_uri() . '/inc/custom-controls/typography/typography.js', array( 'jquery', 'wellness-coach-lite-selectize' ), false, true );
    
    		$google_fonts   = Wellness_Coach_Lite_Fonts::get_google_fonts();
    		$standard_fonts = Wellness_Coach_Lite_Fonts::get_standard_fonts();
    		$all_variants   = Wellness_Coach_Lite_Fonts::get_all_variants();
    
    		$standard_fonts_final = array();
    		foreach ( $standard_fonts as $key => $value ) {
    			$standard_fonts_final[] = array(
    				'family'      => $value['stack'],
    				'label'       => $value['label'],
    				'is_standard' => true,
    				'variants'    => array(
    					array(
    						'id'    => 'regular',
    						'label' => $all_variants['regular'],
    					),
    					array(
    						'id'    => 'italic',
    						'label' => $all_variants['italic'],
    					),
    					array(
    						'id'    => '700',
    						'label' => $all_variants['700'],
    					),
    					array(
    						'id'    => '700italic',
    						'label' => $all_variants['700italic'],
    					),
    				),
    			);
    		}
    
    		$google_fonts_final = array();
    
    		if ( is_array( $google_fonts ) ) {
    			foreach ( $google_fonts as $family => $args ) {
    				$label    = ( isset( $args['label'] ) ) ? $args['label'] : $family;
    				$variants = ( isset( $args['variants'] ) ) ? $args['variants'] : array( 'regular', '700' );
    
    				$available_variants = array();
    				foreach ( $variants as $variant ) {
    					if ( array_key_exists( $variant, $all_variants ) ) {
    						$available_variants[] = array( 'id' => $variant, 'label' => $all_variants[ $variant ] );
    					}
    				}
    
    				$google_fonts_final[] = array(
    					'family'   => $family,
    					'label'    => $label,
    					'variants' => $available_variants
    				);
    			}
    		}
    
    		$final = array_merge( $standard_fonts_final, $google_fonts_final );
    		wp_localize_script( 'wellness-coach-lite-typography', 'all_fonts', $final );
    	}
    
    	/**
    	 * An Underscore (JS) template for this control's content (but not its container).
    	 *
    	 * Class variables for this control class are available in the `data` JS object;
    	 * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
    	 *
    	 * I put this in a separate file because PhpStorm didn't like it and it fucked with my formatting.
    	 *
    	 * @see    WP_Customize_Control::print_template()
    	 *
    	 * @access protected
    	 * @return void
    	 */
    	protected function content_template(){ ?>
    		<# if ( data.tooltip ) { #>
                <a href="#" class="tooltip hint--left" data-hint="{{ data.tooltip }}"><span class='dashicons dashicons-info'></span></a>
            <# } #>
            
            <label class="customizer-text">
                <# if ( data.label ) { #>
                    <span class="customize-control-title">{{{ data.label }}}</span>
                <# } #>
                <# if ( data.description ) { #>
                    <span class="description customize-control-description">{{{ data.description }}}</span>
                <# } #>
            </label>
            
            <div class="wrapper">
                <# if ( data.default['font-family'] ) { #>
                    <# if ( '' == data.value['font-family'] ) { data.value['font-family'] = data.default['font-family']; } #>
                    <# if ( data.choices['fonts'] ) { data.fonts = data.choices['fonts']; } #>
                    <div class="font-family">
                        <h5>{{ data.l10n['font-family'] }}</h5>
                        <select id="wellness-coach-lite-typography-font-family-{{{ data.id }}}" placeholder="{{ data.l10n['select-font-family'] }}"></select>
                    </div>
                    <div class="variant wellness-coach-lite-variant-wrapper">
                        <h5>{{ data.l10n['style'] }}</h5>
                        <select class="variant" id="wellness-coach-lite-typography-variant-{{{ data.id }}}"></select>
                    </div>
                <# } #>   
                
            </div>
            <?php
		}
		
		protected function render_content(){
			
		}
    }
}