<?php
/** Theme Customizer Functions */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/*========================== CUSTOMIZER CONTROLS FUNCTIONS ==========================*/
// Add simple heading option to the theme customizer
if ( class_exists( 'WP_Customize_Control' ) ) :
    class Pmthemes_Customize_Header_Control extends WP_Customize_Control {
        public function render_content() {  ?>
			<label>
				<span class="customize-control-title"><?php echo wp_kses_post( $this->label ); ?></span>
			</label>
            <?php if ( ! empty( $this->description ) ) : ?>
            <span class="description customize-control-description"><?php echo $this->description; ?></span>
            <?php endif; ?>
<?php
        }
    }
	class Pmthemes_Customize_Description_Control extends WP_Customize_Control {
        public function render_content() {  ?>
			<span class="description"><?php echo wp_kses_post( $this->label ); ?></span>	
<?php
        }
    }
	class Pmthemes_Customize_Text_Control extends WP_Customize_Control {
        public function render_content() {  ?>
			<span class="textfield"><?php echo esc_html( $this->label ); ?></span>
<?php
        }
    }
	class Pmthemes_Customize_Font_List_Control extends WP_Customize_Control {
		// critical for JS constructor
		public $type = 'pmthemes_custom_font_list';
		public $l10n = array();
		public function __construct($manager, $id, $args = array(), $options = array()) {
			// Make Buttons translateable
			$this->l10n = array(
				'update' =>	__( 'Update Fonts', 'pmthemes' )
			);
			parent::__construct( $manager, $id, $args );	
		}
		public function enqueue() {
			// Register and Enqueue Custom Font JS Constructor
			wp_enqueue_script( 'pmthemes-custom-font-control', get_template_directory_uri() . '/inc/customizer/assets/js/custom-font-control.js', array( 'customize-controls' ), '20150422', true );
		}
		public function render_content() {
			if ( empty( $this->choices ) )
				return;
			// Create Data Attributes with font lists
			$default = json_encode(Pmthemes_Custom_Font_Lists::get_fonts('default'));
			$favorite = json_encode(Pmthemes_Custom_Font_Lists::get_fonts('favorite'));
			$popular = json_encode(Pmthemes_Custom_Font_Lists::get_fonts('popular'));
			$all = json_encode(Pmthemes_Custom_Font_Lists::get_fonts('all'));
			$l10n = json_encode( $this->l10n );
			?>
			<label>
				<?php if ( ! empty( $this->label ) ) : ?>
					<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
				<?php endif;
				if ( ! empty( $this->description ) ) : ?>
					<span class="description customize-control-description"><?php echo $this->description; ?></span>
				<?php endif; ?>

				<select <?php $this->link(); ?>>
					<?php
					foreach ( $this->choices as $value => $label )
						echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
					?>
				</select>
				<div class="actions"></div>
				<div class="custom-font-lists" data-l10n="<?php echo esc_attr( $l10n ); ?>" data-standard="<?php echo esc_attr( $default ); ?>" data-favorite="<?php echo esc_attr( $favorite ); ?>" data-popular="<?php echo esc_attr( $popular ); ?>" data-all="<?php echo esc_attr( $all ); ?>"></div>
				<div class="debugging"></div>
			</label>
			
		<?php
		}
	}
	class Pmthemes_Customize_Font_Control extends WP_Customize_Control {
		private $fonts = false;
		public $l10n = array();
		// critical for JS constructor
		public $type = 'pmthemes_custom_font';
		public function __construct($manager, $id, $args = array(), $options = array()) {
			// Make Buttons translateable
			$this->l10n = array(
				'previous' =>	__( 'Previous Font', 'pmthemes' ),
				'next' =>		__( 'Next Font', 'pmthemes' ),
				'standard' =>	__( 'Default', 'pmthemes' )
			);		
			// Get Theme Options
			$theme_options = pmthemes_theme_options();
			// Set Fonts
			$this->fonts = Pmthemes_Custom_Font_Lists::get_fonts($theme_options['available_fonts']);
			parent::__construct( $manager, $id, $args );
		}
		public function enqueue() {
			// Register and Enqueue Custom Font JS Constructor
			wp_enqueue_script( 'pmthemes-custom-font-control', get_template_directory_uri() . '/inc/customizer/assets/js/custom-font-control.js', array( 'customize-controls' ), '20150422', true );
		}
		public function render_content() {
			$l10n = json_encode( $this->l10n );
			if( !empty($this->fonts) ) :
            ?>
                <label>
                    <span class="customize-control-title" data-l10n="<?php echo esc_attr( $l10n ); ?>" data-font="<?php echo esc_attr( $this->setting->default ); ?>">
						<?php echo esc_html( $this->label ); ?>
					</span>
					<div class="customize-font-select-control">
						<select <?php $this->link(); ?>>
							<?php
								foreach ( $this->fonts as $k => $v )
								{
									printf('<option value="%s" %s>%s</option>', $k, selected($this->value(), $k, false), $v);
								}
							?>
						</select>
					</div>
					<div class="actions"></div>
				</label>
                
            <?php
			endif;
		}
	}
    // Slider
    class WP_Customize_Range extends WP_Customize_Control {
        public $type = 'pmthemes-range';
        public function __construct( $manager, $id, $args = array() ) {
            parent::__construct( $manager, $id, $args );
            $defaults = array(
                'min' => 0,
                'max' => 100,
                'step' => 1
            );
            $args = wp_parse_args( $args, $defaults );
            $this->min = $args['min'];
            $this->max = $args['max'];
            $this->step = $args['step'];
            $this->unit = $args['unit'];
        }
        public function enqueue() {
            wp_enqueue_script( 'pmthemes-range', PMTHEMES_URI . '/inc/customizer/assets/js/ranger.js', array( 'jquery', 'customize-base', 'jquery-ui-slider' ), false, true );
            wp_enqueue_style( 'pmthemes-range', PMTHEMES_URI . '/inc/customizer/assets/css/ranger.css', null );
        }

        public function render_content() {
            ?>
            <label>
                <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
                <?php if ( ! empty( $this->description ) ) : ?>
                    <span class="description customize-control-description"><?php echo $this->description; ?></span>
                <?php endif; ?>
                <span class="pmthemes-reset-slider"><span class="dashicons dashicons-image-rotate"></span></span>
                <div class="control-wrap">
                    <input type='range' min="<?php echo $this->min ?>" max="<?php echo $this->max ?>" step="<?php echo $this->step ?>" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> data-reset_value="<?php echo esc_attr( $this->settings['default']->default ); ?>" oninput="jQuery(this).next('input').val( jQuery(this).val() )">
                    <input onKeyUp="jQuery(this).prev('input').val( jQuery(this).val() )" type='number' min="<?php echo $this->min ?>" max="<?php echo $this->max ?>" step="<?php echo $this->step ?>" class="pmthemes-range-input"  value='<?php echo esc_attr( $this->value() ); ?>'><span class="unit"><?php echo $this->unit ?></span>
                </div>
            </label>
            <?php
        }

    }

    // Toggle
    class Pmthemes_Control_Toggle extends WP_Customize_Control {
        public $type = 'control_section_toggle';
        public $targets = '';

        public function enqueue() {
            wp_enqueue_script( 'pmthemes-control-target', PMTHEMES_URI . '/inc/customizer/assets/js/toggle.js', array( 'jquery', 'customize-base' ), false, true );
        }

        public function to_json() {
            parent::to_json();

            $this->json[ 'targets' ] = $this->targets;

        }
        public function content_template() {
            ?>
            <div class="pmthemes-control-toggles">
                <# jQuery.each( data.targets, function( index, value ) { #>
                <button data-target="{{ index }}">{{ value }}</button>
                <# } ); #>
            </div>
            <?php
        }
    }

    // Spacing
    class Pmthemes_Spacing_Control extends WP_Customize_Control {

        public $type = 'pmthemes-spacing';

        public $l10n = array();

        public $element = '';

        public function __construct( $manager, $id, $args = array() ) {
            // Let the parent class do its thing.
            parent::__construct( $manager, $id, $args );
            // Make sure we have labels.
            $this->l10n = wp_parse_args(
                $this->l10n,
                array(
                    'top'            => esc_html__( 'Top', 'pmthemes' ),
                    'right'          => esc_html__( 'Right',   'pmthemes' ),
                    'bottom'         => esc_html__( 'Bottom', 'pmthemes' ),
                    'left'           => esc_html__( 'Left',  'pmthemes' )
                )
            );
        }

        public function enqueue() {
            wp_enqueue_script( 'pmthemes-spacing-customizer', PMTHEMES_URI . '/inc/customizer/assets/js/spacing.js', array( 'jquery', 'customize-controls' ), false, true );
        }

        public function to_json() {
            parent::to_json();
            // Loop through each of the settings and set up the data for it.
            foreach ( $this->settings as $setting_key => $setting_id ) {
                $this->json[ $setting_key ] = array(
                    'link'  => $this->get_link( $setting_key ),
                    'value' => $this->value( $setting_key ),
                    'label' => isset( $this->l10n[ $setting_key ] ) ? $this->l10n[ $setting_key ] : ''
                );
            }

            $this->json[ 'element' ] = $this->element;
            $this->json[ 'title' ] = __( 'Link values','pmthemes' );
            $this->json[ 'unlink_title' ] = __( 'Un-link values','pmthemes' );
        }

        public function content_template() {
            ?>
            <# if ( data.label ) { #>
            <label for="{{{ data.element }}}-{{{ data.top.label }}}">
                <span class="customize-control-title">{{ data.label }}</span>
            </label>
            <# } #>

            <# if ( data.description ) { #>
            <span class="description customize-control-description">{{{ data.description }}}</span>
            <# } #>

            <div class="pmniches-spacing-section">
                <input id="{{{ data.element }}}-{{{ data.top.label }}}" min="0" class="pmthemes-number-control spacing-top" type="number" style="text-align: center;" {{{ data.top.link }}} value="{{{ data.top.value }}}" />
                <# if ( data.top.label ) { #>
                <span class="description" style="font-style:normal;">{{ data.top.label }}</span>
                <# } #>
            </div>

            <div class="pmniches-spacing-section">
                <input min="0" class="pmthemes-number-control spacing-right" type="number" style="text-align: center;" {{{ data.right.link }}} value="{{{ data.right.value }}}" />
                <# if ( data.right.label ) { #>
                <span class="description" style="font-style:normal;">{{ data.right.label }}</span>
                <# } #>
            </div>

            <div class="pmniches-spacing-section">
                <input min="0" class="pmthemes-number-control spacing-bottom" type="number" style="text-align: center;" {{{ data.bottom.link }}} value="{{{ data.bottom.value }}}" />
                <# if ( data.bottom.label ) { #>
                <span class="description" style="font-style:normal;">{{ data.bottom.label }}</span>
                <# } #>
            </div>

            <div class="pmniches-spacing-section">
                <input min="0" class="pmthemes-number-control spacing-left" type="number" style="text-align: center;" {{{ data.left.link }}} value="{{{ data.left.value }}}" />
                <# if ( data.left.label ) { #>
                <span class="description" style="font-style:normal;">{{ data.left.label }}</span>
                <# } #>
            </div>

            <# if ( data.element ) { #>
            <div class="pmniches-spacing-section pmniches-link-spacing-section">
                <span class="dashicons dashicons-editor-unlink pmniches-link-spacing" data-element="{{ data.element }}" title="{{ data.title }}"></span>
                <span class="dashicons dashicons-admin-links pmniches-unlink-spacing" style="display:none" data-element="{{ data.element }}" title="{{ data.unlink_title }}"></span>
            </div>
            <# } #>
            <?php
        }
    }
    // Taxonomies - for ticker bar
    class Pmthemes_Customize_Taxonomies_Control extends WP_Customize_Control {

        public $type = 'pmthemes-taxonomies';

        public $taxonomy = '';


        public function __construct( $manager, $id, $args = array() ) {

            $our_taxonomy = 'category';
            if ( isset( $args['taxonomy'] ) ) {
                $taxonomy_exist = taxonomy_exists( esc_attr( $args['taxonomy'] ) );
                if ( true === $taxonomy_exist ) {
                    $our_taxonomy = esc_attr( $args['taxonomy'] );
                }
            }
            $args['taxonomy'] = $our_taxonomy;
            $this->taxonomy = esc_attr( $our_taxonomy );

            parent::__construct( $manager, $id, $args );
        }

        public function render_content() {

            $tax_args = array(
                'hierarchical' => 0,
                'taxonomy'     => $this->taxonomy,
            );
            $all_taxonomies = get_categories( $tax_args );

            ?>
            <label>
                <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
                <select <?php echo esc_attr( $this->link() ); ?>>
                    <?php
                    printf('<option value="%s" %s>%s</option>', '', selected($this->value(), '', false),esc_html__('Select', 'pmthemes') );
                    ?>
                    <?php if ( ! empty( $all_taxonomies ) ): ?>
                        <?php foreach ( $all_taxonomies as $key => $tax ): ?>
                            <?php
                            printf('<option value="%s" %s>%s</option>', esc_attr( $tax->term_id ), selected($this->value(), $tax->term_id, false), esc_html( $tax->name ) );
                            ?>
                        <?php endforeach ?>
                    <?php endif ?>
                </select>

            </label>
            <?php
        }
    }

endif;
?>