<?php
abstract class WPCTP_Customizer_Base {
	public function __construct() {
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue' ) );
		if ( method_exists( $this, 'register' ) ) {
			add_action( 'customize_register', array( $this, 'register' ), 15 );
		} else {
			_doing_it_wrong( 'WPCTP_Customizer_Base', __( 'When extending WPCTP_Customizer_Base, you must create a register method.', 'wpctp' ) );
		}
		if ( method_exists( $this, 'scripts' ) ) {
			add_action( 'customize_preview_init', 'scripts' );
		}
	}
	protected function get_field_name( $name ) {
		return sprintf( '%s[%s]', $this->settings_field, $name );
	}
	protected function get_field_id( $id ) {
		return sprintf( '%s[%s]', $this->settings_field, $id );
	}
	protected function get_field_value( $key ) {
		return wpctp_get_option( $key, $this->settings_field );
	}
	public function enqueue(){
		wp_enqueue_style( 'wp-color-picker' ); 
		wp_enqueue_script( 'wp-color-picker' ); 
        wp_enqueue_script( 'wpctp-customize', WPCTP_JS_URL . '/customizer.js', array() );
		wp_enqueue_style( 'wpctp-customize', WPCTP_CSS_URL . '/customizer.css', array() );
	}
}
class WPCTP_Customizer extends WPCTP_Customizer_Base {
	public $settings_field = 'wpctp-settings';
	public function default_settings() {
		return wpctp_default_args();
	}
	public function register( $wp_customize ) {
		$priority = 5;
		foreach( $this->default_settings() as $key => $panel ){
			$panel['priority'] = isset( $panel['priority'] ) ? $panel['priority'] : $priority;
			$wp_customize->add_panel( $key, $panel );
			$priority = $priority + 5;
		}
		if( ! get_option( $this->settings_field ) ) :
			update_option( $this->settings_field, wpctp_get_option_settings() );
		endif;
		$this->option_customizer( $wp_customize );
		$this->option_fields( $wp_customize );
	}
	private function option_customizer( $wp_customize ) {
		$priority = 5;
		foreach( $this->default_settings() as $panel_id => $option ){
			if( ! empty( $option['sub_tab'] ) ){
				foreach( $option['sub_tab'] as $id => $sections ){
					$wp_customize->add_section(
						'section_' . $id,
						array(
							'title'		=> $sections['title'],
							'priority' 	=> isset( $sections['priority'] ) ? $sections['priority'] : $priority,
							'panel'		=> $panel_id
						)
					);
					$priority = $priority + 5;
				}
			}
		}
	}
	private function option_fields( $wp_customize ) {
		foreach( $this->default_settings() as $option ){
			if( ! empty( $option['sub_tab'] ) ){
				foreach( $option['sub_tab'] as $id => $fields ){
					if( ! empty( $fields['fields'] ) ) {
						foreach( $fields['fields'] as $_id => $field ){
							$field = wp_parse_args(
								$field, 
								array(
									'default' => '',
									'label' => '', 
									'type' => '',
									'description' => '', 
									'choices' => array(),
									'input_attrs' => array(), 
								) 
							);
							$setting_args = array(
								'type' => 'option'
							);
							if( isset( $field['default'] ) ) :
								$setting_args['default'] = $field['default'];
							endif;
							$wp_customize->add_setting(
								$this->get_field_name( $_id ),
								$setting_args
							);
							$control_args = array(
								'label'    	=> $field['label'],
								'section'  	=> 'section_' . $id,
								'settings' 	=> $this->get_field_name( $_id ),
								'type'     	=> $field['type']
							);
							if( isset( $field['description'] ) ) :
								$control_args['description'] = $field['description'];
							endif;
							if( isset( $field['choices'] ) ) :
								$control_args['choices'] = $field['choices'];
							endif;
							if( isset( $field['input_attrs'] ) ) :
								$control_args['input_attrs'] = $field['input_attrs'];
							endif;
							if( $field['type'] == 'googlefont' ) :
								$wp_customize->add_control(
									new WPCTP_Google_Font_Control(
										$wp_customize,
										'control_' . $_id,
										$control_args
									)
								);
							elseif( $field['type'] == 'image' ) :
								$picture_control_args = $control_args;
								$wp_customize->add_control(
									new WP_Customize_Image_Control(
										$wp_customize,
										'control_' . $_id,
										$picture_control_args
									)
								);
							elseif( $field['type'] == 'image-choices' ) :
								$wp_customize->add_control(
									new WPCTP_Image_Choices_Control(
										$wp_customize,
										'control_' . $_id,
										$control_args
									)
								);
							else :
								$wp_customize->add_control(
									'control_' . $_id,
									$control_args
								);
							endif;
						}
					}
				}
			}
		}
	}
}
add_action( 'admin_init', 'wpctp_register_settings' );
function wpctp_register_settings() {
	if( ! get_option( 'wpctp' ) ) :
		add_option( 'wpctp', wpctp_get_option_settings() );
	endif;
}
add_action( 'init', 'wpctp_customizer_init' );
function wpctp_customizer_init() {
	new WPCTP_Customizer;
}