<?php
namespace Sifency\Traits;

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

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;

trait Sifency_Addons_Master_Typography {

	protected function add_sifency_addons_master_typography_controls( $args = array() ) {

		$defaults = array(
			'prefix'               => 'custom_',
			'label'                => esc_html__( 'Typography', 'sifency-addons' ),
			'selector'             => '',
			'hover_selector'       => '',
			'target_children'      => false,
			'tab'                  => Controls_Manager::TAB_STYLE,
			'condition'            => array(),
			'show_alignment'       => true,
			'show_shadow'          => true,
			'show_box_model'       => true,
			'show_typography'      => true,
			'show_transition'      => true,
			'show_max_width'       => false,
			'show_min_width'       => false,
			'show_rotate'          => false,
			'show_background_blur' => false,
			'show_blend_mode'      => false,
			'show_indent'          => true,
			'color_only'           => false,
			'no_color'             => false,
			'exclude'              => array(),
			'hover_tab_label'      => esc_html__( 'Hover', 'sifency-addons' ),
		);

		$args   = wp_parse_args( $args, $defaults );
		$prefix = rtrim( $args['prefix'], '_' ) . '_';

		$base_sel      = $args['selector'];
		$full_base_sel = ( strpos( $base_sel, '{{WRAPPER}}' ) === false ) ? '{{WRAPPER}} ' . $base_sel : $base_sel;

		if ( $args['target_children'] ) {
			$tags = is_array( $args['target_children'] ) ? $args['target_children'] : array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'div', 'span', 'a', 'strong', 'i', 'b', 'label' );
			$list = array();
			foreach ( $tags as $tag ) {
				$list[] = $full_base_sel . ' ' . $tag;
			}
			$list[]        = $full_base_sel;
			$text_selector = implode( ',', $list );
		} else {
			$text_selector = $full_base_sel;
		}

		if ( ! empty( $args['hover_selector'] ) ) {
			$hover_trigger        = ( strpos( $args['hover_selector'], '{{WRAPPER}}' ) === false ) ? '{{WRAPPER}} ' . $args['hover_selector'] : $args['hover_selector'];
			$hover_selector_final = $hover_trigger;
		} else {
			$hover_trigger = $full_base_sel . ':hover';
			if ( $args['target_children'] ) {
				$hover_selector_final = implode( ',', array_map( fn( $s ) => $s . ':hover', explode( ',', $text_selector ) ) );
			} else {
				$hover_selector_final = $full_base_sel . ':hover';
			}
		}

		$exclude   = (array) $args['exclude'];
		$show_tabs = ! in_array( 'tabs', $exclude, true );

		if ( ! in_array( 'wrapper', $exclude, true ) ) {
			$this->start_controls_section(
				$prefix . 'section',
				array(
					'label'     => $args['label'],
					'tab'       => $args['tab'],
					'condition' => $args['condition'],
				)
			);}

		if ( $args['show_max_width'] && ! in_array( 'max_width', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'max_width',
				array(
					'label'      => esc_html__( 'Max Width', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', '%', 'em', 'rem', 'vw' ),
					'selectors'  => array( $full_base_sel => 'max-width: {{SIZE}}{{UNIT}}; margin-left:auto; margin-right:auto;' ),
				)
			);
		}
		if ( $args['show_min_width'] && ! in_array( 'min_width', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'min_width',
				array(
					'label'      => esc_html__( 'Min Width', 'sifency-addons' ),
					'type'       => Controls_Manager::SLIDER,
					'size_units' => array( 'px', '%', 'em', 'rem', 'vw' ),
					'selectors'  => array( $full_base_sel => 'min-width: {{SIZE}}{{UNIT}};' ),
				)
			);
		}

		if ( $args['show_alignment'] && ! in_array( 'alignment', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'align',
				array(
					'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
					'type'      => Controls_Manager::CHOOSE,
					'options'   => array(
						'left'    => array(
							'title' => 'Left',
							'icon'  => 'eicon-text-align-left',
						),
						'center'  => array(
							'title' => 'Center',
							'icon'  => 'eicon-text-align-center',
						),
						'right'   => array(
							'title' => 'Right',
							'icon'  => 'eicon-text-align-right',
						),
						'justify' => array(
							'title' => 'Justify',
							'icon'  => 'eicon-text-align-justify',
						),
					),
					'selectors' => array( $full_base_sel => 'text-align: {{VALUE}};' ),
				)
			);
		}

		if ( $args['show_typography'] && ! in_array( 'typography', $exclude, true ) ) {
			$this->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'     => $prefix . 'typography',
					'selector' => $text_selector,
				)
			);
			if ( $args['show_shadow'] && ! in_array( 'shadow', $exclude, true ) ) {
				$this->add_group_control(
					Group_Control_Text_Shadow::get_type(),
					array(
						'name'     => $prefix . 'text_shadow',
						'selector' => $text_selector,
					)
				);
			}
		}

		if ( $show_tabs ) {
			$this->start_controls_tabs( $prefix . 'tabs' );
			$this->start_controls_tab( $prefix . 'normal_tab', array( 'label' => esc_html__( 'Normal', 'sifency-addons' ) ) );
		}

		if ( ! $args['no_color'] && ! in_array( 'color', $exclude, true ) ) {

			if ( ! $args['color_only'] && ! in_array( 'gradient', $exclude, true ) ) {
				$this->add_control(
					$prefix . 'fill_type',
					array(
						'label'   => esc_html__( 'Text Fill', 'sifency-addons' ),
						'type'    => Controls_Manager::SELECT,
						'default' => 'color',
						'options' => array(
							'color'    => 'Solid Color',
							'gradient' => 'Gradient / Image',
						),
					)
				);
			}

			$this->add_control(
				$prefix . 'color',
				array(
					'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
					'type'      => Controls_Manager::COLOR,
					'condition' => ( $args['color_only'] || in_array( 'gradient', $exclude, true ) ) ? array() : array( $prefix . 'fill_type' => 'color' ),
					'selectors' => array( $text_selector => 'color: {{VALUE}}; background-image: none; -webkit-background-clip: unset; -webkit-text-fill-color: currentColor;' ),
				)
			);

			if ( ! $args['color_only'] && ! in_array( 'gradient', $exclude, true ) ) {
				$this->add_group_control(
					Group_Control_Background::get_type(),
					array(
						'name'           => $prefix . 'gradient_fill',
						'types'          => array( 'gradient', 'classic' ),
						'selector'       => $text_selector,
						'condition'      => array( $prefix . 'fill_type' => 'gradient' ),
						'fields_options' => array(
							'background' => array( 'selectors' => array( '{{SELECTOR}}' => 'background-color:transparent;-webkit-background-clip:text;-webkit-text-fill-color:transparent;' ) ),
						),
					)
				);
			}
		}

		if ( ! in_array( 'stroke', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'stroke_sw',
				array(
					'label' => esc_html__( 'Stroke', 'sifency-addons' ),
					'type'  => Controls_Manager::SWITCHER,
				)
			);
			$this->add_control(
				$prefix . 'stroke_c',
				array(
					'label'     => 'Color',
					'type'      => Controls_Manager::COLOR,
					'selectors' => array( $text_selector => '-webkit-text-stroke-color: {{VALUE}};' ),
					'condition' => array( $prefix . 'stroke_sw' => 'yes' ),
				)
			);
			$this->add_responsive_control(
				$prefix . 'stroke_w',
				array(
					'label'     => 'Width',
					'type'      => Controls_Manager::SLIDER,
					'selectors' => array( $text_selector => '-webkit-text-stroke-width: {{SIZE}}{{UNIT}};' ),
					'condition' => array( $prefix . 'stroke_sw' => 'yes' ),
				)
			);
		}

		if ( $show_tabs ) {
			$this->end_controls_tab();

			$this->start_controls_tab( $prefix . 'hover_tab', array( 'label' => $args['hover_tab_label'] ) );

			if ( ! $args['no_color'] && ! in_array( 'color', $exclude, true ) ) {
				if ( ! $args['color_only'] && ! in_array( 'gradient', $exclude, true ) ) {
					$this->add_control(
						$prefix . 'hover_fill_type',
						array(
							'label'   => esc_html__( 'Text Fill', 'sifency-addons' ),
							'type'    => Controls_Manager::SELECT,
							'default' => 'color',
							'options' => array(
								'color'    => 'Solid Color',
								'gradient' => 'Gradient / Image',
							),
						)
					);
				}

				$this->add_control(
					$prefix . 'hover_color',
					array(
						'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
						'type'      => Controls_Manager::COLOR,
						'condition' => ( $args['color_only'] || in_array( 'gradient', $exclude, true ) ) ? array() : array( $prefix . 'hover_fill_type' => 'color' ),
						'selectors' => array( $hover_selector_final => 'color: {{VALUE}}; background-image: none; -webkit-background-clip: unset; -webkit-text-fill-color: currentColor;' ),
					)
				);

				if ( ! $args['color_only'] && ! in_array( 'gradient', $exclude, true ) ) {
					$this->add_group_control(
						Group_Control_Background::get_type(),
						array(
							'name'           => $prefix . 'hover_gradient_fill',
							'types'          => array( 'gradient', 'classic' ),
							'selector'       => $hover_selector_final,
							'condition'      => array( $prefix . 'hover_fill_type' => 'gradient' ),
							'fields_options' => array( 'background' => array( 'selectors' => array( '{{SELECTOR}}' => 'background-color:transparent;-webkit-background-clip:text;-webkit-text-fill-color:transparent;' ) ) ),
						)
					);
				}
			}

			if ( ! in_array( 'stroke', $exclude, true ) ) {
				$this->add_control(
					$prefix . 'hover_stroke_sw',
					array(
						'label' => 'Stroke',
						'type'  => Controls_Manager::SWITCHER,
					)
				);
				$this->add_control(
					$prefix . 'hover_stroke_c',
					array(
						'label'     => 'Color',
						'type'      => Controls_Manager::COLOR,
						'selectors' => array( $hover_selector_final => '-webkit-text-stroke-color: {{VALUE}};' ),
						'condition' => array( $prefix . 'hover_stroke_sw' => 'yes' ),
					)
				);
				$this->add_responsive_control(
					$prefix . 'hover_stroke_w',
					array(
						'label'     => 'Width',
						'type'      => Controls_Manager::SLIDER,
						'selectors' => array( $hover_selector_final => '-webkit-text-stroke-width: {{SIZE}}{{UNIT}};' ),
						'condition' => array( $prefix . 'hover_stroke_sw' => 'yes' ),
					)
				);
			}

			if ( $args['show_box_model'] && ! in_array( 'box_model', $exclude, true ) ) {
				if ( ! in_array( 'bg', $exclude, true ) ) {
					$this->add_group_control(
						Group_Control_Background::get_type(),
						array(
							'name'      => $prefix . 'hover_box_bg',
							'label'     => 'Box Background',
							'condition' => ( $args['color_only'] || in_array( 'gradient', $exclude, true ) ) ? array() : array( $prefix . 'fill_type' => 'color' ),
							'selector'  => $hover_trigger,
						)
					);}
				if ( ! in_array( 'border', $exclude, true ) ) {
					$this->add_group_control(
						Group_Control_Border::get_type(),
						array(
							'name'     => $prefix . 'hover_box_border',
							'selector' => $hover_trigger,
						)
					);}
			}

			$this->end_controls_tab();
			$this->end_controls_tabs();
		}

		if ( $args['show_box_model'] && ! in_array( 'box_model', $exclude, true ) ) {
			if ( ! in_array( 'bg', $exclude, true ) ) {
				$this->add_group_control(
					Group_Control_Background::get_type(),
					array(
						'name'      => $prefix . 'box_bg',
						'condition' => ( $args['color_only'] || in_array( 'gradient', $exclude, true ) ) ? array() : array( $prefix . 'fill_type' => 'color' ),
						'label'     => 'Box Background',
						'selector'  => $full_base_sel,
					)
				);}
			if ( ! in_array( 'border', $exclude, true ) ) {
				$this->add_group_control(
					Group_Control_Border::get_type(),
					array(
						'name'     => $prefix . 'box_border',
						'selector' => $full_base_sel,
					)
				);}
			if ( ! in_array( 'radius', $exclude, true ) ) {
				$this->add_responsive_control(
					$prefix . 'box_radius',
					array(
						'label'     => 'Border Radius',
						'type'      => Controls_Manager::DIMENSIONS,
						'selectors' => array( $full_base_sel => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
					)
				);}
			if ( ! in_array( 'padding', $exclude, true ) ) {
				$this->add_responsive_control(
					$prefix . 'padding',
					array(
						'label'     => 'Padding',
						'type'      => Controls_Manager::DIMENSIONS,
						'selectors' => array( $full_base_sel => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
					)
				);}
			if ( ! in_array( 'margin', $exclude, true ) ) {
				$this->add_responsive_control(
					$prefix . 'margin',
					array(
						'label'     => 'Margin',
						'type'      => Controls_Manager::DIMENSIONS,
						'selectors' => array( $full_base_sel => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
					)
				);}
		}

		if ( $args['show_transition'] && ! in_array( 'transition', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'transition',
				array(
					'label'     => 'Transition',
					'type'      => Controls_Manager::SLIDER,
					'range'     => array(
						'px' => array(
							'max'  => 3,
							'step' => 0.1,
						),
					),
					'default'   => array( 'size' => 0.3 ),
					'selectors' => array(
						$full_base_sel => 'transition: all {{SIZE}}s ease;',
						$text_selector => 'transition: all {{SIZE}}s ease;',
					),
				)
			);
		}
		if ( $args['show_rotate'] && ! in_array( 'rotate', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'rotate',
				array(
					'label'     => 'Rotate',
					'type'      => Controls_Manager::SLIDER,
					'range'     => array(
						'px' => array(
							'max'  => 360,
							'step' => 1,
						),
					),
					'selectors' => array(
						$full_base_sel => 'transform: rotate({{SIZE}}deg);',
						$text_selector => 'transform: rotate({{SIZE}}deg);',
					),
				)
			);
		}
		if ( $args['show_background_blur'] && ! in_array( 'background_blur', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'background_blur',
				array(
					'label'      => esc_html__( 'Background Blur', 'sifency-addons' ),
					'type'       => \Elementor\Controls_Manager::SLIDER,
					'size_units' => array( 'px' ),
					'range'      => array(
						'px' => array(
							'min'  => 0,
							'max'  => 100,
							'step' => 1,
						),
					),
					'selectors'  => array(
						$full_base_sel => 'backdrop-filter: blur({{SIZE}}{{UNIT}}); -webkit-backdrop-filter: blur({{SIZE}}{{UNIT}});',
					),
				)
			);
		}

		if ( $args['show_blend_mode'] && ! in_array( 'blend_mode', $exclude, true ) ) {
			$this->add_control(
				$prefix . 'blend_mode',
				array(
					'label'     => esc_html__( 'Blend Mode', 'sifency-addons' ),
					'type'      => Controls_Manager::SELECT,
					'options'   => array(
						''            => esc_html__( 'Normal', 'sifency-addons' ),
						'multiply'    => esc_html__( 'Multiply', 'sifency-addons' ),
						'screen'      => esc_html__( 'Screen', 'sifency-addons' ),
						'overlay'     => esc_html__( 'Overlay', 'sifency-addons' ),
						'darken'      => esc_html__( 'Darken', 'sifency-addons' ),
						'lighten'     => esc_html__( 'Lighten', 'sifency-addons' ),
						'color-dodge' => esc_html__( 'Color Dodge', 'sifency-addons' ),
						'color-burn'  => esc_html__( 'Color Burn', 'sifency-addons' ),
						'hard-light'  => esc_html__( 'Hard Light', 'sifency-addons' ),
						'soft-light'  => esc_html__( 'Soft Light', 'sifency-addons' ),
						'difference'  => esc_html__( 'Difference', 'sifency-addons' ),
						'exclusion'   => esc_html__( 'Exclusion', 'sifency-addons' ),
						'hue'         => esc_html__( 'Hue', 'sifency-addons' ),
						'saturation'  => esc_html__( 'Saturation', 'sifency-addons' ),
						'color'       => esc_html__( 'Color', 'sifency-addons' ),
						'luminosity'  => esc_html__( 'Luminosity', 'sifency-addons' ),
					),
					'selectors' => array(
						$full_base_sel => 'mix-blend-mode: {{VALUE}};',
					),
				)
			);
		}
		if ( $args['show_indent'] && ! in_array( 'indent', $exclude, true ) ) {
			$this->add_responsive_control(
				$prefix . 'indent',
				array(
					'label'      => esc_html__( 'Indent', 'sifency-addons' ),
					'type'       => \Elementor\Controls_Manager::SLIDER,
					'size_units' => array( 'px' ),
					'range'      => array(
						'px' => array(
							'min'  => 0,
							'max'  => 100,
							'step' => 1,
						),
					),
					'selectors'  => array(
						$full_base_sel => 'text-indent: {{SIZE}}{{UNIT}};',
					),
				)
			);
		}
		if ( ! in_array( 'wrapper', $exclude, true ) ) {
			$this->end_controls_section();}
	}
}
