<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Text_Stroke;

use EcafeAddons\EcafeElementLoad;

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

class EcafeCreativeTitle extends Widget_Base {

	public function get_name() {
		return 'ecafe-creative-title';
	}

	public function get_title() {
		return esc_html__( 'Creative Title', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-creative-title-icon';
	}

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

	public function get_keywords() {
		return array( 'creative title', 'title', 'highlight', 'title creative' );
	}

	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}
	protected function register_controls() {

		$this->start_controls_section(
			'contentSection',
			array(
				'label' => esc_html__( 'Creative Title', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'title',
			array(
				'label'       => esc_html__( 'Title', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'default'     => esc_html__( 'CREATIVE', 'essential-classy-addons-for-elementor' ),
				'placeholder' => esc_html__( 'Enter Title', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'titleTag',
			array(
				'label'   => esc_html__( 'Tag', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'h1',
				'options' => \EcafeHelper::getHtmlTag( 'a' ),
			)
		);
		$this->add_control(
			'titleLink',
			array(
				'label'       => esc_html__( 'Link', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::URL,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( '#', 'essential-classy-addons-for-elementor' ),
				'default'     => array(
					'url' => '#',
				),
				'condition'   => array(
					'titleTag' => 'a',
				),
			)
		);
		$this->add_control(
			'hoverText',
			array(
				'label'       => esc_html__( 'Hover Text', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'default'     => esc_html__( 'Good Morning', 'essential-classy-addons-for-elementor' ),
				'placeholder' => esc_html__( 'Enter Hover Text', 'essential-classy-addons-for-elementor' ),
				'separator'   => 'before',
			)
		);
		$this->add_responsive_control(
			'ctSize',
			array(
				'label'           => esc_html__( 'Size', 'essential-classy-addons-for-elementor' ),
				'type'            => Controls_Manager::SLIDER,
				'range'           => array(
					'px' => array(
						'min' => 1,
						'max' => 800,
					),
				),
				'devices'         => array( 'desktop', 'tablet', 'mobile' ),
				'desktop_default' => array(
					'size' => 150,
					'unit' => 'px',
				),
				'tablet_default'  => array(
					'size' => 80,
					'unit' => 'px',
				),
				'mobile_default'  => array(
					'size' => 40,
					'unit' => 'px',
				),
				'separator'       => 'before',
				'selectors'       => array(
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span' => 'font-size: {{SIZE}}{{UNIT}} !important;line-height:  {{SIZE}}{{UNIT}} !important;',
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-text' => 'height: calc({{SIZE}}{{UNIT}} / 2);',
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-text:last-child span' => 'margin-top: calc(-{{SIZE}}{{UNIT}} / 2);',
				),
			)
		);
		$this->add_responsive_control(
			'ctHoverGap',
			array(
				'label'      => esc_html__( 'Hover Gap', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 1,
						'max' => 500,
					),
					'%'  => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 30,
				),
				'separator'  => 'before',
				'selectors'  => array(
					'{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text:first-child' => '-webkit-transform: translateY(-{{SIZE}}{{UNIT}});-moz-transform: translateY(-{{SIZE}}{{UNIT}});-o-transform: translateY(-{{SIZE}}{{UNIT}});-ms-transform: translateY(-{{SIZE}}{{UNIT}});transform: translateY(-{{SIZE}}{{UNIT}});',
					'{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text:last-child' => '-webkit-transform: translateY({{SIZE}}{{UNIT}});-moz-transform: translateY({{SIZE}}{{UNIT}});-o-transform: translateY({{SIZE}}{{UNIT}});-ms-transform: translateY({{SIZE}}{{UNIT}});transform: translateY({{SIZE}}{{UNIT}});',
				),
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdemolink = esc_url( 'https://ecaddons.com/elements/creative-text-title-info-elementor/' );
		$ecwidgetdoclink  = esc_url( 'https://ecaddons.com/docs/creative-text-title-info-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		/*title start*/
		$this->start_controls_section(
			'titlesection',
			array(
				'label' => esc_html__( 'Title', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'titleTypo',
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span',
			)
		);
		$this->start_controls_tabs( 'tabsTitletyle' );
		$this->start_controls_tab(
			'tabTitleNorm',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'titleColorN',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'titleBgN',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'titleBorderN',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span',
			)
		);
		$this->add_responsive_control(
			'titleBrN',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'titleShadowN',
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			array(
				'name'     => 'titleTextShadowN',
				'label'    => esc_html__( 'Text Shadow', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Stroke::get_type(),
			array(
				'name'     => 'titleTextStrokeN',
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-text span',
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabTitleHover',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'titleopacity',
			array(
				'label'     => __( 'Opacity', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 0,
				'max'       => 1,
				'step'      => 0.1,
				'default'   => 0.5,
				'selectors' => array(
					'{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text' => 'opacity:{{VALUE}};',
				),
			)
		);
		$this->add_control(
			'titleColorH',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text span' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'titleBgH',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text span',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'titleBorderH',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text span',
			)
		);
		$this->add_responsive_control(
			'titleBrH',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text span' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'titleShadowH',
				'selector' => '{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text span',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			array(
				'name'     => 'titleTextShadowH',
				'label'    => esc_html__( 'Text Shadow', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text span',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Stroke::get_type(),
			array(
				'name'     => 'titleTextStrokeH',
				'selector' => '{{WRAPPER}} .eca-ct-wrapper:hover .eca-ct-text span',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		/*title end*/

		/*hover start*/
		$this->start_controls_section(
			'hoversection',
			array(
				'label' => esc_html__( 'Hover Text', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'hoverTextPadding',
			array(
				'label'      => esc_html__( 'Padding', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'hoverTextMargin',
			array(
				'label'      => esc_html__( 'Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'hoverTextTypo',
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext',
			)
		);
		$this->add_control(
			'hoverTextColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'hoverTextBg',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'hoverTextBorder',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext',
			)
		);
		$this->add_responsive_control(
			'hoverTextBr',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'hoverTextShadow',
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			array(
				'name'     => 'hoverTextTextShadow',
				'label'    => esc_html__( 'Text Shadow', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Stroke::get_type(),
			array(
				'name'     => 'hoverTextTextStroke',
				'selector' => '{{WRAPPER}} .eca-ct-wrapper .eca-ct-hovertext',
			)
		);
		$this->end_controls_section();
		/*hover end*/
	}

	protected function render() {
		$settings  = $this->get_settings_for_display();
		$title     = ! empty( $settings['title'] ) ? esc_attr( $settings['title'] ) : '';
		$titleTag  = ! empty( $settings['titleTag'] ) ? $settings['titleTag'] : 'h1';
		$hoverText = ! empty( $settings['hoverText'] ) ? esc_attr( $settings['hoverText'] ) : '';

		$output = '';
		if ( ! empty( $title ) ) {
			if ( $titleTag == 'a' && ! empty( $settings['titleLink']['url'] ) ) {
				$this->add_link_attributes( 'titlelink', $settings['titleLink'] );
				$this->add_render_attribute( 'titlelink', 'class', 'eca-ct-wrapper' );

				$output .= '<' . \EcafeHelper::check_and_validate_html_tag( $titleTag ) . ' ' . $this->get_render_attribute_string( 'titlelink' ) . '>';
			} else {
				$output .= '<' . \EcafeHelper::check_and_validate_html_tag( $titleTag ) . ' class="eca-ct-wrapper">';
			}
				$output .= '<div class="eca-ct-text"><span>' . esc_html( $title ) . '</span></div>';
			if ( ! empty( $hoverText ) ) {
				$output .= '<div class="eca-ct-hovertext">' . esc_html( $hoverText ) . '</div>';
			}
				$output .= '<div class="eca-ct-text"><span>' . esc_html( $title ) . '</span></div>';
			$output     .= '</' . \EcafeHelper::check_and_validate_html_tag( $titleTag ) . '>';
		}

		echo wp_kses_normalize_entities( $output );
	}
}
