<?php
namespace DotLifeElementor\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Scheme_Color;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * Elementor Animated Text
 *
 * Elementor widget for animated text
 *
 * @since 1.0.0
 */
class DotLife_Animated_Text extends Widget_Base {

	/**
	 * Retrieve the widget name.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'dotlife-animated-text';
	}

	/**
	 * Retrieve the widget title.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return __( 'Animated Text', 'dotlife-elementor' );
	}

	/**
	 * Retrieve the widget icon.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-t-letter';
	}

	/**
	 * Retrieve the list of categories the widget belongs to.
	 *
	 * Used to determine where to display the widget in the editor.
	 *
	 * Note that currently Elementor supports only one category.
	 * When multiple categories passed, Elementor uses the first one.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'dotlife-theme-widgets-category' ];
	}

	/**
	 * Retrieve the list of scripts the widget depended on.
	 *
	 * Used to set scripts dependencies required to run the widget.
	 *
	 * @since 1.0.0
	 *
	 * @access public
	 *
	 * @return array Widget scripts dependencies.
	 */
	public function get_script_depends() {
		return [ 'dotlife-elementor' ];
	}

	/**
	 * Register the widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.0
	 *
	 * @access protected
	 */
	protected function register_controls() {
		$this->start_controls_section(
			'section_text',
			[
				'label' => __( 'Text', 'dotlife-elementor' ),
			]
		);
		
		$this->add_control(
			'title_content',
			[
				'label' => __( 'Title', 'dotlife-elementor' ),
				'type' => \Elementor\Controls_Manager::TEXTAREA,
				'rows' => 5,
				'default' => '',
				'placeholder' => __( 'Type your title here', 'dotlife-elementor' ),
			]
		);
		
		$this->add_control(
			'title_link',
			[
				'label' => __( 'Link', 'dotlife-elementor' ),
				'type' => \Elementor\Controls_Manager::URL,
				'placeholder' => __( 'Paste URL or type', 'dotlife-elementor' ),
				'show_external' => true,
				'default' => [
					'url' => '',
					'is_external' => true,
					'nofollow' => true,
				],
			]
		);
		
		$this->add_control(
			'title_html_tag',
			[
				'label' => __( 'HTML Tag', 'dotlife-elementor' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'h1',
				'options' => [
					'h1'  => __( 'H1', 'dotlife-elementor' ),
					'h2'  => __( 'H2', 'dotlife-elementor' ),
					'h3'  => __( 'H3', 'dotlife-elementor' ),
					'h4'  => __( 'H4', 'dotlife-elementor' ),
					'h5'  => __( 'H5', 'dotlife-elementor' ),
					'h6'  => __( 'H6', 'dotlife-elementor' ),
					'div'  => __( 'div', 'dotlife-elementor' ),
					'span'  => __( 'span', 'dotlife-elementor' ),
					'p'  => __( 'p', 'dotlife-elementor' ),
				],
			]
		);
		
		$this->add_control(
			'title_delimiter_type',
			[
				'label' => __( 'Delimiter Type', 'dotlife-elementor' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'word',
				'options' => [
					'character'  => __( 'Character', 'dotlife-elementor' ),
					'word'  => __( 'Word', 'dotlife-elementor' ),
					'sentence'  => __( 'Sentence', 'dotlife-elementor' ),
				],
			]
		);
		
		$this->add_responsive_control(
			'title_alignment',
			[
				'label' => __( 'Alignment', 'dotlife-elementor' ),
				'type' => \Elementor\Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => __( 'Left', 'dotlife-elementor' ),
						'icon' => 'eicon-text-align-left',
					],
					'center' => [
						'title' => __( 'Center', 'dotlife-elementor' ),
						'icon' => 'eicon-text-align-center',
					],
					'right' => [
						'title' => __( 'Right', 'dotlife-elementor' ),
						'icon' => 'eicon-text-align-right',
					],
				],
				'default' => 'left',
				'toggle' => true,
		        'selectors' => [
		            '{{WRAPPER}} .themegoods-animated-text' => 'text-align: {{VALUE}}',
		        ],
			]
		);
		
		$this->add_control(
		    'title_transition_speed',
		    [
		        'label' => __( 'Transition Speed (in milli-seconds)', 'dotlife-elementor' ),
		        'type' => Controls_Manager::SLIDER,
		        'default' => [
		            'size' => 100,
		        ],
		        'range' => [
		            'px' => [
		                'min' => 100,
		                'max' => 2000,
		                'step' => 100,
		            ]
		        ],
		        'size_units' => [ 'px' ]
		    ]
		);
		
		$this->add_control(
		    'title_transition_duration',
		    [
		        'label' => __( 'Transition Duration (in milli-seconds)', 'dotlife-elementor' ),
		        'type' => Controls_Manager::SLIDER,
		        'default' => [
		            'size' => 800,
		        ],
		        'range' => [
		            'px' => [
		                'min' => 100,
		                'max' => 10000,
		                'step' => 100,
		            ]
		        ],
		        'size_units' => [ 'px' ]
		    ]
		);
		
		$this->add_control(
			'title_transition_from',
			[
				'label' => __( 'Transition from', 'dotlife-elementor' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'right',
				'options' => [
					'left'  => __( 'Left', 'dotlife-elementor' ),
					'right'  => __( 'Right', 'dotlife-elementor' ),
					'top'  => __( 'Top', 'dotlife-elementor' ),
					'bottom'  => __( 'Bottom', 'dotlife-elementor' ),
					'zoomin'  => __( 'Zoom In', 'dotlife-elementor' ),
					'zoomout'  => __( 'Zoom Out', 'dotlife-elementor' ),
				],
			]
		);
		
		$this->add_control(
			'title_transition_overflow',
			[
				'label' => __( 'Transition Overflow', 'dotlife-elementor' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'visible',
				'options' => [
					'visible'  => __( 'Visible', 'dotlife-elementor' ),
					'hidden'  => __( 'Hidden', 'dotlife-elementor' ),
				],
			]
		);
		
		$this->add_control(
			'title_transition_delay',
			[
				'label' => __( 'Animation Delay (ms)', 'dotlife-elementor' ),
				'type' => \Elementor\Controls_Manager::NUMBER,
				'min' => 0,
				'max' => 5000,
				'step' => 5,
				'default' => 0,
				'frontend_available' => false,
			]
		);
		
		$this->end_controls_section();
		
		$this->start_controls_section(
			'section_title_style',
			array(
				'label'      => esc_html__( 'Title', 'dotlife-elementor' ),
				'tab'        => Controls_Manager::TAB_STYLE,
				'show_label' => false,
			)
		);
		
		$this->add_control(
		    'title_font_color',
		    [
		        'label' => __( 'Text Color', 'dotlife-elementor' ),
		        'type' => Controls_Manager::COLOR,
		        'default' => '#000000',
		        'selectors' => [
		            '{{WRAPPER}} .themegoods-animated-text h1' => 'color: {{VALUE}}',
		            '{{WRAPPER}} .themegoods-animated-text h2' => 'color: {{VALUE}}',
		            '{{WRAPPER}} .themegoods-animated-text h3' => 'color: {{VALUE}}',
		            '{{WRAPPER}} .themegoods-animated-text h4' => 'color: {{VALUE}}',
		            '{{WRAPPER}} .themegoods-animated-text h5' => 'color: {{VALUE}}',
		            '{{WRAPPER}} .themegoods-animated-text h6' => 'color: {{VALUE}}',
		            '{{WRAPPER}} .themegoods-animated-text div' => 'color: {{VALUE}}',
		            '{{WRAPPER}} .themegoods-animated-text span' => 'color: {{VALUE}}',
		            '{{WRAPPER}} .themegoods-animated-text p' => 'color: {{VALUE}}',
		        ],
		    ]
		);
		
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'title_typography',
				'label' => __( 'Typography', 'dotlife-elementor' ),
				'selector' => '{{WRAPPER}} .themegoods-animated-text h1, {{WRAPPER}} .themegoods-animated-text h2, {{WRAPPER}} .themegoods-animated-text h3, {{WRAPPER}} .themegoods-animated-text h4, {{WRAPPER}} .themegoods-animated-text h5, {{WRAPPER}} .themegoods-animated-text h6, {{WRAPPER}} .themegoods-animated-text div, {{WRAPPER}} .themegoods-animated-text span, {{WRAPPER}} .themegoods-animated-text p',
			]
		);
		
		$this->end_controls_section();
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.0
	 *
	 * @access protected
	 */
	protected function render() {
		include(DOTLIFE_ELEMENTOR_PATH.'templates/animated-text/index.php');
	}

	/**
	 * Render the widget output in the editor.
	 *
	 * Written as a Backbone JavaScript template and used to generate the live preview.
	 *
	 * @since 1.0.0
	 *
	 * @access protected
	 */
	protected function content_template() {
		return '';
	}
}
