<?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 Sifency\Traits\Sifency_Core_Text_Style;
use \Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Custom_Title_Widget extends Widget_Base {
    use Sifency_Animation_Controls;
	use Sifency_Core_Text_Style;

	public function get_name() {
		return 'sifency-addons-title';
	}
	
	public function get_title() {
		return esc_html__( 'Sifency Title', 'sifency-addons' );
	}
	
	public function get_icon() {
		return 'eicon-heading';
	}
	
	public function get_categories() {
		return [ 'sifency-addons-widgets' ];
	}

	public function get_keywords() {
		return [ 'heading', 'title', 'text', 'animation', 'stroke', 'split' ];
	}

    public function get_style_depends() {
	    return [ 'sifency-typo-style' ];
    }

	protected function register_controls() {
		// --- Section: Content ---
		$this->start_controls_section(
			'heading_content_section',
			[ 'label' => esc_html__( 'Content', 'sifency-addons' ) ]
		);
		$this->add_control(
			'title_source',
			[
				'label'   => esc_html__( 'Source', 'sifency-addons' ), 'type' => Controls_Manager::SELECT,
				'options' => [ 'custom' => esc_html__( 'Custom', 'sifency-addons' ), 'post_title' => esc_html__( 'Post Title', 'sifency-addons' ) ],
				'default' => 'custom',
			]
		);
		$this->add_control(
			'heading_text',
			[
				'label' => esc_html__( 'Text', 'sifency-addons' ), 'type' => Controls_Manager::TEXTAREA,
				'default' => esc_html__( 'Sifency Heading Title', 'sifency-addons' ), 'condition' => [ 'title_source' => 'custom' ], 'dynamic' => [ 'active' => true ],
			]
		);
		$this->add_control(
			'link',
			[ 'label' => esc_html__( 'Link', 'sifency-addons' ), 'type' => Controls_Manager::URL, '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' => 'h2',
			]
		);
		$this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'heading_style',
            'label'    => 'Heading Style',
            'selector' => '.sifency-heading-style',
            'target_children' => false,
        ]);

		$this->add_sifency_animation_controls();
		$this->update_control(
            'sifency_animation_target',
            [
                'type' => Controls_Manager::HIDDEN,
                'default' => 'wrapper',
            ]
        );
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$text = ('post_title' === $settings['title_source']) ? get_the_title() : $settings['heading_text'];
		if ( empty($text) ) {
			return;
		}
		$animation_manager = new Sifency_Animation_Manager($settings);
		$html_tag = tag_escape($settings['html_tag']);
		$this->add_render_attribute('heading', 'class', 'sifency-heading sifency-heading-style' );
		if ( ! empty( $settings['link']['url'] ) ) {
			$this->add_link_attributes( 'link', $settings['link'] );
		}
		$wrapper_classes = ['sifency-heading-wrapper'];
		$fill_type = $settings['text_fill_advanced_background'] ?? '';
		$has_image = ! empty( $settings['text_fill_advanced_image']['url'] );
		if ( 'gradient' === $fill_type || ( 'classic' === $fill_type && $has_image ) ) {
			$wrapper_classes[] = 'sifency-heading--text-fill';
		}
		$wrapper_attrs = $animation_manager->get_wrapper_attributes($wrapper_classes);
		?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php if ( ! empty( $settings['link']['url'] ) ) : ?>
				<a <?php echo wp_kses_post ($this->get_render_attribute_string('link')); ?>>
			<?php endif; ?>

			<<?php echo esc_html($html_tag); ?> <?php echo wp_kses_post ($this->get_render_attribute_string('heading')); ?>>
				<?php echo wp_kses_post($text); ?>
			</<?php 
			
			echo esc_html ($html_tag); ?>>

			<?php if ( ! empty( $settings['link']['url'] ) ) : ?>
				</a>
			<?php endif; ?>
		</div>
		<?php
	}
}