<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;  }

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_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;

class Sifency_Typewriter_Text extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency_typewriter',
			'title'          => 'Sifency Typewriter',
			'icon'           => 'eicon-animation-text',
			'category'       => 'sifency-typography-widgets',
			'keywords'       => array( 'typewriter', 'text', 'animation', 'typing' ),
			'script_depends' => array( 'sifency-typeout-script', 'sifency-typed-lib-script' ),
			'style_depends'  => array( 'sifency-typeout-style', 'sifency-typo-style' ),
		);}
	protected function register_controls() {
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Typewriter Content', 'sifency-addons' ) )
		);
		$this->add_control(
			'prefix_text',
			array(
				'label'       => esc_html__( 'Prefix Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'placeholder' => 'I am a',
				'default'     => 'We build ',
				'dynamic'     => array( 'active' => true ),
			)
		);
		$repeater = new Repeater();

		$repeater->add_control(
			'string_text',
			array(
				'label'       => esc_html__( 'Text String', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'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',
			array(
				'label'       => esc_html__( 'Typed Strings', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'string_text' => 'Websites.' ),
					array( 'string_text' => 'Applications.' ),
					array( 'string_text' => '<b>Experiences.</b>' ),
				),
				'title_field' => '{{{ string_text }}}',
			)
		);
		$this->add_control(
			'suffix_text',
			array(
				'label'       => esc_html__( 'Suffix Text', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXTAREA,
				'placeholder' => 'for you.',
				'default'     => '',
				'dynamic'     => array( 'active' => true ),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_settings',
			array( 'label' => esc_html__( 'Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'type_speed',
			array(
				'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',
			array(
				'label'   => esc_html__( 'Start Delay (ms)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 0,
			)
		);
		$this->add_control(
			'back_speed',
			array(
				'label'   => esc_html__( 'Back Speed (ms)', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 30,
			)
		);
		$this->add_control(
			'back_delay',
			array(
				'label'       => esc_html__( 'Back Delay (ms)', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'default'     => 1500,
				'description' => 'Time to wait before deleting.',
			)
		);
		$this->add_control(
			'loop',
			array(
				'label'   => esc_html__( 'Loop Infinitely', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'show_cursor',
			array(
				'label'   => esc_html__( 'Show Cursor', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);
		$this->add_control(
			'cursor_char',
			array(
				'label'     => esc_html__( 'Cursor Character', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => '|',
				'condition' => array( 'show_cursor' => 'yes' ),
			)
		);
		$this->add_control(
			'text_align',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'toggle'    => true,
				'selectors' => array(
					'{{WRAPPER}} .sifency-typewriter-wrapper' => 'text-align: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'heading_style',
				'label'     => 'Prefix Text',
				'selector'  => '.sifency-heading-style',
				'condition' => array( 'prefix_text!' => '' ),
				'exclude'   => array( 'alignment' ),
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Typed Text',
				'selector' => '.sifency-content-style',
				'exclude'  => array( 'alignment' ),
			)
		);

		$this->start_controls_section(
			'section_style_cursor',
			array(
				'label'     => esc_html__( 'Cursor', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_cursor' => 'yes' ),
			)
		);
		$this->add_control(
			'cursor_color',
			array(
				'label'     => esc_html__( 'Cursor Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .typed-cursor' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'cursor_size',
			array(
				'label'     => esc_html__( 'Size (px)', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .typed-cursor' => 'font-size: {{SIZE}}px;' ),
			)
		);
		$this->add_control(
			'disable_blink',
			array(
				'label'     => esc_html__( 'Disable Blinking', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'selectors' => array(
					'{{WRAPPER}} .typed-cursor' => 'animation: none; opacity: 1;',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style',
				'label'     => 'Suffix Text',
				'selector'  => '.sifency-label-style',
				'condition' => array( 'suffix_text!' => '' ),
				'exclude'   => array( 'alignment' ),
			)
		);
	}

	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();

		$strings = array();
		foreach ( $settings['strings_list'] as $item ) {
			$strings[] = $item['string_text'];
		}

		$config = array(
			'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,
		);

		$this->add_render_attribute(
			'typewriter-wrapper',
			array(
				'class'         => 'sifency-typewriter-wrapper',
				'data-settings' => wp_json_encode( $config ),
			)
		);

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses( $this->get_render_attribute_string( 'typewriter-wrapper' ), array() ); ?>>
			<?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
	}
}
