<?php

if( class_exists( 'WP_Customize_Control' ) ) {
	
	// Range Control
	
	class KKTFWP_Range_Control extends Kktfwp_Customize_Control {
		
		public $type = 'kktfwp_range';

		public function enqueue() {
			wp_enqueue_script( 'customizer-range-value-control', get_theme_file_uri( '/includes/customizer/js/customizer-custom-controls.js' ), array( 'jquery' ), kktfwp_themeData(), true );
			wp_enqueue_style( 'kktfwp-customizer-styles', get_theme_file_uri( '/includes/customizer/css/style.css' ), array(), kktfwp_themeData() );
		}

		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 esc_html( $this->description) ?></span>
				<?php endif; ?>
				<div class="range-slider"  style="width:100%; display:flex;flex-direction: row;justify-content: flex-start;">
					<span  style="width:100%; flex: 1 0 0; vertical-align: middle;"><input class="range-slider__range" type="range" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->input_attrs(); $this->link(); ?>>
					<input class="range-slider__value" type="text" size="40" value="0">
					
					</span>
				</div>
			</label>
			<?php
		}
	}

	// Toggle Control

	class KKTFWP_Toggle_Control extends Kktfwp_Customize_Control {
		/**
		 * The type of control being rendered
		 */
		public $type = 'kktfwp_toggle';
		/**
		 * Enqueue our scripts and styles
		 */
		public function enqueue(){
				wp_enqueue_style( 'kktfwp-customizer-styles', get_theme_file_uri( '/includes/customizer/css/style.css' ), array(), kktfwp_themeData() );
		}
		/**
		 * Render the control in the customizer
		 */
		public function render_content(){
		?>
			<div class="toggle-switch-control">
				<div class="toggle-switch">
					<input type="checkbox" id="<?php echo esc_attr($this->id); ?>" name="<?php echo esc_attr($this->id); ?>" class="toggle-switch-checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?>>
					<label class="toggle-switch-label" for="<?php echo esc_attr($this->id); ?>">
						<span class="toggle-switch-inner"></span>
						<span class="toggle-switch-switch"></span>
					</label>
				</div>
				<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
				<?php if( !empty( $this->description ) ) { ?>
					<span class="customize-control-description"><?php echo esc_html( $this->description ); ?></span>
				<?php } ?>
			</div>
		<?php
		}
	}
	
	/**
	 * A class to create a dropdown for all google fonts
	 */
	class KKTFWP_Control_Google_Font extends Kktfwp_Customize_Control {
		
		private $fonts = false;		
		private $fontFile;				
		public $type = 'kktfwp_google_font';

		public $auto_preview = 'font';

		public function __construct( $manager, $id, $args = array(), $options = array() ) {
			parent::__construct( $manager, $id, $args );
			$this->fontFile = dirname( __FILE__ ) . '/cache/google-web-fonts.txt';
			$this->fonts = $this->get_fonts();			
		}
		
		public function enqueue() {
			wp_enqueue_style( 'kktfwp-customizer-select2', get_theme_file_uri( '/includes/customizer/css/select2.min.css' ), array(), kktfwp_themeData() );
			wp_enqueue_script( 'kktfwp-customizer-select2', get_theme_file_uri( '/includes/customizer/js/jquery.select2.min.js' ), array( 'jquery' ), kktfwp_themeData() );
		}


		/**
		 * Render the content of the category dropdown
		 *
		 * @return null
		 */
		public function render_content() {
			if ( ! empty( $this->fonts ) ) {
				?>
				<label> <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> 
					<span class="description customize-control-description"><?php echo esc_html( $this->description ); ?></span>
						<select class="select-two" <?php $this->link(); ?>>
						<?php
						$fonts = array();
						foreach ( $this->fonts as $font ) {
							$variants = array();
							
							foreach ( $font->variants as $var ) {
								if ( stripos( $var, 'talic') ) continue;
								$variants[] = ( $var === 'regular' ) ? '400' : $var;								
							}
							$fonts[$font->family] = $variants;
							$variants = implode(',', $variants);
							$variants = esc_attr($variants);
							printf( '<option data-variants="%s" value="%s" %s>%s</option>', $variants, $font->family, selected( $this->value(), $font->family, false ), $font->family );
						}
						?>
					</select>
					<span class="description customize-control-description"><?php echo esc_html( 'Font Weight', 'essu' ); ?></span>
					<select id="font-variants_<?php echo esc_attr($this->id); ?>" class="font-variants select-two" <?php $this->link('variant'); ?> >
						<?php 
						foreach ($fonts[$this->value()] as $key) {
							printf( '<option value="%s" %s>%s</option>', $key, selected( $this->value('variant'), $key, false ), $key ); 						
						}
						?>
					</select> </label>

				<script type="text/javascript">
					jQuery('.select-two').select2({
						width: '100%'
					});
				</script>
				<style>
					.wp-full-overlay {
						z-index: 500 !important; /* Lower the z-index of customizer overlay so that select2 is visible*/
					}
				</style>
				<?php
			}
		}

		/**
		 * Get the google fonts from the API or in the cache
		 *
		 * @param  $amount
		 *
		 * @return array
		 */
		public function get_fonts( $amount = 'all' ) {
			require_once(ABSPATH . 'wp-admin/includes/file.php');
			WP_Filesystem();			
			global $wp_filesystem;

			if ( $wp_filesystem->exists( $this->fontFile ) ) {
				$content = json_decode( $wp_filesystem->get_contents( $this->fontFile ));
			
				if ( $amount == 'all' ) {
					return $content->items;
					
				} else {
					return array_slice( $content->items, 0, $amount );
				}
			}
		}
	}
	
	if ( ! function_exists( 'kktfwp_switch_sanitization' ) ) {
		function kktfwp_switch_sanitization( $input ) {
			if ( true === $input ) {
				return 1;
			} else {
				return 0;
			}
		}
	}
	
	if ( ! function_exists( 'kktfwp_footer' ) ) {
		function kktfwp_footer( $input ) {
			
		$allowed_html = array(
			'a'      => array(
					'href'  => array(),
					'title' => array(),
					'target'=> array()
			),
			'br'     => array(),
			'em'     => array(),
			'span'   => array(
				'style' => array()
			),
			'strong' => array(),
			'img'    => array(
					'src'   => array(),
					'alt' => array(),
					'style'=> array()
			)
		);

		return wp_kses( $input, $allowed_html );
		}
	}
	
	if ( ! function_exists( 'kktfwp_sanitize_select' ) ) {
		function kktfwp_sanitize_select( $input, $setting ) {
			$input = sanitize_key( $input );
			//global $wp_customize;
			$control = $setting->manager->get_control( $setting->id )->choices;
			//$control = $wp_customize->get_control( $setting->id )->choices;
			//var_dump($setting->id);
			//var_dump($input);
			//var_dump($setting->manager->get_control( $setting->id )->choices);
			
			/* if ( array_key_exists( $input, $control ) ) {
				return $input;
			} else {
				return $setting->default;
			} */

			return ( array_key_exists( $input, $control ) ) ? $input : $setting->default;
		}
	}
	
	/**
	 * Decimal sanitization
	 *
	 * @param  string		Input value to check
	 * @return integer	Returned integer value
	 */
	if ( ! function_exists( 'kktfwp_sanitize_float' ) ) {
		function kktfwp_sanitize_float( $input ) {
			return floatval( $input );
		}
	}
}