<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Border;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Highlighted_Text extends Widget_Base {
    use Sifency_Core_Text_Style;

	public function get_name() {
		return 'sifency_highlighted_text';
	}

	public function get_title() {
		return esc_html__( 'Sifency Highlighted Text', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-edit';
	}

	public function get_categories() {
		return [ 'sifency-addons-widgets' ];
	}

	public function get_style_depends() {
		return [ 'sifency-highlight-css' ];
	}

	protected function register_controls() {

		// ==========================
		// CONTENT SECTION
		// ==========================
		$this->start_controls_section(
			'section_content',
			[ 'label' => esc_html__( 'Text Content', 'sifency-addons' ) ]
		);

		$this->add_control(
			'text_content',
			[
				'label' => esc_html__( 'Description', 'sifency-addons' ),
				'type' => Controls_Manager::TEXTAREA,
				'rows' => 6,
				'default' => 'Highlighted Text widget provides an easy way {to highlight specific words or parts of text}, all while applying different stylizations.',
				'description' => esc_html__( 'Wrap the text you want to highlight in curly braces. Ex: {This is highlighted}', 'sifency-addons' ),
				'dynamic' => [ 'active' => true ],
			]
		);

		$this->add_control(
			'html_tag',
			[
				'label' => esc_html__( 'HTML Tag', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'h1' => 'H1',
					'h2' => 'H2',
					'h3' => 'H3',
					'h4' => 'H4',
					'h5' => 'H5',
					'h6' => 'H6',
					'div' => 'div',
					'span' => 'span',
					'p' => 'p',
				],
				'default' => 'h3',
			]
		);

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

		$this->end_controls_section(); // <--- Correctly closes 'section_content'
        // REMOVED THE EXTRA CLOSING TAG HERE

		// ==========================
		// STYLE: HIGHLIGHT
		// ==========================
		$this->start_controls_section(
			'section_style_highlight',
			[ 'label' => esc_html__( 'Highlight Style', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ]
		);

		$this->add_control(
			'highlight_shape',
			[
				'label' => esc_html__( 'Shape', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'options' => [
					'full' => 'Full Box (Background)',
					'underline' => 'Underline',
					'marker' => 'Marker (Bottom Half)',
				],
				'default' => 'full',
				'prefix_class' => 'sifency-hl-shape-',
			]
		);

		$this->add_control(
			'highlight_bg_color',
			[
				'label' => esc_html__( 'Highlight Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'default' => '#f25f29', // Orange from screenshot
				'selectors' => [ 
					'{{WRAPPER}} .sifency-hl-text' => 'background-color: {{VALUE}}; --sifency-hl-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'highlight_text_color',
			[
				'label' => esc_html__( 'Text Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'default' => '#ffffff',
				'selectors' => [ '{{WRAPPER}} .sifency-hl-text' => 'color: {{VALUE}};' ],
				'condition' => [ 'highlight_shape' => 'full' ], // Only relevant for full box
			]
		);

		// Animation
		$this->add_control(
			'hover_animation',
			[
				'label' => esc_html__( 'Hover Animation', 'sifency-addons' ),
				'type' => Controls_Manager::HOVER_ANIMATION,
			]
		);

		$this->end_controls_section();

        // Traits automatically open and close their own sections, so this is safe to place here
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'label_style',
            'label'    => 'Highlighted Text Advance',
            'selector' => '.sifency-label-style',
            'target_children' => false,
        ]);
        
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Normal Text Style',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);
	}

    protected function render() {
        $settings = $this->get_settings_for_display();

        if ( empty( $settings['text_content'] ) ) {
            return;
        }

        $raw_text  = $settings['text_content'];
        $tag       = ! empty( $settings['html_tag'] ) ? $settings['html_tag'] : 'div';
        $animation = ! empty( $settings['hover_animation'] ) ? 'elementor-animation-' . esc_attr( $settings['hover_animation'] ) : '';

        // Replace {text} with span
        $processed_text = preg_replace(
            '/\{(.*?)\}/',
            '<span class="sifency-hl-text sifency-label-style ' . $animation . '">$1</span>',
            $raw_text
        );
        ?>
        <<?php echo esc_attr( $tag ); ?> class="sifency-highlight-wrapper sifency-heading-style">
            <?php echo wp_kses_post( $processed_text ); ?>
        </<?php echo esc_attr( $tag ); ?>>
        <?php
    }
}