<?php
namespace Sifency\Traits;

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

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

trait Sifency_Addons_Combine_Title_Controls {

	/**
	 * 1. CONTENT CONTROLS
	 */
	protected function add_sifency_addons_combine_title_content_controls( $args = array() ) {
		$args = wp_parse_args(
			$args,
			array(
				'prefix'    => 'title_',
				'label'     => esc_html__( 'Title', 'sifency-addons' ),
				'show_tag'  => true,
				'show_link' => true,
				'condition' => array(),
			)
		);

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

		$this->start_controls_section(
			$prefix . 'content_section',
			array(
				'label'     => $args['label'],
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => $args['condition'],
			)
		);

		$this->add_control(
			$prefix . 'enable',
			array(
				'label'        => esc_html__( 'Enable Title', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
				'label_off'    => esc_html__( 'No', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			$prefix . 'text',
			array(
				'label'     => esc_html__( 'Title Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXTAREA,
				'rows'      => 3,
				'default'   => esc_html__( 'Enter your title here', 'sifency-addons' ),
				'condition' => array( $prefix . 'enable' => 'yes' ),
			)
		);

		if ( $args['show_tag'] ) {
			$this->add_control(
				$prefix . 'tag',
				array(
					'label'     => esc_html__( 'HTML Tag', 'sifency-addons' ),
					'type'      => Controls_Manager::SELECT,
					'options'   => array(
						'h1'   => 'H1',
						'h2'   => 'H2',
						'h3'   => 'H3',
						'h4'   => 'H4',
						'h5'   => 'H5',
						'h6'   => 'H6',
						'div'  => 'div',
						'span' => 'span',
						'p'    => 'p',
					),
					'default'   => 'h2',
					'condition' => array( $prefix . 'enable' => 'yes' ),
				)
			);
		}

		if ( $args['show_link'] ) {
			$this->add_control(
				$prefix . 'enable_link',
				array(
					'label'        => esc_html__( 'Enable Link', 'sifency-addons' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html__( 'Yes', 'sifency-addons' ),
					'label_off'    => esc_html__( 'No', 'sifency-addons' ),
					'return_value' => 'yes',
					'default'      => '',
					'condition'    => array( $prefix . 'enable' => 'yes' ),
				)
			);

			$this->add_control(
				$prefix . 'link',
				array(
					'label'       => esc_html__( 'Link', 'sifency-addons' ),
					'type'        => Controls_Manager::URL,
					'placeholder' => 'https://your-link.com',
					'default'     => array( 'url' => '' ),
					'condition'   => array(
						$prefix . 'enable'      => 'yes',
						$prefix . 'enable_link' => 'yes',
					),
				)
			);
		}

		$this->add_control(
			$prefix . 'icon',
			array(
				'label'     => esc_html__( 'Icon', 'sifency-addons' ),
				'type'      => Controls_Manager::ICONS,
				'condition' => array( $prefix . 'enable' => 'yes' ),
			)
		);

		$this->add_control(
			$prefix . 'icon_pos',
			array(
				'label'     => esc_html__( 'Icon Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'after',
				'options'   => array(
					'before' => esc_html__( 'Before', 'sifency-addons' ),
					'after'  => esc_html__( 'After', 'sifency-addons' ),
				),
				'condition' => array(
					$prefix . 'enable'       => 'yes',
					$prefix . 'icon[value]!' => '',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * 2. STYLE CONTROLS
	 */
	protected function add_Sifency_Addons_Combine_Title_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_rotate'     => false,
			'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 = 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 . ', ' . $full_base_sel . ' a';
		}

		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, ' . $full_base_sel . ' a: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_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; text-decoration: none;' ),
				)
			);

			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 ( ! in_array( 'wrapper', $exclude, true ) ) {
			$this->end_controls_section();
		}
	}

	/**
	 * 3. RENDER FUNCTION
	 */
	protected function add_sifency_addons_combine_title_render( $settings, $prefix = 'title_', $class = '' ) {
		$prefix = rtrim( $prefix, '_' ) . '_';

		if ( 'yes' !== ( $settings[ $prefix . 'enable' ] ?? '' ) ) {
			return;
		}

		$allowed_tags = array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p' );
		$html_tag     = in_array( $settings[ $prefix . 'tag' ] ?? 'h2', $allowed_tags ) ? $settings[ $prefix . 'tag' ] : 'h2';

		$this->add_render_attribute( $prefix . 'wrapper', 'class', array( 'sifency-title-style', $class ) );

		$is_link_enabled = ( 'yes' === ( $settings[ $prefix . 'enable_link' ] ?? '' ) );
		$has_link_url    = ! empty( $settings[ $prefix . 'link' ]['url'] );
		$use_link        = $is_link_enabled && $has_link_url;

		if ( $use_link ) {
			$this->add_link_attributes( $prefix . 'link_attr', $settings[ $prefix . 'link' ] );
			$this->add_render_attribute( $prefix . 'link_attr', 'class', 'sifency-title-link' );
		}

		$icon_pos = $settings[ $prefix . 'icon_pos' ] ?? 'after';
		$has_icon = ! empty( $settings[ $prefix . 'icon' ]['value'] );

		echo '<' . esc_attr( $html_tag ) . ' ' . wp_kses_post( $this->get_render_attribute_string( $prefix . 'wrapper' ) ) . '>';

		if ( $use_link ) {
			echo '<a ' . wp_kses_post( $this->get_render_attribute_string( $prefix . 'link_attr' ) ) . '>';
		}

		if ( $has_icon && 'before' === $icon_pos ) {
			echo '<span class="sifency-title-icon icon-before">';
			Icons_Manager::render_icon( $settings[ $prefix . 'icon' ], array( 'aria-hidden' => 'true' ) );
			echo '</span>';
		}

		if ( ! empty( $settings[ $prefix . 'text' ] ) ) {
			echo '<span class="sifency-title-text">' . wp_kses_post( $settings[ $prefix . 'text' ] ) . '</span>';
		}

		if ( $has_icon && 'after' === $icon_pos ) {
			echo '<span class="sifency-title-icon icon-after">';
			Icons_Manager::render_icon( $settings[ $prefix . 'icon' ], array( 'aria-hidden' => 'true' ) );
			echo '</span>';
		}

		if ( $use_link ) {
			echo '</a>';
		}

		echo '</' . esc_attr( $html_tag ) . '>';
	}
}
