<?php

namespace Tech360;

defined( 'ABSPATH' ) or die;

class Customizer {

	private $sections = [];
	private $controls = [];
	private $control_types = [ 'text', 'checkbox', 'radio', 'select', 'textarea', 'dropdown-pages', 'email', 'url', 'number', 'hidden', 'date' ];
	private $special_control_types = [ 'color', 'upload', 'image' ];
	private $remove_tagline;

	function __construct( $remove_tagline = false ) {

		$this->remove_tagline = $remove_tagline;

	}

	public function add_section( $section ) {

		if( ! is_array( $section ) ) {
			return;
		}

		if( ! array_key_exists( 'id', $section ) || ! array_key_exists( 'title', $section ) ) {
			return;
		}

		$this->sections[] = $section;

	}

	public function add_control( $control ) {

		if( ! is_array( $control ) ) {
			return;
		}

		if( ! array_key_exists( 'id', $control ) || ! array_key_exists( 'title', $control ) || ! array_key_exists( 'section', $control ) ) {
			return;
		}

		if( array_key_exists( 'type', $control ) && ! in_array( $control[ 'type' ], array_merge( $this->control_types, $this->special_control_types ) ) ) {
			return;
		}

		if( array_key_exists( 'choices', $control ) && ! is_array( $control[ 'choices' ] ) ) {
			return;
		}

		$this->controls[] = $control;

	}

	public function register( $wp_customizer ) {

		// Remove tagline
		if( $this->remove_tagline ) {
			$wp_customizer->remove_control( 'blogdescription' );
		}

		// Add sections
		foreach( $this->sections as $section ) {

			$section_args = [
				'title' => $section[ 'title' ]
			];

			if( array_key_exists( 'description', $section ) ) {
				$section_args[ 'description' ] = $section[ 'description' ];
			}

			if( array_key_exists( 'priority', $section ) ) {
				$section_args[ 'priority' ] = intval( $section[ 'priority' ] );
			}

			$wp_customizer->add_section( $section[ 'id' ], $section_args );

		}

		// Add settings
		foreach( $this->controls as $setting ) {

			$setting_args = [
				'type' => 'option',
				'default' => array_key_exists( 'default', $setting ) ? $setting[ 'default' ] : '',
				'transport' => array_key_exists( 'transport', $setting ) ? $setting[ 'transport' ] : 'refresh'
			];

			$wp_customizer->add_setting( $setting[ 'id' ], $setting_args );

		}

		// Add controls
		foreach( $this->controls as $control ) {

			$new_control_args = [
				'label' => $control[ 'title' ],
				'section' => $control[ 'section' ],
				'settings' => $control[ 'id' ]
			];

			if( array_key_exists( 'description', $control ) ) {
				$new_control_args[ 'description' ] = $control[ 'description' ];
			}

			if( array_key_exists( 'choices', $control ) ) {
				$new_control_args[ 'choices' ] = $control[ 'choices' ];
			}

			if( array_key_exists( 'type', $control ) ) {

				if( 'color' == $control[ 'type' ] ) {
					$new_control = new \WP_Customize_Color_Control( $wp_customizer, $control[ 'id' ], $new_control_args );
				} elseif( 'upload' == $control[ 'type' ] ) {
					$new_control = new \WP_Customize_Upload_Control( $wp_customizer, $control[ 'id' ], $new_control_args );
				} elseif( 'image' == $control[ 'type' ] ) {
					$new_control = new \WP_Customize_Image_Control( $wp_customizer, $control[ 'id' ], $new_control_args );
				} else {
					$new_control_args[ 'type' ] = $control[ 'type' ];
					$new_control = new \WP_Customize_Control( $wp_customizer, $control[ 'id' ], $new_control_args );
				}

			} else {
				$new_control = new \WP_Customize_Control( $wp_customizer, $control[ 'id' ], $new_control_args );
			}

			$wp_customizer->add_control( $new_control );

		}

	}

	public function render() {
		add_action( 'customize_register', [ $this, 'register' ] );
	}

	public static function option( $option_name ) {
		return get_option( $option_name, false );
	}

}
