<?php

/**
 * This class renders all of the font options for the theme in the WordPress Customizer.
 * 
 * @author Pix & Hue
 */

class Rilee_Fonts_Customizer_Manager {

	public $sections = array();
	public $settings = array();

	function __construct($sections, $settings) {

		$this->sections = $sections;
		$this->settings = $settings;

		$this->rilee_fonts_register_actions();
	}

	/*****************************************************************************
 	* Render Custom Actions needed for Class
	****************************************************************************/ 
	public function rilee_fonts_register_actions() {

		add_action('customize_register', array($this, 'rilee_fonts_load_fonts_styles'));
		add_action('customize_register', array($this, 'rilee_fonts_render_customizer_options'));
		add_action('customize_controls_enqueue_scripts', array($this, 'rilee_fonts_load_fonts_scripts'));
	}

	/*****************************************************************************
 	* Load the Scripts + Styles
	****************************************************************************/ 
	public function rilee_fonts_load_fonts_styles($wp_customize) {

		wp_enqueue_style('jquery-ui-css', plugins_url('css/jquery-ui.css',  dirname(__FILE__)));	
		wp_enqueue_style('rilee-fonts-css', plugins_url('css/rilee-fonts.css',  dirname(__FILE__)));	
	}

	public function rilee_fonts_load_fonts_scripts($wp_customize) {
		wp_enqueue_script('rilee-fonts-js', plugins_url('js/rilee-fonts-custom.js',  dirname(__FILE__)), array('customize-controls'));
	}	
	
	/*****************************************************************************
 	* Render the Customizer Settings
	****************************************************************************/ 
	public function rilee_fonts_render_customizer_options($wp_customize) {

		//Render the theme Fonts Panel
		$wp_customize->add_panel('rilee_fonts_panel', array(
			'title' => 'Rilee Fonts',
			'capability'     => 'edit_theme_options',
			'priority' => 201,
		));

		//Add the Customizer Sections 
		foreach($this->sections as $section=>$key) {
				
			$wp_customize->add_section( $key['id'], array(
				'panel' => $key['panel'],
				'title' => $key['title'],
				'description' => $key['description'],
				'priority' => $key['priority'],
			));	
		}

		//Add the Customizer Settings + Controls
		foreach($this->settings as $control_section) {

			foreach($control_section as $setting=>$key) {

				/* --------------------- File Upload --------------------- */
				if($key['type'] == 'rilee_file_upload') {
					$file_number = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight');
					//$counter = 0; 

					for($i = 0; $i < 8; $i++) {

						/* --------- File Upload Title --------- */
						$wp_customize->add_setting($key['name'] . '_subtitle_' . $file_number[$i], array(
					  		'sanitize_callback' => 'wp_filter_nohtml_kses',
						));

						$wp_customize->add_control(new Rilee_Fonts_Title_Control($wp_customize, $key['name'] . '_subtitle_' . $file_number[$i], array(
							'type' => 'font_control_title',
							'title' => $key['subtitle'] . ' ' . $file_number[$i],
							'subtitle' => __('1) Select Font File: ', 'rilee-fonts'),
							'section' => $key['section'], 
							)
						));
						
						/* --------- File Upload Functionality --------- */
						$wp_customize->add_setting($key['name'] . '_' . $file_number[$i], array(
					  		'sanitize_callback' => 'rilee_fonts_sanitize_file',
						));
						$wp_customize->add_control(new WP_Customize_Upload_Control($wp_customize, $key['name'] . '_' . $file_number[$i], array(
							'type' => 'upload',
							'title' => $key['title'],
							'section' => $key['section'], 
							)
						));

						/* --------- Select File Type --------- */
						$wp_customize->add_setting($key['name'] . '_file_type_' . $file_number[$i], array(
				  			'sanitize_callback' => 'rilee_fonts_sanitize_select',
				  			'default' => 'woff',
						));

						$wp_customize->add_control(new WP_Customize_Control($wp_customize, $key['name'] . '_file_type_' . $file_number[$i], array(
							'type' => 'select',
							'label' => __('2) Select File Type:', 'rilee-fonts'),
							'name' => $key['name'] . '_file_type_' . $file_number[$i],
							'section' => $key['section'],
							'choices' => array(
						        'woff' => 'WOFF',
						        'woff2' => 'WOFF2',
						        'truetype' => 'TTF',
						        'opentype' => 'OTF',
						    ) 
							)
						));

						/* --------- Add Font Name --------- */
						$wp_customize->add_setting('font_family_name_' . $file_number[$i], array(
					  		'sanitize_callback' => 'wp_filter_nohtml_kses',
						));
						$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'font_family_name_' . $file_number[$i], array(
							'type' => 'text',
							'label' => __('3) Add Font Name:', 'rilee-fonts'),
							'section' => $key['section'],
							)
						));	
					}
				}	
				
				/* --------------------- Font Controls  --------------------- */
				if($key['type'] == 'rilee_font_control') { 

					if(isset($key['custom_type']) && $key['custom_type'] == 'elementor_section') {

						/* --------------------- Override Elementor Page Builder --------------------- */
						$wp_customize->add_setting('rilee_override_elementor', array(
					  		'sanitize_callback' => 'rilee_fonts_sanitize_select',
					  		'default' => 'no_override_elementor',
						));

						$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'rilee_override_elementor', array(
							'type' => 'select',
							'label' => __('Override Elementor Page Builder', 'rilee-fonts'),
							'name' => 'rilee-override-elementor',
							'class' => 'rilee-override-elementor',
							'section' => 'rilee_elementor_fonts_section',
							'choices' => array(
						        'no_override_elementor' => 'No',
						        'override_elementor' => 'Yes',
						    ) 
							)
						)); 
					}

					/* ---------------------  Start the Font Type Inner Section --------------------- */
					$wp_customize->add_setting($key['name'] . '_title', array(
				  		'sanitize_callback' => 'wp_filter_nohtml_kses',
					));

					$wp_customize->add_control(new Rilee_Fonts_Title_Control($wp_customize, $key['name'] . '_title', array(
						'type' => 'font_control_title',
						'title' => $key['title'],
						'section' => $key['section'], 
						)
					));
					
					/* --------------------- Font Family --------------------- */
					$file_number = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'); 
					$font_family_choices = array();
					$font_family_choices[$key['font_controls']['font-family']] = $key['font_controls']['font-family'];
					
					//Get the Font Family options based on File Uploads
					for($i = 0; $i < 8; $i++) {
						if(get_theme_mod('rilee_file_upload_' . $file_number[$i])) {
							$font_family_choices[get_theme_mod('font_family_name_' . $file_number[$i])] = get_theme_mod('font_family_name_' . $file_number[$i]);
						}
					}

					//Add the Font Family Setting + Control 
					$wp_customize->add_setting($key['name'] . '-font-family', array(
				  		'default' => $key['font_controls']['font-family'],
					));

					$wp_customize->add_control(new WP_Customize_Control($wp_customize, $key['name'] . '-font-family', array(
						'type' => 'select',
						'label' => __('Font Family', 'rilee-fonts'),
						'name' => $key['name'] . '-font-family',
						'class' => 'rilee_font_family',
						'section' => $key['section'],
						'choices' => $font_family_choices,
						)
					));

					/* --------------------- Font Size --------------------- */
					$wp_customize->add_setting($key['name'] . '-font-size', array(
				  		'sanitize_callback' => 'rilee_fonts_sanitize_float',
				  		'default' => $key['font_controls']['font-size'],
					));

					$wp_customize->add_control(new Rilee_Fonts_Slider_Control($wp_customize, $key['name'] . '-font-size', array(
						'type' => 'slider',
						'class' => 'rilee_font_size',
						'title' => __('Font Size', 'rilee-fonts'),
						'name' => $key['name'] . '-font-size',
						'section' => $key['section'], 
						'metric' => 'px',
						)
					));

					/* --------------------- Tablet Font Size --------------------- */
					$wp_customize->add_setting($key['name'] . '-tablet-font-size', array(
				  		'sanitize_callback' => 'rilee_fonts_sanitize_float',
				  		'default' => $key['font_controls']['tablet-font-size'],
					));

					$wp_customize->add_control(new Rilee_Fonts_Slider_Control($wp_customize, $key['name'] . '-tablet-font-size', array(
						'type' => 'slider',
						'class' => 'rilee_font_size',
						'title' => __('(Tablet) Font Size', 'rilee-fonts'),
						'name' => $key['name'] . '-tablet-font-size',
						'section' => $key['section'], 
						'metric' => 'px',
						)
					));

					/* --------------------- Mobile Font Size --------------------- */
					$wp_customize->add_setting($key['name'] . '-mobile-font-size', array(
				  		'sanitize_callback' => 'rilee_fonts_sanitize_float',
				  		'default' => $key['font_controls']['mobile-font-size'],
					));

					$wp_customize->add_control(new Rilee_Fonts_Slider_Control($wp_customize, $key['name'] . '-mobile-font-size', array(
						'type' => 'slider',
						'class' => 'rilee_font_size',
						'title' => __('(Mobile) Font Size', 'rilee-fonts'),
						'name' => $key['name'] . '-mobile-font-size',
						'section' => $key['section'], 
						'metric' => 'px',
						)
					));

					/* --------------------- Letter Spacing --------------------- */
					$wp_customize->add_setting($key['name'] . '-letter-spacing', array(
				  		'sanitize_callback' => 'rilee_fonts_sanitize_float',
				  		'default' => $key['font_controls']['letter-spacing'],
					));

					$wp_customize->add_control(new Rilee_Fonts_Slider_Control($wp_customize, $key['name'] . '-letter-spacing', array(
						'type' => 'slider',
						'title' => __('Letter Spacing', 'rilee-fonts'),
						'name' => $key['name'] . '-letter-spacing',
						'class' => 'rilee_letter_spacing',
						'section' => $key['section'], 
						'metric' => 'px',
						)
					));

					/* --------------------- Line Height --------------------- */
					$wp_customize->add_setting($key['name'] . '-line-height', array(
				  		'sanitize_callback' => 'rilee_fonts_sanitize_float',
				  		'default' => $key['font_controls']['line-height'],
					));

					$wp_customize->add_control(new Rilee_Fonts_Slider_Control($wp_customize, $key['name'] . '-line-height', array(
						'type' => 'slider',
						'title' => __('Line Height', 'rilee-fonts'),
						'name' => $key['name'] . 'line-height',
						'class' => 'rilee_line_height',
						'section' => $key['section'], 
						'metric' => 'em',
						)
					));

					/* --------------------- Text Transform --------------------- */
					$wp_customize->add_setting($key['name'] . '-text-transform', array(
				  		'sanitize_callback' => 'rilee_fonts_sanitize_select',
				  		'default' => $key['font_controls']['text-transform'],
					));

					$wp_customize->add_control(new WP_Customize_Control($wp_customize, $key['name'] . '-text-transform', array(
						'type' => 'select',
						'label' => __('Text Transform', 'rilee-fonts'),
						'name' => $key['name'] . '-text-transform',
						'class' => 'rilee_text_transform',
						'section' => $key['section'],
						'choices' => array(
					        'none' => 'None',
					        'capitalize' => 'Capitalize',
					        'uppercase' => 'Uppercase',
					        'lowercase' => 'Lowercase',
					    ) 
						)
					));

				}	
			}
		}	
	}
}	