<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
class Sifency_Addons_Text_Reveal extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Custom_CSS_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-text-reveal',
			'title'          => 'Sifency Text Reveal',
			'icon'           => 'eicon-text-align-center',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'gsap', 'gsap-scrolltrigger', 'sifency-text-reveal-script', 'lenis' ),
			'style_depends'  => array( 'sifency-creative-style' ),
			'keywords'       => array( 'typography', 'text reveal', 'modern style text', 'gsap scroll' ),
		);
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Reveal Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'reveal_text',
			array(
				'label'       => esc_html__( 'Text to Reveal', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => esc_html__( 'Modern web design is about storytelling. As you scroll, the narrative unfolds seamlessly, creating an immersive experience for the user.', 'sifency-addons' ),
				'description' => 'Write a paragraph or heading. The JS will automatically split it for animation.',
			)
		);
		$this->add_control(
			'html_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',
					'p'   => 'p',
					'div' => 'div',
				),
				'default' => 'h2',
			)
		);
		$this->add_control(
			'split_type',
			array(
				'label'   => esc_html__( 'Animate By', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'word' => 'Words (Smooth & Standard)',
					'char' => 'Characters (Modern Style)',
				),
				'default' => 'word',
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_settings',
			array( 'label' => esc_html__( 'Animation Behavior', 'sifency-addons' ) )
		);

		$this->add_control(
			'animation_style',
			array(
				'label'   => esc_html__( 'Animation Effect', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'fade'    => 'Fade Opacity Only',
					'slideUp' => 'Fade & Slide Up',
				),
				'default' => 'fade',
			)
		);
		$this->add_control(
			'inactive_opacity',
			array(
				'label'       => esc_html__( 'Inactive Opacity', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.1,
					),
				),
				'default'     => array( 'size' => 0.2 ),
				'description' => 'How transparent the text is before scrolling.',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'typo_',
				'label'           => 'Typography',
				'selector'        => '.sifency-text-reveal',
				'color_only'      => true,
				'show_blend_mode' => true,
				'exclude'         => array( 'tabs', 'transition', 'shadow', 'stroke', 'border', 'radius', 'bg', 'padding', 'margin' ),
			)
		);
		$this->add_Sifency_Addons_Custom_CSS_Controls();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	protected function render_dom( array $context ) {

		$settings   = $this->get_settings_for_display();
		$wrapper_id = ! empty( $context['dom_id'] ) ? $context['dom_id'] : '';
		$text       = $settings['reveal_text'];
		$tag        = ! empty( $settings['html_tag'] ) ? tag_escape( $settings['html_tag'] ) : 'div';

		if ( empty( $text ) ) {
			return;
		}

		$this->sifency_addons_render_custom_css();

		// Config (clean + safe)
		$data_attributes = array(
			'split'   => $settings['split_type'] ?? 'words',
			'effect'  => $settings['animation_style'] ?? 'fade',
			'opacity' => $settings['inactive_opacity']['size'] ?? 0.2,
		);

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_classes = array( 'sifency-text-reveal-wrapper' );

		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );

		if ( ! empty( $wrapper_id ) ) {
			$wrapper_attrs .= ' id="' . esc_attr( $wrapper_id ) . '"';
		}
		?>

		<div <?php echo wp_kses_post( $wrapper_attrs ); ?>>

			<<?php echo esc_html( $tag ); ?>
				class="sifency-text-reveal"
				data-split="<?php echo esc_attr( $data_attributes['split'] ); ?>"
				data-effect="<?php echo esc_attr( $data_attributes['effect'] ); ?>"
				data-opacity="<?php echo esc_attr( $data_attributes['opacity'] ); ?>">

				<?php echo wp_kses_post( $text ); ?>

			</<?php echo esc_html( $tag ); ?>>

		</div>

		<?php
	}
}
