<?php
/**
 * Alpha Animated Text Widget
 *
 * @author     D-THEMES
 * @package    Alpha Core FrameWork
 * @subpackage Core
 * @since      1.2.0
 */

defined( 'ABSPATH' ) || die;

use Elementor\Repeater;
use Elementor\Controls_Manager;
use Elementor\Core\Schemes\Color;
use Elementor\Group_Control_Typography;
use Elementor\Core\Schemes\Typography;

class Alpha_Animated_Text_Elementor_Widget extends Elementor\Widget_Heading {

	public function get_name() {
		return ALPHA_NAME . '_widget_animated_text';
	}

	public function get_title() {
		return esc_html__( 'Animated Text', 'alpha-core' );
	}

	public function get_categories() {
		return array( 'alpha_widget' );
	}

	public function get_icon() {
		return 'alpha-elementor-widget-icon alpha-widget-icon-animate';
	}

	public function get_keywords() {
		return array( 'animation', 'animated', 'heading', 'text', 'alpha' );
	}

	/**
	 * Get the style depends.
	 *
	 * @since 1.2.0
	 */
	public function get_style_depends() {
		wp_register_style( 'alpha-animated-text', alpha_core_framework_uri( '/widgets/animated-text/animated-text' . ( is_rtl() ? '-rtl' : '' ) . '.min.css' ), array(), ALPHA_CORE_VERSION );
		return array( 'alpha-animated-text' );
	}

	/**
	 * Get the script depends.
	 *
	 * @since 1.2.0
	 */
	public function get_script_depends() {
		wp_register_script( 'alpha-animated-text', alpha_core_framework_uri( '/widgets/animated-text/animated-text' . ALPHA_JS_SUFFIX ), array( 'jquery-core' ), ALPHA_CORE_VERSION, true );
		return array( 'alpha-animated-text' );
	}

	protected function register_controls() {

		parent::register_controls();

		$repeater = new Repeater();

		$repeater->add_control(
			'text',
			array(
				'label'       => esc_html__( 'Text', 'alpha-core' ),
				'description' => esc_html__( 'Type a certain heading you want to display.', 'alpha-core' ),
				'label_block' => true,
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'dynamic'     => array(
					'active' => true,
				),
			)
		);

		$repeater->add_control(
			'color',
			array(
				'label'       => esc_html__( 'Color', 'alpha-core' ),
				'description' => esc_html__( 'Controls the color.', 'alpha-core' ),
				'type'        => Controls_Manager::COLOR,
				'default'     => alpha_get_option( 'primary_color' ),
				'selectors'   => array(
					'.elementor-element-{{ID}} {{CURRENT_ITEM}}' => 'color: {{VALUE}}',
				),
			)
		);

		$repeater->add_control(
			'custom_class',
			array(
				'label'       => esc_html__( 'Custom Class', 'alpha-core' ),
				'description' => esc_html__( 'Add your custom class WITHOUT the dot. e.g: my-class', 'alpha-core' ),
				'type'        => Controls_Manager::TEXT,
			)
		);

		$this->add_control(
			'title_after',
			array(
				'label'       => esc_html__( 'Title After', 'alpha-core' ),
				'description' => esc_html__( 'Enter after text.', 'alpha-core' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Theme', 'alpha-core' ),
				'dynamic'     => array(
					'active' => true,
				),
			),
			array(
				'position' => array(
					'at' => 'after',
					'of' => 'title',
				),
			)
		);

		$presets = array(
			array(
				'text' => esc_html__( 'Business', 'alpha-core' ),
			),
			array(
				'text' => esc_html__( 'Portfolio', 'alpha-core' ),
			),
			array(
				'text' => esc_html__( 'Education', 'alpha-core' ),
			),
			array(
				'text' => esc_html__( 'E-Commerce', 'alpha-core' ),
			),
		);

		$this->add_control(
			'items',
			array(
				'label'       => esc_html__( 'Animating Items', 'alpha-core' ),
				'type'        => Controls_Manager::REPEATER,
				'title_field' => '{{{ text }}}',
				'fields'      => $repeater->get_controls(),
				'default'     => $presets,
			),
			array(
				'position' => array(
					'at' => 'after',
					'of' => 'title',
				),
			)
		);

		$this->add_control(
			'title_before',
			array(
				'label'       => esc_html__( 'Title Before', 'alpha-core' ),
				'description' => esc_html__( 'Enter before text.', 'alpha-core' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Truly', 'alpha-core' ),
				'dynamic'     => array(
					'active' => true,
				),
			),
			array(
				'position' => array(
					'at' => 'after',
					'of' => 'title',
				),
			)
		);

		$this->remove_control( 'title' );
		$this->remove_control( 'link' );
		$this->remove_control( 'size' );

		$this->start_controls_section(
			'section_animation',
			array(
				'label' => esc_html__( 'Animation Options', 'alpha-core' ),
			)
		);

			$this->add_control(
				'animation_type',
				array(
					'label'       => esc_html__( 'Animation Type', 'alpha-core' ),
					'description' => esc_html__( 'Select the style for animated text.', 'alpha-core' ),
					'type'        => Controls_Manager::SELECT,
					'default'     => 'joke',
					'options'     => array(
						'joke'     => esc_html__( 'Joke', 'alpha-core' ),
						'fall'     => esc_html__( 'Fall', 'alpha-core' ),
						'rotation' => esc_html__( 'Rotation', 'alpha-core' ),
						'croco'    => esc_html__( 'Croco', 'alpha-core' ),
						'scaling'  => esc_html__( 'Scaling', 'alpha-core' ),
						'typing'   => esc_html__( 'Typing', 'alpha-core' ),
					),
				)
			);

			$this->add_control(
				'animation_delay',
				array(
					'label'       => esc_html__( 'Animation Delay (ms)', 'alpha-core' ),
					'description' => esc_html__( 'Controls the delay of animation between each text in a set. In milliseconds, 1000 = 1 second.', 'alpha-core' ),
					'type'        => Controls_Manager::TEXT,
					'placeholder' => '3000',
				)
			);

			$this->add_control(
				'split_type',
				array(
					'label'       => esc_html__( 'Split Type', 'alpha-core' ),
					'description' => esc_html__( 'Split the animated text into animating pieces such as letters or words.', 'alpha-core' ),
					'type'        => Controls_Manager::SELECT,
					'default'     => 'letter',
					'options'     => array(
						'letter' => esc_html__( 'Letters', 'alpha-core' ),
						'word'   => esc_html__( 'Words', 'alpha-core' ),
					),
				)
			);

			$this->add_control(
				'typing_color',
				array(
					'label'       => esc_html__( 'Typing Color', 'alpha-core' ),
					'description' => esc_html__( 'Control color of split focus line in typing animation.', 'alpha-core' ),
					'type'        => Controls_Manager::COLOR,
					'selectors'   => array(
						'.elementor-element-{{ID}} .animating-text-typing .animating-item:after' => 'color: {{VALUE}}',
					),
					'condition'   => array(
						'animation_type' => 'typing',
					),
				),
				array(
					'position' => array(
						'at' => 'after',
						'of' => 'blend_mode',
					),
				)
			);

		$this->end_controls_section();
	}

	protected function render() {
		$atts         = $this->get_settings_for_display();
		$atts['self'] = $this;
		require alpha_core_framework_path( ALPHA_CORE_FRAMEWORK_PATH . '/widgets/animated-text/render-animated-text-elementor.php' );
	}

	protected function content_template() {
	}
}
