<?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_Core_Text_Style {

	protected function add_sifency_advanced_text_style( $args = [] ) {
		$defaults = [
			'prefix'          => 'custom_style_',
			'label'           => 'Custom Style',
			'selector'        => '', 
			'target_children' => true,
			'show_box_model'  => true,
			'tab'             => Controls_Manager::TAB_STYLE,
		];

		$args = wp_parse_args( $args, $defaults );
		$prefix   = $args['prefix'];
		
		$base_sel = $args['selector'];
		
		$full_base_sel = '{{WRAPPER}} ' . $base_sel;

		if ( $args['target_children'] ) {
			$tags = [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'div', 'span', 'a', 'strong', 'i', 'b' ];
			$target_selectors = [];
			foreach ( $tags as $tag ) {
				$target_selectors[] = $full_base_sel . ' ' . $tag;
			}
			$target_selectors[] = $full_base_sel;
			
			$text_selector_string = implode( ',', $target_selectors );
			$hover_selector_string = implode( ',', array_map( function($s) { return $s . ':hover'; }, $target_selectors ) );
		} else {
			$text_selector_string  = $full_base_sel;
			$hover_selector_string = $full_base_sel . ':hover';
		}

		$this->start_controls_section(
			$prefix . '_section',
			[ 'label' => $args['label'], 'tab' => $args['tab'] ]
		);

		// --- ALIGNMENT ---
		$this->add_responsive_control(
			$prefix . '_align',
			[
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'left'    => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-text-align-left' ],
					'center'  => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-text-align-center' ],
					'right'   => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-text-align-right' ],
					'justify' => [ 'title' => esc_html__( 'Justify', 'sifency-addons' ), 'icon' => 'eicon-text-align-justify' ],
				],
				'selectors' => [ $full_base_sel => 'text-align: {{VALUE}};' ],
			]
		);

		// --- TYPOGRAPHY ---
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[ 'name' => $prefix . '_typography', 'selector' => $text_selector_string ]
		);

		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			[ 'name' => $prefix . '_text_shadow', 'selector' => $text_selector_string ]
		);

		$this->add_control( $prefix . '_hr_1', [ 'type' => Controls_Manager::DIVIDER ] );

		// --- TABS ---
		$this->start_controls_tabs( $prefix . '_tabs' );

		// ==========================
		//      NORMAL TAB
		// ==========================
		$this->start_controls_tab( $prefix . '_normal_tab', [ 'label' => esc_html__( 'Normal', 'sifency-addons' ) ] );
		$this->add_control(
			$prefix . '_fill_type',
			[
				'label'   => esc_html__( 'Text Fill', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [ 'color' => 'Solid Color', 'gradient' => 'Gradient / Image' ],
				'default' => 'color',
				'prefix_class' => 'sifency-' . $prefix . '-fill-', 
			]
		);

		$this->add_control(
			$prefix . '_color',
			[
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'condition' => [ $prefix . '_fill_type' => 'color' ],
				'selectors' => [
					$text_selector_string => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'      => $prefix . '_gradient_fill',
				'types'     => [ 'gradient', 'classic' ],
				'selector'  => $text_selector_string,
				'condition' => [ $prefix . '_fill_type' => 'gradient' ],
				'fields_options' => [
					'background' => [ 'label' => esc_html__( 'Fill', 'sifency-addons' ) ],
				],
			]
		);

		$this->add_control( $prefix . '_stroke_sw', [ 'label' => esc_html__( 'Stroke', 'sifency-addons' ), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before' ] );
		$this->add_control( $prefix . '_stroke_c', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ $text_selector_string => '-webkit-text-stroke-color: {{VALUE}};' ], 'condition' => [ $prefix . '_stroke_sw' => 'yes' ] ] );
		$this->add_responsive_control( $prefix . '_stroke_w', [ 'label' => 'Width', 'type' => Controls_Manager::SLIDER, 'selectors' => [ $text_selector_string => '-webkit-text-stroke-width: {{SIZE}}{{UNIT}};' ], 'condition' => [ $prefix . '_stroke_sw' => 'yes' ] ] );

		$this->end_controls_tab();

		// ==========================
		//      HOVER TAB
		// ==========================
		$this->start_controls_tab( $prefix . '_hover_tab', [ 'label' => esc_html__( 'Hover', 'sifency-addons' ) ] );

		$this->add_control(
			$prefix . '_hover_fill_type',
			[
				'label'   => esc_html__( 'Text Fill', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [ 'color' => 'Solid Color', 'gradient' => 'Gradient / Image' ],
				'default' => 'color',
				'prefix_class' => 'sifency-' . $prefix . '-hover-fill-',
			]
		);

		$this->add_control(
			$prefix . '_hover_color',
			[
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'condition' => [ $prefix . '_hover_fill_type' => 'color' ],
				'selectors' => [
					$hover_selector_string => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'      => $prefix . '_hover_gradient_fill',
				'types'     => [ 'gradient', 'classic' ],
				'selector'  => $hover_selector_string,
				'condition' => [ $prefix . '_hover_fill_type' => 'gradient' ],
				'fields_options' => [ 'background' => [ 'label' => esc_html__( 'Fill', 'sifency-addons' ) ] ],
			]
		);

		$this->add_control( $prefix . '_hover_stroke_sw', [ 'label' => esc_html__( 'Stroke', 'sifency-addons' ), 'type' => Controls_Manager::SWITCHER, 'separator' => 'before' ] );
		$this->add_control( $prefix . '_hover_stroke_c', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ $hover_selector_string => '-webkit-text-stroke-color: {{VALUE}};' ], 'condition' => [ $prefix . '_hover_stroke_sw' => 'yes' ] ] );
		$this->add_responsive_control( $prefix . '_hover_stroke_w', [ 'label' => 'Width', 'type' => Controls_Manager::SLIDER, 'selectors' => [ $hover_selector_string => '-webkit-text-stroke-width: {{SIZE}}{{UNIT}};' ], 'condition' => [ $prefix . '_hover_stroke_sw' => 'yes' ] ] );

		if ( $args['show_box_model'] ) {
			$this->add_control( $prefix . '_hr_hover_box', [ 'type' => Controls_Manager::DIVIDER ] );
			$this->add_group_control( Group_Control_Background::get_type(), [ 'name' => $prefix . '_hover_box_bg', 'label' => 'Box Background', 'selector' => $full_base_sel . ':hover' ] );
			$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => $prefix . '_hover_box_border', 'selector' => $full_base_sel . ':hover' ] );
		}

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

		// --- BOX MODEL ---
		if ( $args['show_box_model'] ) {
			$this->add_control( $prefix . '_hr_box', [ 'type' => Controls_Manager::DIVIDER, 'style' => 'thick' ] );
			$this->add_group_control( Group_Control_Background::get_type(), [ 'name' => $prefix . '_box_bg', 'label' => 'Box Background', 'selector' => $full_base_sel, 'condition'  => [$prefix . '_fill_type' => 'color', ], ] );
			$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => $prefix . '_box_border', 'selector' => $full_base_sel ] );
			$this->add_responsive_control( $prefix . '_box_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'size_units' => [ 'px', '%' ], 'selectors' => [ $full_base_sel => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
			$this->add_responsive_control( $prefix . '_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ $full_base_sel => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
			$this->add_responsive_control( $prefix . '_margin', [ 'label' => 'Margin', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ $full_base_sel => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ] ] );
		}

		// --- TRANSITION ---
		$this->add_control(
			$prefix . '_transition',
			[
				'label'     => esc_html__( 'Transition Duration (s)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => [ 'px' => [ 'max' => 3, 'step' => 0.1 ] ],
				'default'   => [ 'size' => 0.3 ],
				'selectors' => [
					$full_base_sel => 'transition: all {{SIZE}}s ease;',
					$text_selector_string => 'transition: all {{SIZE}}s ease;',
				],
			]
		);

		$this->end_controls_section();
	}
}