<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Nevo Theme Customizer
 *
 * @package nevo
 */
class  Nevo_Customizer {
	static $config;
	static $_instance;
	static $has_icon = false;
	static $has_font = false;
	public $devices = array( 'desktop', 'tablet', 'mobile' );
	private $selective_settings = array();

	function __construct() {

	}

	/**
	 * Main initial
	 */
	function init() {

		require_once get_template_directory() . '/inc/classes/class-customizer-sanitize.php';
		require_once get_template_directory() . '/inc/classes/class-customizer-auto-css.php';

		if ( is_admin() || is_customize_preview() ) {
			add_action( 'customize_register', array( $this, 'register' ), 666 );
			add_action( 'customize_preview_init', array( $this, 'preview_js' ) );
			add_action( 'wp_ajax_nevo/customizer/ajax/get_icons', array( $this, 'get_icons' ) );
			add_action( 'wp_footer', array( $this, 'print_preview_scripts' ), 200 );

			require_once get_template_directory() . '/inc/classes/class-customizer-fonts.php';
			require_once get_template_directory() . '/inc/classes/class-customizer.php';
		}

		add_action( 'wp_ajax_nevo__reset_section', array( 'Nevo_Customizer', 'reset_customize_section' ) );
	}

	/**
	 * Instance.
	 *
	 * @return Nevo_Customizer
	 */
	static function get_instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}

		return self::$_instance;
	}

	/**
	 * Reset Customize section
	 */
	static function reset_customize_section() {
		if ( ! current_user_can( 'customize' ) ) {
			wp_send_json_error();
		}

		$settings = isset( $_POST['settings'] ) ? wp_unslash( $_POST['settings'] ) : array(); // phpcs:ignore

		foreach ( $settings as $k ) {
			$k = sanitize_text_field( $k );
			remove_theme_mod( $k );
		}

		wp_send_json_success();
	}

	/**
	 * Reset Customize section
	 */
	function get_icons() {
		if ( ! current_user_can( 'customize' ) ) {
			wp_send_json_error();
		}

		require_once get_template_directory() . '/inc/classes/class-customizer-icons.php';
		wp_send_json_success( Nevo_Font_Icons::get_instance()->get_icons() );
		die();
	}

	/**
	 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
	 */
	function preview_js() {
		if ( is_customize_preview() ) {
			$suffix = Nevo()->get_asset_suffix();

			wp_enqueue_script( 'nevo-customizer-auto-css', esc_url( get_template_directory_uri() ) . '/assets/js/customizer/auto-css' . $suffix . '.js', array( 'customize-preview' ), '20151215', true );
			wp_enqueue_script(
				'nevo-customizer',
				esc_url( get_template_directory_uri() ) . '/assets/js/customizer/customizer' . $suffix . '.js',
				array(
					'customize-preview',
					'customize-selective-refresh',
				),
				'20151215',
				true
			);
			wp_localize_script(
				'nevo-customizer-auto-css',
				'Nevo_Preview_Config',
				array(
					'fields'         => $this->get_config(),
					'devices'        => $this->devices,
					'typo_fields'    => $this->get_typo_fields(),
					'styling_config' => $this->get_styling_config(),
				)
			);
			// Thêm defer vào các tệp JS
			wp_script_add_data( 'nevo-customizer-auto-css', 'defer', true );
			wp_script_add_data( 'nevo-customizer', 'defer', true );
		}
	}
	/**
	 * Print <script> tags for preview frame.
	 */
	/* (function() {
		'use strict';
		function initialize() {
			if (window.nevoSSPro && typeof window.nevoSSPro.initAll === 'function') {
				window.nevoSSPro.initAll();
			}
			if (window.nevoHSPro && typeof window.nevoHSPro.initAll === 'function') {
				window.nevoHSPro.initAll();
			}
			if (window.nevomegamenu && typeof window.nevomegamenu.initAll === 'function') {
				window.nevomegamenu.initAll();
			}
			if (window.nevoajaxpostPro && typeof window.nevoajaxpostPro.initAll === 'function') {
				window.nevoajaxpostPro.initAll();
			}
			if (window.nevoAutoCompletePro && typeof window.nevoAutoCompletePro.initAll === 'function') {
				window.nevoAutoCompletePro.initAll();
			}
		}
		window.addEventListener('load', initialize);
		if ( 'undefined' !== typeof wp && wp.customize && wp.customize.selectiveRefresh && wp.customize.widgetsPreview && wp.customize.widgetsPreview.WidgetPartial ) {
			wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
				initialize();
			} );
		}
	})(); */
	public function print_preview_scripts() {
		?>
		<style type="text/css">
			.site-header .customize-partial-edit-shortcut,.site-footer .customize-partial-edit-shortcut,.nevo-vertical-header .customize-partial-edit-shortcut,.related-posts .customize-partial-edit-shortcut{display:none!important}.site span.customize-partial-edit-shortcut{padding:0!important}div.builder-item-focus{display:inline-flex;position:relative;align-items:center;height:100%}.site-header .customize-partial-edit{top:100%}.nevo-popup .customize-partial-edit,.site-content .customize-partial-edit,.site-footer .customize-partial-edit{bottom:100%}.customize-partial-edit{position:absolute;right:50%;opacity:.5;display:none;z-index:99;font-weight:600;line-height:14px;cursor:pointer;color:var(--nvt-white)!important;background:#0073aa;white-space:nowrap;padding:5px 6px 6px;font-size:12px!important;transform:translateX(50%);-webkit-transform:translateX(50%);list-style:none outside none}.site-header .customize-partial-edit:before,.nevo-popup .customize-partial-edit:after,.site-content .customize-partial-edit:after,.site-footer .customize-partial-edit:after{position:absolute;left:calc(50% - 10px);content:'';width:0;height:0;border-style:solid}.site-header .customize-partial-edit:before{top:-10px;border-width:0 10px 10px;border-color:#0073aa transparent}.nevo-popup .customize-partial-edit:after,.site-content .customize-partial-edit:after,.site-footer .customize-partial-edit:after{bottom:-10px;border-width:10px 10px 0;border-color:#0073aa transparent}.builder-item-focus:hover > .customize-partial-edit{opacity:1;display:block;z-index:9999999}.site-footer .builder-item-focus:hover > .customize-partial-edit{left:0;right:auto;transform:translateX(15px);-webkit-transform:translateX(15px)}.rtl .site-footer .builder-item-focus:hover > .customize-partial-edit{left:auto;right:0}
		</style>
		<script defer="defer" type="text/javascript">
			(function() {
				'use strict';
				document.addEventListener( 'load', function() {
					if ( 'undefined' !== typeof wp && wp.customize && wp.customize.selectiveRefresh && wp.customize.widgetsPreview && wp.customize.widgetsPreview.WidgetPartial ) {
						wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function( placement ) {
							window.nevo.initAll();
						} );
					}
				});
			})();
		</script>
		<?php
	}
	/**
	 * Get all customizer settings/control that added via `nevo/customizer/config` hook
	 *
	 *  Ensure you call this method after all config files loaded
	 *
	 * @param WP_Customize_Manager $wp_customize
	 *
	 * @return array
	 */
	static function get_config( $wp_customize = null ) {
		if ( is_null( self::$config ) ) {

			$_config = apply_filters( 'nevo/customizer/config', array(), $wp_customize );
			$config  = array();
			foreach ( $_config as $f ) {

				$f = wp_parse_args(
					$f,
					array(

						'priority'    => null,
						'title'       => null,
						'label'       => null,
						'name'        => null,
						'type'        => null,
						'description' => null,
						'capability'  => null,
						'mod'         => null, // Can be theme_mod or option, default theme_mod.
						'settings'    => null,

						'active_callback'      => null, // For control.

						/**
						 * For settings
						 */
						'sanitize_callback'    => array( 'Nevo_Sanitize_Input', 'sanitize_customizer_input' ),
						'sanitize_js_callback' => null,
						'theme_supports'       => null,
						'default'              => null,

						/**
						 * For selective refresh
						 */
						'selector'             => null,
						'render'               => null, // same render_callback.
						'render_callback'      => null,
						'css_format'           => null,

						'device'          => null,
						'device_settings' => null,

						'field_class' => null, // Custom class for control.
					)
				);

				if ( ! isset( $f['type'] ) ) {
					$f['type'] = null;
				}

				switch ( $f['type'] ) {
					case 'panel':
						$config[ 'panel|' . $f['name'] ] = $f;
						break;
					case 'section':
						$config[ 'section|' . $f['name'] ] = $f;
						break;
					default:
						if ( 'icon' == $f['type'] ) {
							self::$has_icon = true;
						}

						if ( 'font' == $f['type'] ) {
							self::$has_font = true;
						}

						if ( isset( $f['fields'] ) ) {
							if ( ! in_array( $f['type'], array( 'typography', 'styling', 'modal' ) ) ) { //phpcs:ignore
								$types = wp_list_pluck( $f['fields'], 'type' );
								if ( in_array( 'icon', $types ) ) { //phpcs:ignore
									self::$has_icon = true;
								}

								if ( in_array( 'font', $types ) ) { //phpcs:ignore
									self::$has_font = true;
								}
							}
						}

						$config[ 'setting|' . $f['name'] ] = $f;

				}
			}

			self::$config = $config;
		}

		return self::$config;
	}

	/**
	 * Check if has icon field;
	 *
	 * @return bool
	 */
	function has_icon() {
		return self::$has_icon;
	}

	/**
	 * Check if has font field;
	 *
	 * @return bool
	 */
	function has_font() {
		return self::$has_icon;
	}

	/**
	 *  Get Customizer setting.
	 *
	 * @param string      $name   Customize setting id.
	 * @param string      $device Device type for settings.
	 * @param string|bool $key    Value of this array key that you want to get.
	 *
	 * @return array|bool|string
	 */
	function get_setting( $name, $device = 'desktop', $key = false ) {
		$config    = self::get_config();
		$get_value = null;
		if ( isset( $config[ 'setting|' . $name ] ) ) {
			$default = isset( $config[ 'setting|' . $name ]['default'] ) ? $config[ 'setting|' . $name ]['default'] : false;
			$default = apply_filters( 'nevo/customize/settings-default', $default, $name );

			if ( 'option' == $config[ 'setting|' . $name ]['mod'] ) {
				$value = get_option( $name, $default );
			} else {
				$value = get_theme_mod( $name, $default );
			}

			// Maybe need merge defined items with saved item for defined list.
			if (
				'repeater' == $config[ 'setting|' . $name ]['type']
				&& isset( $config[ 'setting|' . $name ]['addable'] )
				&& false == $config[ 'setting|' . $name ]['addable']
			) {
				$value = self::merge_items( $value, $default );
			}

			if ( ! $config[ 'setting|' . $name ]['device_settings'] ) {
				return $value;
			}
		} else {
			$value = get_theme_mod( $name, null );
		}

		if ( ! $key ) {
			if ( 'all' != $device ) {
				if ( is_array( $value ) && isset( $value[ $device ] ) ) {
					$get_value = $value[ $device ];
				} else {
					$get_value = $value;
				}
			} else {
				$get_value = $value;
			}
		} else {
			$value_by_key = isset( $value[ $key ] ) ? $value[ $key ] : false;
			if ( 'all' != $device && is_array( $value_by_key ) ) {
				if ( is_array( $value_by_key ) && isset( $value_by_key[ $device ] ) ) {
					$get_value = $value_by_key[ $device ];
				} else {
					$get_value = $value_by_key;
				}
			} else {
				$get_value = $value_by_key;
			}
		}

		return $get_value;
	}


	/**
	 * Merge 2 array width `_key` is key of each item
	 *
	 * @since 0.2.5
	 *
	 * @param array $destination
	 * @param array $new_array
	 *
	 * @return array
	 */
	public static function merge_items( $destination = array(), $new_array = array() ) {

		$keys     = array();
		$new_keys = array();

		foreach ( $destination as $item ) {
			if ( isset( $item['_key'] ) ) {
				$keys[ $item['_key'] ] = $item['_key'];
			}
		}

		if ( empty( $keys ) ) {
			return $destination;
		}

		// Add item if it not in saved list.
		foreach ( $new_array as $item ) {
			if ( isset( $item['_key'] ) ) {
				$new_keys[ $item['_key'] ] = $item['_key'];
				if ( ! isset( $keys[ $item['_key'] ] ) ) {
					$destination[] = $item;
				}
			}
		}

		// Remove the item not in the defined list.
		$new_destination = array();
		foreach ( $destination as $_dk => $item ) {
			if ( isset( $item['_key'] ) ) {
				if ( isset( $new_keys[ $item['_key'] ] ) ) {
					$new_destination[] = $item;
				}
			} else {
				$new_destination[] = $item;
			}
		}

		return $new_destination;
	}

	/**
	 * Get customizer setting data when the field type is `modal`
	 *
	 * @param string      $name Setting name.
	 * @param string|bool $tab  String tab name.
	 *
	 * @return array|bool
	 */
	function get_setting_tab( $name, $tab = null ) {
		$values = $this->get_setting( $name, 'all' );
		if ( ! $tab ) {
			return $values;
		}
		if ( is_array( $values ) && isset( $values[ $tab ] ) ) {
			return $values[ $tab ];
		}

		return false;
	}

	/**
	 * Get typography fields
	 *
	 * @return array
	 */
	function get_typo_fields() {
		$typo_fields = array(
			array(
				'name'    => 'font',
				'type'    => 'select',
				'label'   => __( 'Font Family', 'nevo' ),
				'choices' => array(),
			),
			array(
				'name'    => 'font_weight',
				'type'    => 'select',
				'label'   => __( 'Font Weight', 'nevo' ),
				'choices' => array(),
			),
			array(
				'name'  => 'languages',
				'type'  => 'checkboxes',
				'label' => __( 'Font Languages', 'nevo' ),
			),
			array(
				'name'            => 'font_size',
				'type'            => 'slider',
				'label'           => __( 'Font Size', 'nevo' ),
				'device_settings' => true,
				'min'             => 9,
				'max'             => 80,
				'step'            => 1,
			),
			array(
				'name'            => 'line_height',
				'type'            => 'slider',
				'label'           => __( 'Line Height', 'nevo' ),
				'device_settings' => true,
				'min'             => 9,
				'max'             => 80,
				'step'            => 1,
			),
			array(
				'name'  => 'letter_spacing',
				'type'  => 'slider',
				'label' => __( 'Letter Spacing', 'nevo' ),
				'min'   => - 10,
				'max'   => 10,
				'step'  => 0.1,
			),
			array(
				'name'    => 'style',
				'type'    => 'select',
				'label'   => __( 'Font Style', 'nevo' ),
				'choices' => array(
					''        => __( 'Default', 'nevo' ),
					'normal'  => __( 'Normal', 'nevo' ),
					'italic'  => __( 'Italic', 'nevo' ),
					'oblique' => __( 'Oblique', 'nevo' ),
				),
			),
			array(
				'name'    => 'text_decoration',
				'type'    => 'select',
				'label'   => __( 'Text Decoration', 'nevo' ),
				'choices' => array(
					''             => __( 'Default', 'nevo' ),
					'underline'    => __( 'Underline', 'nevo' ),
					'overline'     => __( 'Overline', 'nevo' ),
					'line-through' => __( 'Line through', 'nevo' ),
					'none'         => __( 'None', 'nevo' ),
				),
			),
			array(
				'name'    => 'text_transform',
				'type'    => 'select',
				'label'   => __( 'Text Transform', 'nevo' ),
				'choices' => array(
					''           => __( 'Default', 'nevo' ),
					'uppercase'  => __( 'Uppercase', 'nevo' ),
					'lowercase'  => __( 'Lowercase', 'nevo' ),
					'capitalize' => __( 'Capitalize', 'nevo' ),
					'none'       => __( 'None', 'nevo' ),
				),
			),
		);

		return $typo_fields;
	}

	/**
	 * Get styling field
	 *
	 * @return array
	 */
	function get_styling_config() {
		$fields = array(
			'tabs'          => array(
				'normal' => __( 'Normal', 'nevo' ),  // null or false to disable.
				'hover'  => __( 'Hover', 'nevo' ), // null or false to disable.
			),
			'normal_fields' => array(
				array(
					'name'       => 'primary',
					'type'       => 'color',
					'label'      => __( 'Primary Color', 'nevo' ),
					'css_format' => '--nvt-primary: {{value}};',
				),
				array(
					'name'       => 'secondary',
					'type'       => 'color',
					'label'      => __( 'Secondary Color', 'nevo' ),
					'css_format' => '--nvt-secondary: {{value}};',
				),
				array(
					'name'       => 'success',
					'type'       => 'color',
					'label'      => __( 'Success Color', 'nevo' ),
					'css_format' => '--nvt-success: {{value}};',
				),
				array(
					'name'       => 'info',
					'type'       => 'color',
					'label'      => __( 'Info Color', 'nevo' ),
					'css_format' => '--nvt-info: {{value}};',
				),
				array(
					'name'       => 'warning',
					'type'       => 'color',
					'label'      => __( 'Warning Color', 'nevo' ),
					'css_format' => '--nvt-warning: {{value}};',
				),
				array(
					'name'  => 'type_heading',
					'type'  => 'heading',
					'label' => __( 'Type Colors', 'nevo' ),
				),
				array(
					'name'       => 'link',
					'type'       => 'color',
					'label'      => __( 'Link Color', 'nevo' ),
					'css_format' => '--nvt-link: {{value}};',
				),
				array(
					'name'       => 'link_hover',
					'type'       => 'color',
					'label'      => __( 'Link Hover Color', 'nevo' ),
					'css_format' => '--nvt-link-hover: {{value}};',
				),
				array(
					'name'       => 'base',
					'type'       => 'color',
					'label'      => __( 'Base Color', 'nevo' ),
					'description' => __( 'Used for all normal texts.', 'nevo' ),
					'css_format' => '--nvt-base: {{value}};',
				),
				array(
					'name'       => 'title',
					'type'       => 'color',
					'label'      => __( 'Title Color', 'nevo' ),
					'description' => __( 'Used for post, page and Archive title', 'nevo' ),
					'css_format' => '--nvt-title: {{value}};',
				),
				array(
					'name'       => 'meta',
					'type'       => 'color',
					'label'      => __( 'Meta Color', 'nevo' ),
					'description' => __( 'Used for post, page, product and comment meta', 'nevo' ),
					'css_format' => '--nvt-meta: {{value}};',
				),
				array(
					'name'       => 'heading',
					'type'       => 'color',
					'label'      => __( 'Heading Color', 'nevo' ),
					'description' => __( 'Used for all headlines. (H1, H2, H3, H4, H5, H6.)', 'nevo' ),
					'css_format' => '--nvt-heading: {{value}};',
				),
				
				//
				array(
					'name'       => 'text_color',
					'type'       => 'color',
					'label'      => __( 'Color', 'nevo' ),
					'css_format' => 'color: {{value}}; text-decoration-color: {{value}};',
				),
				array(
					'name'       => 'link_color',
					'type'       => 'color',
					'label'      => __( 'Link Color', 'nevo' ),
					'css_format' => 'color: {{value}}; text-decoration-color: {{value}};',
				),

				array(
					'name'            => 'margin',
					'type'            => 'css_ruler',
					'device_settings' => true,
					'css_format'      => array(
						'top'    => 'margin-top: {{value}};',
						'right'  => 'margin-right: {{value}};',
						'bottom' => 'margin-bottom: {{value}};',
						'left'   => 'margin-left: {{value}};',
					),
					'label'           => __( 'Margin', 'nevo' ),
				),

				array(
					'name'            => 'padding',
					'type'            => 'css_ruler',
					'device_settings' => true,
					'css_format'      => array(
						'top'    => 'padding-top: {{value}};',
						'right'  => 'padding-right: {{value}};',
						'bottom' => 'padding-bottom: {{value}};',
						'left'   => 'padding-left: {{value}};',
					),
					'label'           => __( 'Padding', 'nevo' ),
				),
				
				array(
					'name'            => 'paddinga',
					'type'            => 'css_ruler',
					'device_settings' => true,
					'fields_disabled' => array(
						'left'  => 'nevo',
						'right' => 'nevo',
					),
					'css_format'      => array(
						'top'    => 'padding-top: {{value}};',
						'bottom' => 'padding-bottom: {{value}};',
					),
					'label'           => __( 'Padding', 'nevo' ),
				),

				array(
					'name'  => 'bg_heading',
					'type'  => 'heading',
					'label' => __( 'Background', 'nevo' ),
				),

				array(
					'name'       => 'bg_color',
					'type'       => 'color',
					'label'      => __( 'Background Color', 'nevo' ),
					'css_format' => 'background-color: {{value}};',
					'device_settings' => false,
				),
				array(
					'name'       => 'bg_image',
					'type'       => 'image',
					'label'      => __( 'Background Image', 'nevo' ),
					'css_format' => 'background-image: url("{{value}}");',
					'device_settings' => false,
				),
				array(
					'name'       => 'bg_cover',
					'type'       => 'select',
					'choices'    => array(
						''        => __( 'Default', 'nevo' ),
						'auto'    => __( 'Auto', 'nevo' ),
						'cover'   => __( 'Cover', 'nevo' ),
						'contain' => __( 'Contain', 'nevo' ),
					),
					'required'   => array( 'bg_image', 'not_empty', '' ),
					'label'      => __( 'Size', 'nevo' ),
					'class'      => 'field-half-left',
					'css_format' => '-webkit-background-size: {{value}}; -moz-background-size: {{value}}; -o-background-size: {{value}}; background-size: {{value}};',
				),
				array(
					'name'       => 'bg_position',
					'type'       => 'select',
					'label'      => __( 'Position', 'nevo' ),
					'required'   => array( 'bg_image', 'not_empty', '' ),
					'class'      => 'field-half-right',
					'choices'    => array(
						''              => __( 'Default', 'nevo' ),
						'center'        => __( 'Center', 'nevo' ),
						'top left'      => __( 'Top Left', 'nevo' ),
						'top right'     => __( 'Top Right', 'nevo' ),
						'top center'    => __( 'Top Center', 'nevo' ),
						'bottom left'   => __( 'Bottom Left', 'nevo' ),
						'bottom center' => __( 'Bottom Center', 'nevo' ),
						'bottom right'  => __( 'Bottom Right', 'nevo' ),
					),
					'css_format' => 'background-position: {{value}};',
				),
				array(
					'name'       => 'bg_repeat',
					'type'       => 'select',
					'label'      => __( 'Repeat', 'nevo' ),
					'class'      => 'field-half-left',
					'required'   => array(
						array( 'bg_image', 'not_empty', '' ),
					),
					'choices'    => array(
						'repeat'    => __( 'Default', 'nevo' ),
						'no-repeat' => __( 'No repeat', 'nevo' ),
						'repeat-x'  => __( 'Repeat horizontal', 'nevo' ),
						'repeat-y'  => __( 'Repeat vertical', 'nevo' ),
					),
					'css_format' => 'background-repeat: {{value}};',
				),

				array(
					'name'       => 'bg_attachment',
					'type'       => 'select',
					'label'      => __( 'Attachment', 'nevo' ),
					'class'      => 'field-half-right',
					'required'   => array(
						array( 'bg_image', 'not_empty', '' ),
					),
					'choices'    => array(
						''       => __( 'Default', 'nevo' ),
						'scroll' => __( 'Scroll', 'nevo' ),
						'fixed'  => __( 'Fixed', 'nevo' ),
					),
					'css_format' => 'background-attachment: {{value}};',
				),

				array(
					'name'  => 'border_heading',
					'type'  => 'heading',
					'label' => __( 'Border', 'nevo' ),
				),

				array(
					'name'       => 'border_style',
					'type'       => 'select',
					'class'      => 'clear',
					'label'      => __( 'Border Style', 'nevo' ),
					'default'    => '',
					'choices'    => array(
						''       => __( 'Default', 'nevo' ),
						'none'   => __( 'None', 'nevo' ),
						'solid'  => __( 'Solid', 'nevo' ),
						'dotted' => __( 'Dotted', 'nevo' ),
						'dashed' => __( 'Dashed', 'nevo' ),
						'double' => __( 'Double', 'nevo' ),
						'ridge'  => __( 'Ridge', 'nevo' ),
						'inset'  => __( 'Inset', 'nevo' ),
						'outset' => __( 'Outset', 'nevo' ),
					),
					'css_format' => 'border-style: {{value}};',
				),

				array(
					'name'       => 'border_width',
					'type'       => 'css_ruler',
					'label'      => __( 'Border Width', 'nevo' ),
					'required'   => array(
						array( 'border_style', '!=', 'none' ),
						array( 'border_style', '!=', '' ),
					),
					'css_format' => array(
						'top'    => 'border-top-width: {{value}};',
						'right'  => 'border-right-width: {{value}};',
						'bottom' => 'border-bottom-width: {{value}};',
						'left'   => 'border-left-width: {{value}};',
					),
				),
				array(
					'name'       => 'border_color',
					'type'       => 'color',
					'label'      => __( 'Border Color', 'nevo' ),
					'required'   => array(
						array( 'border_style', '!=', 'none' ),
						array( 'border_style', '!=', '' ),
					),
					'css_format' => 'border-color: {{value}};',
				),

				array(
					'name'       => 'border_radius',
					'type'       => 'css_ruler',
					'label'      => __( 'Border Radius', 'nevo' ),
					'css_format' => array(
						'top'    => 'border-top-left-radius: {{value}};',
						'right'  => 'border-top-right-radius: {{value}};',
						'bottom' => 'border-bottom-right-radius: {{value}};',
						'left'   => 'border-bottom-left-radius: {{value}};',
					),
				),

				array(
					'name'       => 'box_shadow',
					'type'       => 'shadow',
					'label'      => __( 'Box Shadow', 'nevo' ),
					'css_format' => 'box-shadow: {{value}};',
				),

			),

			'hover_fields' => array(
				array(
					'name'       => 'text_color',
					'type'       => 'color',
					'label'      => __( 'Color', 'nevo' ),
					'css_format' => 'color: {{value}}; text-decoration-color: {{value}};',
				),
				array(
					'name'       => 'link_color',
					'type'       => 'color',
					'label'      => __( 'Link Color', 'nevo' ),
					'css_format' => 'color: {{value}}; text-decoration-color: {{value}};',
				),
				array(
					'name'  => 'bg_heading',
					'type'  => 'heading',
					'label' => __( 'Background', 'nevo' ),
				),
				array(
					'name'       => 'bg_color',
					'type'       => 'color',
					'label'      => __( 'Background Color', 'nevo' ),
					'css_format' => 'background-color: {{value}};',
				),
				array(
					'name'  => 'border_heading',
					'type'  => 'heading',
					'label' => __( 'Border', 'nevo' ),
				),
				array(
					'name'       => 'border_style',
					'type'       => 'select',
					'label'      => __( 'Border Style', 'nevo' ),
					'default'    => '',
					'choices'    => array(
						''       => __( 'Default', 'nevo' ),
						'none'   => __( 'None', 'nevo' ),
						'solid'  => __( 'Solid', 'nevo' ),
						'dotted' => __( 'Dotted', 'nevo' ),
						'dashed' => __( 'Dashed', 'nevo' ),
						'double' => __( 'Double', 'nevo' ),
						'ridge'  => __( 'Ridge', 'nevo' ),
						'inset'  => __( 'Inset', 'nevo' ),
						'outset' => __( 'Outset', 'nevo' ),
					),
					'css_format' => 'border-style: {{value}};',
				),
				array(
					'name'       => 'border_width',
					'type'       => 'css_ruler',
					'label'      => __( 'Border Width', 'nevo' ),
					'required'   => array( 'border_style', '!=', 'none' ),
					'css_format' => array(
						'top'    => 'border-top-width: {{value}};',
						'right'  => 'border-right-width: {{value}};',
						'bottom' => 'border-bottom-width: {{value}};',
						'left'   => 'border-left-width: {{value}};',
					),
				),
				array(
					'name'       => 'border_color',
					'type'       => 'color',
					'label'      => __( 'Border Color', 'nevo' ),
					'required'   => array( 'border_style', '!=', 'none' ),
					'css_format' => 'border-color: {{value}};',
				),
				array(
					'name'       => 'border_radius',
					'type'       => 'css_ruler',
					'label'      => __( 'Border Radius', 'nevo' ),
					'css_format' => array(
						'top'    => 'border-top-left-radius: {{value}};',
						'right'  => 'border-top-right-radius: {{value}};',
						'bottom' => 'border-bottom-right-radius: {{value}};',
						'left'   => 'border-bottom-left-radius: {{value}};',
					),
				),
				array(
					'name'       => 'box_shadow',
					'type'       => 'shadow',
					'label'      => __( 'Box Shadow', 'nevo' ),
					'css_format' => 'box-shadow: {{value}};',
				),

			),

		);

		return apply_filters( 'nevo/get_styling_config', $fields );
	}

	/**
	 * Setup icon args
	 *
	 * @param array $icon
	 *
	 * @return array
	 */
	function setup_icon( $icon ) {
		if ( ! is_array( $icon ) ) {
			$icon = array();
		}

		return wp_parse_args(
			$icon,
			array(
				'type' => '',
				'icon' => '',
			)
		);
	}

	/**
	 * Get customize setting data
	 *
	 * @param string $key Setting name.
	 *
	 * @return bool|mixed
	 */
	function get_field_setting( $key ) {
		$config = self::get_config();
		if ( isset( $config[ 'setting|' . $key ] ) ) {
			return $config[ 'setting|' . $key ];
		}

		return false;
	}

	/**
	 * Get Media url from data
	 *
	 * @param string/array $value   media data.
	 * @param null|string  $size WordPress image size name.
	 *
	 * @return string|false Media url or empty
	 */
	function get_media( $value, $size = null ) {

		if ( empty( $value ) ) {
			return false;
		}

		if ( ! $size ) {
			$size = 'full';
		}

		if ( is_numeric( $value ) ) {
			$image_attributes = wp_get_attachment_image_src( $value, $size );
			if ( $image_attributes ) {
				return $image_attributes[0];
			} else {
				return false;
			}
		} elseif ( is_string( $value ) ) {
			$img_id = attachment_url_to_postid( $value );
			if ( $img_id ) {
				$image_attributes = wp_get_attachment_image_src( $img_id, $size );
				if ( $image_attributes ) {
					return $image_attributes[0];
				} else {
					return false;
				}
			}

			return $value;
		} elseif ( is_array( $value ) ) {
			$value = wp_parse_args(
				$value,
				array(
					'id'   => '',
					'url'  => '',
					'mime' => '',
				)
			);

			if ( empty( $value['id'] ) && empty( $value['url'] ) ) {
				return false;
			}

			$url = '';

			if ( isset($value['mime']) && strpos( $value['mime'], 'image/' ) !== false ) {
				$image_attributes = wp_get_attachment_image_src( $value['id'], $size );
				if ( $image_attributes ) {
					$url = $image_attributes[0];
				}
			} else {
				$url = wp_get_attachment_url( $value['id'] );
			}

			if ( ! $url ) {
				$url = $value['url'];
				if ( $url ) {
					$img_id = attachment_url_to_postid( $url );
					if ( $img_id ) {
						return wp_get_attachment_url( $img_id );
					}
				}
			}

			return $url;
		}

		return false;
	}

	/**
	 * Load support controls
	 */
	function load_controls() {
		$fields = array(
			'base',
			'select',
			'font',
			'font_style',
			'text_align',
			'text_align_no_justify',
			'checkbox',
			'css_ruler',
			'shadow',
			'icon',
			'slider',
			'color',
			'textarea',
			'radio',

			'media',
			'image',
			'video',

			'text',
			'hidden',
			'heading',
			'typography',
			'modal',
			'styling',
			'hr',

			'repeater',

			'pro',
		);

		$fields = apply_filters( 'nevo/customize/register-controls', $fields );

		foreach ( $fields as $k => $f ) {

			if ( is_numeric( $k ) ) {
				$field_type = $f;
				$file       = get_template_directory() . '/inc/classes/class-control-' . str_replace( '_', '-', $field_type ) . '.php';
			} else {
				$field_type = $k;
				$file       = $f;
			}

			if ( file_exists( $file ) ) {

				$control_class_name = 'Nevo_Customizer_Control_';
				$tpl_type           = str_replace( '_', ' ', $field_type );
				$tpl_type           = str_replace( ' ', '_', ucfirst( $tpl_type ) );
				$control_class_name .= $tpl_type;
				require_once $file;

				if ( $control_class_name ) {
					if ( method_exists( $control_class_name, 'field_template' ) ) {
						add_action(
							'customize_controls_print_footer_scripts',
							array(
								$control_class_name,
								'field_template',
							)
						);
					}
				}
			}
		}

	}


	/**
	 * Register Customize Settings
	 *
	 * @param WP_Customize_Manager $wp_customize Customize manager.
	 */
	function register( $wp_customize ) {

		// Custom panel.
		require_once get_template_directory() . '/inc/classes/class-customizer-panel.php';
		// Load custom section.
		require_once get_template_directory() . '/inc/classes/class-customizer-section.php';

		// Load custom section pro.
		require_once get_template_directory() . '/inc/classes/class-customizer-section-pro.php';

		// Register new panel and section type.
		$wp_customize->register_panel_type( 'Nevo_WP_Customize_Panel' );
		$wp_customize->register_section_type( 'Nevo_WP_Customize_Section' );

		// Register section type.
		$wp_customize->register_section_type( 'Nevo_WP_Customize_Section_Pro' );

		$this->load_controls();

		foreach ( self::get_config( $wp_customize ) as $args ) {
			switch ( $args['type'] ) {
				case 'panel':
					$name = $args['name'];
					unset( $args['name'] );
					if ( ! $args['title'] ) {
						$args['title'] = $args['label'];
					}
					if ( ! $name ) {
						$name = $args['title'];
					}
					unset( $args['name'] );
					unset( $args['type'] );
					$panel = new Nevo_WP_Customize_Panel( $wp_customize, $name, $args );
					$wp_customize->add_panel( $panel );
					break;
				case 'section':
					$name = $args['name'];
					unset( $args['name'] );
					if ( ! $args['title'] ) {
						$args['title'] = $args['label'];
					}
					if ( ! $name ) {
						$name = $args['title'];
					}
					unset( $args['name'] );
					unset( $args['type'] );
					if ( isset( $args['section_class'] ) && class_exists( $args['section_class'] ) ) { // Allow custom class.
						$wp_customize->add_section( new $args['section_class']( $wp_customize, $name, $args ) );
					} else {
						$wp_customize->add_section( new Nevo_WP_Customize_Section( $wp_customize, $name, $args ) );
					}

					break;
				default:
					switch ( $args['type'] ) {
						case 'image_select':
							$args['setting_type'] = 'radio';
							$args['field_class']  = 'custom-control-image_select' . ( $args['field_class'] ? ' ' . $args['field_class'] : '' );
							break;
						case 'radio_group':
							$args['setting_type'] = 'radio';
							$args['field_class']  = 'custom-control-radio_group' . ( $args['field_class'] ? ' ' . $args['field_class'] : '' );
							break;
						default:
							$args['setting_type'] = $args['type'];
					}

					$args['default_value']    = $args['default'];
					$settings_args            = array(
						'sanitize_callback'    => $args['sanitize_callback'],
						'sanitize_js_callback' => $args['sanitize_js_callback'],
						'theme_supports'       => $args['theme_supports'],
						'type'                 => $args['mod'],
					);
					$settings_args['default'] = apply_filters( 'nevo/customize/settings-default', $args['default'], $args['name'] );

					$settings_args['transport'] = 'refresh';
					if ( ! $settings_args['sanitize_callback'] ) {
						$settings_args['sanitize_callback'] = array(
							'Nevo_Sanitize_Input',
							'sanitize_customizer_input',
						);
					}

					foreach ( $settings_args as $k => $v ) {
						unset( $args[ $k ] );
					}

					unset( $args['mod'] );
					$name = $args['name'];
					unset( $args['name'] );

					unset( $args['type'] );
					if ( ! $args['label'] ) {
						$args['label'] = $args['title'];
					}

					$selective_refresh = null;
					if ( $args['selector'] && ( ( $args['render_callback'] || $args['render'] ) || $args['css_format'] ) ) {
						$selective_refresh = array(
							'selector'        => $args['selector'],
							'render_callback' => ( $args['render'] ) ? $args['render'] : $args['render_callback'],
						);

						if ( $args['css_format'] ) {
							$settings_args['transport'] = 'postMessage';
							$selective_refresh          = null;
						} else {
							$settings_args['transport'] = 'postMessage';
						}
					}
					unset( $args['default'] );

					$wp_customize->add_setting(
						$name,
						array_merge(
							array(
								'sanitize_callback' => array(
									'Nevo_Sanitize_Input',
									'sanitize_customizer_input',
								),
							),
							$settings_args
						)
					);

					$control_class_name = 'Nevo_Customizer_Control_';
					$tpl_type           = str_replace( '_', ' ', $args['setting_type'] );
					$tpl_type           = str_replace( ' ', '_', ucfirst( $tpl_type ) );
					$control_class_name .= $tpl_type;

					if ( 'js_raw' != $settings_args['type'] ) {
						if ( class_exists( $control_class_name ) ) {
							$wp_customize->add_control( new $control_class_name( $wp_customize, $name, $args ) );
						} else {
							$wp_customize->add_control( new Nevo_Customizer_Control_Base( $wp_customize, $name, $args ) );
						}
					}

					if ( $selective_refresh ) {
						$s_id = $selective_refresh['render_callback'];
						if ( is_array( $s_id ) ) {
							if ( is_string( $s_id[0] ) ) {
								$__id = $s_id[0] . '__' . $s_id[1];
							} else {
								$__id = get_class( $s_id[0] ) . '__' . $s_id[1];
							}
						} else {
							$__id = $s_id;
						}
						if ( ! isset( $this->selective_settings[ $__id ] ) ) {
							$this->selective_settings[ $__id ] = array(
								'settings'            => array(),
								'selector'            => $selective_refresh['selector'],
								'container_inclusive' => ( strpos( $__id, 'Nevo_Customizer_Auto_CSS' ) === false ) ? true : false,
								'render_callback'     => $s_id,
							);
						}

						$this->selective_settings[ $__id ]['settings'][] = $name;
					}

					break;
			}// end switch.
		} // End loop config.

		// Remove partial for default custom logo and add this by theme custom.
		$wp_customize->selective_refresh->remove_partial( 'custom_logo' );

		$wp_customize->get_section( 'title_tagline' )->panel        = 'nevo_header';
		$wp_customize->get_section( 'title_tagline' )->title        = __( 'Logo & Site Identity', 'nevo' );
		$wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
		// Add selective refresh.
		$wp_customize->get_setting( 'custom_logo' )->transport     = 'postMessage';
		$wp_customize->get_setting( 'blogname' )->transport        = 'postMessage';
		$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
		
		$wp_customize->get_section( 'title_tagline' )->priority   = 35;
		
		$wp_customize->get_control( 'custom_logo' )->priority     = 6;
		$wp_customize->get_control( 'blogname' )->priority        = 1;
		$wp_customize->get_control( 'blogdescription' )->priority = 4;
		
		if ( Nevo()->is_woocommerce_active() ) {
			$wp_customize->get_panel( 'woocommerce' )->priority = 50;
		}
		foreach ( $this->selective_settings as $cb => $settings ) {
			reset( $settings['settings'] );
			$settings = apply_filters( $cb, $settings );
			$wp_customize->selective_refresh->add_partial( $cb, $settings );
		}

		// For live CSS.
		$wp_customize->add_setting(
			'nevo__css',
			array(
				'default'           => '',
				'transport'         => 'postMessage',
				'sanitize_callback' => array( 'Nevo_Sanitize_Input', 'sanitize_css_code' ),
			)
		);

		do_action( 'nevo/customize/register_completed', $this );
	}

}
