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

class Sifency_Typewriter_Text extends Widget_Base {
    use Sifency_Core_Text_Style;
	public function get_name() {
		return 'sifency_typewriter';
	}

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

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

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

	public function get_script_depends() {
		return [ 'sifency-typeout-script', 'sifency-typed-lib-script' ];
	}

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

	protected function register_controls() {

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

		$this->add_control(
			'prefix_text',
			[
				'label'       => esc_html__( 'Prefix Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'I am a',
				'default'     => 'We build ',
				'dynamic'     => [ 'active' => true ],
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'string_text',
			[
				'label'       => esc_html__( 'Text String', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => 'Websites',
				'label_block' => true,
				'description' => 'HTML tags like &lt;b&gt;, &lt;i&gt;, &lt;br&gt; are supported.',
			]
		);

		$this->add_control(
			'strings_list',
			[
				'label'       => esc_html__( 'Typed Strings', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => [
					[ 'string_text' => 'Websites.' ],
					[ 'string_text' => 'Applications.' ],
					[ 'string_text' => '<b>Experiences.</b>' ],
				],
				'title_field' => '{{{ string_text }}}',
			]
		);

		$this->add_control(
			'suffix_text',
			[
				'label'       => esc_html__( 'Suffix Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => 'for you.',
				'default'     => '',
				'dynamic'     => [ 'active' => true ],
			]
		);

		$this->end_controls_section();

		// ==============================
		// CONTENT: SETTINGS
		// ==============================
		$this->start_controls_section(
			'section_settings',
			[ 'label' => esc_html__( 'Settings', 'sifency-addons' ) ]
		);

		$this->add_control(
			'type_speed',
			[
				'label'   => esc_html__( 'Type Speed (ms)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 50,
				'min'     => 0,
				'max'     => 500,
				'step'    => 5,
			]
		);

		$this->add_control(
			'start_delay',
			[
				'label'   => esc_html__( 'Start Delay (ms)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 0,
			]
		);

		$this->add_control(
			'back_speed',
			[
				'label'   => esc_html__( 'Back Speed (ms)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 30,
			]
		);

		$this->add_control(
			'back_delay',
			[
				'label'   => esc_html__( 'Back Delay (ms)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 1500,
				'description' => 'Time to wait before deleting.',
			]
		);

		$this->add_control(
			'loop',
			[
				'label'   => esc_html__( 'Loop Infinitely', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'show_cursor',
			[
				'label'   => esc_html__( 'Show Cursor', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'cursor_char',
			[
				'label'     => esc_html__( 'Cursor Character', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => '|',
				'condition' => [ 'show_cursor' => 'yes' ],
			]
		);

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

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'content_style',
            'label'    => 'Typed Text',
            'selector' => '.sifency-content-style',
            'target_children' => false,
        ]);

		// ==============================
		// STYLE: CURSOR
		// ==============================
		$this->start_controls_section(
			'section_style_cursor',
			[ 
				'label' => esc_html__( 'Cursor', 'sifency-addons' ), 
				'tab' => Controls_Manager::TAB_STYLE,
				'condition' => [ 'show_cursor' => 'yes' ]
			]
		);

		$this->add_control(
			'cursor_color',
			[
				'label'     => esc_html__( 'Cursor Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .typed-cursor' => 'color: {{VALUE}};' ],
			]
		);

		$this->add_control(
			'cursor_size',
			[
				'label'     => esc_html__( 'Size (px)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => [ '{{WRAPPER}} .typed-cursor' => 'font-size: {{SIZE}}px;' ],
			]
		);

		$this->add_control(
			'disable_blink',
			[
				'label' => esc_html__( 'Disable Blinking', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'selectors' => [
					'{{WRAPPER}} .typed-cursor' => 'animation: none; opacity: 1;',
				],
			]
		);

		$this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'label_style',
            'label'    => 'Suffix Text',
            'selector' => '.sifency-label-style',
            'target_children' => false,
        ]);

	}

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

		// Prepare strings for JS
		$strings = [];
		foreach ( $settings['strings_list'] as $item ) {
			$strings[] = $item['string_text'];
		}

		// Config options for JS
		$config = [
			'strings'     => $strings,
			'typeSpeed'   => $settings['type_speed'],
			'startDelay'  => $settings['start_delay'],
			'backSpeed'   => $settings['back_speed'],
			'backDelay'   => $settings['back_delay'],
			'loop'        => ( 'yes' === $settings['loop'] ),
			'showCursor'  => ( 'yes' === $settings['show_cursor'] ),
			'cursorChar'  => $settings['cursor_char'],
			'smartBackspace' => true, // Smooth feature
		];

		// Encode settings to pass to JS
		$this->add_render_attribute( 'typewriter-wrapper', [
			'class' => 'sifency-typewriter-wrapper',
			'data-settings' => wp_json_encode( $config ),
		] );

		?>
		<div <?php echo wp_kses( $this->get_render_attribute_string( 'typewriter-wrapper' ), [] ); ?>>
			<?php if ( ! empty( $settings['prefix_text'] ) ) : ?>
				<span class="sifency-prefix-text sifency-heading-style"><?php echo esc_html( $settings['prefix_text'] ); ?></span>
			<?php endif; ?>

			<span class="sifency-typed-string sifency-content-style"></span>

			<?php if ( ! empty( $settings['suffix_text'] ) ) : ?>
				<span class="sifency-suffix-text sifency-label-style"><?php echo esc_html( $settings['suffix_text'] ); ?></span>
			<?php endif; ?>
		</div>
		<?php
	}
}