<?php

namespace Unlockafe_addons\Widgets;

use Unlockafe_addons\Traits\Utils as UnlockafeUtils;
use Unlockafe_addons\Traits\Widget_helper;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Text_Stroke;
use Elementor\Group_Control_Text_Shadow;

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

/**
 * Unlock Heading Widget
 *
 * Elementor-style heading with Title, Link, HTML Tag, Style options and Entrance Animation.
 *
 * @since 1.0.0
 */
class Unlock_Heading extends Widget_Base {

	use Widget_helper, UnlockafeUtils;

	public function get_name() {
		return 'unlockafe-unlock-heading';
	}

	public function get_title() {
		return __( 'Unlock Heading', 'unlock-addons-for-elementor' );
	}

	public function get_icon() {
		return 'eicon-t-letter ulicon';
	}

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

	public function get_keywords() {
		return [ 'heading', 'title', 'text' ];
	}

	public function get_script_depends() {
		return [];
	}

	public function get_style_depends() {
		return [ 'unlockafe-unlock-heading-style' ];
	}

	protected function register_controls() {
		$this->register_content_heading();
		$this->register_content_animation();
		$this->register_style_heading();
	}

	/**
	 * Content tab: Heading section (Title, Link, HTML Tag).
	 */
	protected function register_content_heading() {
		$this->start_controls_section(
			'section_heading',
			[
				'label' => esc_html__( 'Heading', 'unlock-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'title',
			[
				'label'       => esc_html__( 'Title', 'unlock-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [ 'active' => true ],
				'placeholder' => esc_html__( 'Enter your title', 'unlock-addons-for-elementor' ),
				'default'     => esc_html__( 'Add Your Heading Text Here', 'unlock-addons-for-elementor' ),
			]
		);

		$this->add_control(
			'link',
			[
				'label'   => esc_html__( 'Link', 'unlock-addons-for-elementor' ),
				'type'    => Controls_Manager::URL,
				'dynamic' => [ 'active' => true ],
				'default' => [ 'url' => '' ],
			]
		);

		$this->add_control(
			'header_size',
			[
				'label'   => esc_html__( 'HTML Tag', 'unlock-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'h1'   => 'H1',
					'h2'   => 'H2',
					'h3'   => 'H3',
					'h4'   => 'H4',
					'h5'   => 'H5',
					'h6'   => 'H6',
					'div'  => 'div',
					'span' => 'span',
					'p'    => 'p',
				],
				'default' => 'h2',
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab: Entrance Animation section.
	 */
	protected function register_content_animation() {
		$this->start_controls_section(
			'section_entrance_animation',
			[
				'label' => esc_html__( 'Entrance Animation', 'unlock-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'entrance_animation',
			[
				'label'   => esc_html__( 'Animation', 'unlock-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					''              => esc_html__( 'None', 'unlock-addons-for-elementor' ),
					'fadeIn'        => esc_html__( 'Fade In', 'unlock-addons-for-elementor' ),
					'fadeInUp'      => esc_html__( 'Fade In Up', 'unlock-addons-for-elementor' ),
					'fadeInDown'    => esc_html__( 'Fade In Down', 'unlock-addons-for-elementor' ),
					'fadeInLeft'    => esc_html__( 'Fade In Left', 'unlock-addons-for-elementor' ),
					'fadeInRight'   => esc_html__( 'Fade In Right', 'unlock-addons-for-elementor' ),
					'slideInUp'     => esc_html__( 'Slide In Up', 'unlock-addons-for-elementor' ),
					'slideInDown'   => esc_html__( 'Slide In Down', 'unlock-addons-for-elementor' ),
					'slideInLeft'   => esc_html__( 'Slide In Left', 'unlock-addons-for-elementor' ),
					'slideInRight'  => esc_html__( 'Slide In Right', 'unlock-addons-for-elementor' ),
					'zoomIn'       => esc_html__( 'Zoom In', 'unlock-addons-for-elementor' ),
					'zoomOut'      => esc_html__( 'Zoom Out', 'unlock-addons-for-elementor' ),
					'bounceIn'     => esc_html__( 'Bounce In', 'unlock-addons-for-elementor' ),
					'rotateIn'     => esc_html__( 'Rotate In', 'unlock-addons-for-elementor' ),
				],
				'default' => '',
			]
		);

		$this->add_control(
			'animation_duration',
			[
				'label'     => esc_html__( 'Duration (ms)', 'unlock-addons-for-elementor' ),
				'type'      => Controls_Manager::NUMBER,
				'default'   => 800,
				'min'       => 100,
				'max'       => 5000,
				'step'      => 100,
				'condition' => [
					'entrance_animation!' => '',
				],
			]
		);

		$this->add_control(
			'animation_delay',
			[
				'label'     => esc_html__( 'Delay (ms)', 'unlock-addons-for-elementor' ),
				'type'      => Controls_Manager::NUMBER,
				'default'   => 0,
				'min'       => 0,
				'max'       => 5000,
				'step'      => 50,
				'condition' => [
					'entrance_animation!' => '',
				],
			]
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab: Heading section.
	 */
	protected function register_style_heading() {
		$this->start_controls_section(
			'section_heading_style',
			[
				'label' => esc_html__( 'Heading', 'unlock-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_responsive_control(
			'align',
			[
				'label'     => esc_html__( 'Alignment', 'unlock-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'left'    => [
						'title' => esc_html__( 'Left', 'unlock-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					],
					'center'  => [
						'title' => esc_html__( 'Center', 'unlock-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					],
					'right'   => [
						'title' => esc_html__( 'Right', 'unlock-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					],
					'justify' => [
						'title' => esc_html__( 'Justified', 'unlock-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-justify',
					],
				],
				'default'   => '',
				'selectors' => [
					'{{WRAPPER}}' => 'text-align: {{VALUE}};',
				],
				'separator' => 'after',
			]
		);

		$heading_selector = '{{WRAPPER}} .elementor-widget-container .unlock-heading-title';

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'typography',
				'selector' => $heading_selector,
			]
		);

		$this->add_group_control(
			Group_Control_Text_Stroke::get_type(),
			[
				'name'     => 'text_stroke',
				'selector' => $heading_selector,
			]
		);

		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			[
				'name'     => 'text_shadow',
				'selector' => $heading_selector,
			]
		);

		$this->add_control(
			'blend_mode',
			[
				'label'     => esc_html__( 'Blend Mode', 'unlock-addons-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => [
					'normal'      => esc_html__( 'Normal', 'unlock-addons-for-elementor' ),
					'multiply'    => esc_html__( 'Multiply', 'unlock-addons-for-elementor' ),
					'screen'      => esc_html__( 'Screen', 'unlock-addons-for-elementor' ),
					'overlay'     => esc_html__( 'Overlay', 'unlock-addons-for-elementor' ),
					'darken'      => esc_html__( 'Darken', 'unlock-addons-for-elementor' ),
					'lighten'     => esc_html__( 'Lighten', 'unlock-addons-for-elementor' ),
					'color-dodge' => esc_html__( 'Color Dodge', 'unlock-addons-for-elementor' ),
					'saturation'  => esc_html__( 'Saturation', 'unlock-addons-for-elementor' ),
					'color'      => esc_html__( 'Color', 'unlock-addons-for-elementor' ),
					'difference'  => esc_html__( 'Difference', 'unlock-addons-for-elementor' ),
					'exclusion'   => esc_html__( 'Exclusion', 'unlock-addons-for-elementor' ),
					'hue'        => esc_html__( 'Hue', 'unlock-addons-for-elementor' ),
					'luminosity' => esc_html__( 'Luminosity', 'unlock-addons-for-elementor' ),
				],
				'default'   => 'normal',
				'selectors' => [
					$heading_selector => 'mix-blend-mode: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'separator_colors',
			[
				'type' => Controls_Manager::DIVIDER,
			]
		);

		$this->start_controls_tabs( 'title_colors' );

		$this->start_controls_tab(
			'title_colors_normal',
			[
				'label' => esc_html__( 'Normal', 'unlock-addons-for-elementor' ),
			]
		);

		$this->add_control(
			'title_color',
			[
				'label'     => esc_html__( 'Text Color', 'unlock-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					$heading_selector => 'color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'title_colors_hover',
			[
				'label' => esc_html__( 'Hover', 'unlock-addons-for-elementor' ),
			]
		);

		$this->add_control(
			'title_hover_color',
			[
				'label'     => esc_html__( 'Link Color', 'unlock-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					$heading_selector . ' a:hover, ' . $heading_selector . ' a:focus' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'title_hover_color_transition_duration',
			[
				'label'     => esc_html__( 'Transition Duration', 'unlock-addons-for-elementor' ),
				'type'      => Controls_Manager::SLIDER,
				'size_units' => [ 's', 'ms', 'custom' ],
				'default'   => [
					'unit' => 's',
				],
				'selectors' => [
					$heading_selector . ' a' => 'transition-duration: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();
	}

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

		if ( '' === $settings['title'] ) {
			return;
		}

		$this->add_render_attribute( 'title', 'class', 'unlock-heading-title' );
		$this->add_inline_editing_attributes( 'title' );

		$title = wp_kses_post( $settings['title'] );

		if ( ! empty( $settings['link']['url'] ) ) {
			$this->add_link_attributes( 'url', $settings['link'] );
			$title = sprintf( '<a %1$s>%2$s</a>', $this->get_render_attribute_string( 'url' ), $title );
		}

		$tag = Utils::validate_html_tag( $settings['header_size'] );
		$title_html = sprintf( '<%1$s %2$s>%3$s</%1$s>', $tag, $this->get_render_attribute_string( 'title' ), $title );

		$animation = ! empty( $settings['entrance_animation'] ) ? $settings['entrance_animation'] : '';
		$duration  = ! empty( $settings['animation_duration'] ) ? (int) $settings['animation_duration'] : 800;
		$delay     = isset( $settings['animation_delay'] ) ? (int) $settings['animation_delay'] : 0;

		if ( $animation ) {
			$wrapper_class = 'unlock-heading-wrapper unlock-heading-animate unlock-heading-' . esc_attr( $animation );
			$wrapper_style = sprintf(
				' animation-name: unlock-%s; animation-duration: %dms; animation-delay: %dms;',
				esc_attr( $animation ),
				$duration,
				$delay
			);
			echo '<div class="' . esc_attr( $wrapper_class ) . '" style="' . esc_attr( $wrapper_style ) . '">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		echo $title_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		if ( $animation ) {
			echo '</div>';
		}
	}
}
