<?php

/**
 * Control for colors
 *
 * @package Kubelio\\Customizer
 * @since 1.0.0
 */

namespace Kubelio\Customizer;

use Kirki\Control\Headline;

if (! defined('ABSPATH')) {
	exit;
}

class Color {

	use Helper;

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action('after_setup_theme', array($this, 'register_fields'));
	}

	/**
	 * Register Color related customizer fields
	 */
	public function register_fields() {
		$this->set_section();
		$this->add_global_colors();
		$this->add_status_colors();
		$this->add_button_colors();
		$this->add_off_canvas_colors();
	}

	/**
	 * Set sections
	 */
	public function set_section() {
		$this->add_section(
			'general_color',
			array(
				'title'    => esc_html__('Colors', 'kubelio'),
				'panel'    => 'general',
				'priority' => 15,
			)
		);

		$this->add_section(
			'general_button',
			array(
				'title'    => esc_html__('Button', 'kubelio'),
				'panel'    => 'general',
				'priority' => 20,
			)
		);

		$this->add_section(
			'general_off_canvas',
			array(
				'title'       => esc_html__('Off-Canvas', 'kubelio'),
				'description' => esc_html__('Configure the visual styles for off-canvas components, including mobile navigation, submenus, and interactive dropdowns that reveal themselves upon user action.', 'kubelio'),
				'panel'       => 'general',
				'priority'    => 25,
			)
		);
	}

	/**
	 * Add global color fields
	 */
	public function add_global_colors() {
		$section = 'general_color';

		$this->add_field([
			'type'    => 'headline',
			'label' => esc_html__('Global Colors', 'kubelio'),
			'settings' => 'headline_' . uniqid(),
			'section' => $section,
		]);

		// Brand
		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Primary Color', 'kubelio'),
				'settings'  => 'color_primary',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['primary'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--primary',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field([
			'type'    => 'divider',
			'section' => $section,
		]);

		// Font Colors
		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Heading Color', 'kubelio'),
				'settings'  => 'color_heading',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['heading'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--heading',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Text Color', 'kubelio'),
				'settings'  => 'color_body',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['body'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--body',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Meta Color', 'kubelio'),
				'settings'  => 'color_meta',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['meta'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--meta',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field([
			'type'    => 'divider',
			'section' => $section,
		]);

		// Link Colors
		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Link Color', 'kubelio'),
				'settings'  => 'color_link',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['text-link'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--text-link',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'        => 'color',
				'choices'     => array('alpha' => true),
				'label'       => esc_html__('Link Accent Color', 'kubelio'),
				'description' => esc_html__('Set the color when hovering over the element.', 'kubelio'),
				'settings'    => 'color_link_active',
				'section'     => $section,
				'default'     => kubelio_get_theme('color')['text-link'],
				'output'      => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--text-link-active',
					),
				),
				'transport'   => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'     => 'divider',
				'settings' => 'divider_' . uniqid(),
				'section'  => $section,
			)
		);

		// Background Colors
		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Background Color', 'kubelio'),
				'settings'  => 'color_background',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['background'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--background',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Surface Color', 'kubelio'),
				'settings'  => 'color_surface',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['surface'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--surface',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'     => 'divider',
				'settings' => 'divider_' . uniqid(),
				'section'  => $section,
			)
		);

		// Border & UI Colors
		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Border Color', 'kubelio'),
				'settings'  => 'color_border',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['outline'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme, .is-style-base',
						'property' => '--wp--preset--color--outline',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'     => 'divider',
				'settings' => 'divider_' . uniqid(),
				'section'  => $section,
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Highlight Color', 'kubelio'),
				'settings'  => 'color_hightlight',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['highlight'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--preset--color--highlight',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'     => 'divider',
				'settings' => 'divider_' . uniqid(),
				'section'  => $section,
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Backdrop Color', 'kubelio'),
				'settings'  => 'color_backdrop',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['overlay'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--color--overlay',
					),
				),
				'transport' => 'auto',
			)
		);
	}

	/**
	 * Add status color fields
	 */
	public function add_status_colors() {
		$section = 'general_color';

		$this->add_field([
			'type'    => 'headline',
			'label'   => esc_html__('Sematic Colors', 'kubelio'),
			'settings' => 'headline_' . uniqid(),
			'section' => $section,
		]);

		$this->add_field(
			array(
				'type'      => 'color',
				'label'     => esc_html__('Info Color', 'kubelio'),
				'settings'  => 'color_info',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['info'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--color--info',
					),
				),
				'transport' => 'auto',
			)
		);

		// Success
		$this->add_field(
			array(
				'type'      => 'color',
				'label'     => esc_html__('Success Color', 'kubelio'),
				'settings'  => 'color_success',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['success'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--color--success',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'label'     => esc_html__('Warning Color', 'kubelio'),
				'settings'  => 'color_warning',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['warning'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--color--warning',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'label'     => esc_html__('Danger Color', 'kubelio'),
				'settings'  => 'color_danger',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['danger'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--color--danger',
					),
				),
				'transport' => 'auto',
			)
		);
	}

	/**
	 * Add button color fields
	 */
	public function add_button_colors() {
		$section = 'general_button';

		$this->add_field([
			'type'    => 'headline',
			'label'   => esc_html__('Button Primary', 'kubelio'),
			'settings' => 'headline_' . uniqid(),
			'section' => $section,
		]);

		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Text Color', 'kubelio'),
				'settings'  => 'button_color',
				'section'   => $section,
				'default'   => kubelio_get_theme('button')['primary']['color'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--button--color',
					),
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--button-action--color',
					),
				),
				'transport' => 'auto',
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'choices'   => array('alpha' => true),
				'label'     => esc_html__('Background Color', 'kubelio'),
				'settings'  => 'button_background',
				'section'   => $section,
				'default'   => kubelio_get_theme('button')['primary']['background'],
				'output'    => array(
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--button--background',
					),
					array(
						'element'  => '.kubelio-default-scheme',
						'property' => '--wp--custom--button-action--background',
					),
				),
				'transport' => 'auto',
			)
		);
	}

	/**
	 * Add off-canvas color fields
	 */
	public function add_off_canvas_colors() {
		$section = 'general_off_canvas';

		$this->add_field(
			array(
				'type'      => 'color',
				'label'     => esc_html__('Text Color', 'kubelio'),
				'choices'   => array('alpha' => true),
				'settings'  => 'offcanvas_color',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['body-alt'],
				'transport' => 'auto',
				'output'    => array(
					array(
						'element'  => '.is-style-offcanvas',
						'property' => '--wp--preset--color--body',
					),
					array(
						'element'  => '.is-style-offcanvas',
						'property' => '--wp--preset--color--text-link',
					),
					array(
						'element'  => '.is-style-offcanvas',
						'property' => '--wp--preset--color--text-link-active',
					),
					array(
						'element'  => '.site-header .sub-menu',
						'property' => '--menu-color',
					),
				),
			)
		);

		$this->add_field(
			array(
				'type'      => 'color',
				'label'     => esc_html__('Background Color', 'kubelio'),
				'choices'   => array('alpha' => true),
				'settings'  => 'offcanvas_background',
				'section'   => $section,
				'default'   => kubelio_get_theme('color')['background-alt'],
				'transport' => 'auto',
				'output'    => array(
					array(
						'element'  => '.is-style-offcanvas',
						'property' => '--wp--preset--color--background',
					),
					array(
						'element'  => '.site-header .sub-menu',
						'property' => '--wp--preset--color--background',
					),
				),
			)
		);
	}
}
