<?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 Ecafe_Post_Title extends Widget_Base {

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

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

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

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

	public function get_keywords() {
		return array( 'post', 'post title', 'title', 'dynamic' );
	}

	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__( 'Post Title', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$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(
			'titletextAlign',
			array(
				'label'       => esc_html__( 'Text Alignment', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::CHOOSE,
				'options'     => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors'   => array(
					'{{WRAPPER}} .ec-post-title-wrapper' => 'text-align: {{VALUE}};',
				),
				'label_block' => false,
			)
		);
		$this->add_control(
			'titleJustifyAlign',
			array(
				'label'       => esc_html__( 'Justify Alignment', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::CHOOSE,
				'options'     => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors'   => array(
					'{{WRAPPER}} .ec-post-title-wrapper' => 'justify-content: {{VALUE}};',
				),
				'label_block' => false,
			)
		);
		$this->add_control(
			'titleLimit',
			array(
				'label' => esc_html__( 'Limit of Word', 'essential-classy-addons-for-elementor' ),
				'type'  => Controls_Manager::NUMBER,
				'min'   => 1,
				'max'   => 1000,
				'step'  => 1,
			)
		);
		$this->add_control(
			'titleAfter',
			array(
				'label'     => esc_html__( 'After Title', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => '...',
				'dynamic'   => array( 'active' => true ),
				'condition' => array(
					'titleLimit!' => '',
				),
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdoclink = esc_url( 'https://ecaddons.com/docs/post-title-blog-builder-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		/*title start*/
		$this->start_controls_section(
			'titleStyling',
			array(
				'label' => esc_html__( 'Title', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'titlePadding',
			array(
				'label'      => esc_html__( 'Padding', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-post-title-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'titleMargin',
			array(
				'label'      => esc_html__( 'Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-post-title-wrapper' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'titleTypo',
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper',
			)
		);
		$this->start_controls_tabs( 'tabsTitletyle' );
		$this->start_controls_tab(
			'tabTitleN',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'titleColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-post-title-wrapper' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'titleBg',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'titleBorder',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper',
			)
		);
		$this->add_responsive_control(
			'titleBr',
			array(
				'label'      => esc_html__( 'Border Radius', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .ec-post-title-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'titleShadow',
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			array(
				'name'     => 'titleTextShadow',
				'label'    => esc_html__( 'Text Shadow', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Stroke::get_type(),
			array(
				'name'     => 'titleTextStroke',
                'label'    => esc_html__( 'Text Stroke', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper',
			)
		);
		$this->add_control(
			'titleTransition',
			array(
				'label'       => esc_html__( 'Transition css', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'all .3s linear', 'essential-classy-addons-for-elementor' ),
				'selectors'   => array(
					'{{WRAPPER}} .ec-post-title-wrapper' => 'transition:{{VALUE}};',
				),
			)
		);
		$this->add_control(
			'titleTransform',
			array(
				'label'       => esc_html__( 'Transform css', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'skew(-20deg)', 'essential-classy-addons-for-elementor' ),
				'selectors'   => array(
					'{{WRAPPER}} .ec-post-title-wrapper' => 'transform: {{VALUE}};-ms-transform: {{VALUE}};-moz-transform: {{VALUE}};-webkit-transform: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabTitleHover',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'titleColorH',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-post-title-wrapper:hover' => 'color: {{VALUE}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'titleBgH',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper:hover',
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'titleBorderH',
				'label'    => esc_html__( 'Border', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper:hover',
			)
		);
		$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}} .ec-post-title-wrapper:hover' => '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}} .ec-post-title-wrapper:hover',
			)
		);
		$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}} .ec-post-title-wrapper:hover',
			)
		);
		$this->add_group_control(
			Group_Control_Text_Stroke::get_type(),
			array(
				'name'     => 'titleTextStrokeH',
                'label'    => esc_html__( 'Text Stroke', 'essential-classy-addons-for-elementor' ),
				'selector' => '{{WRAPPER}} .ec-post-title-wrapper:hover',
			)
		);
		$this->add_control(
			'titleTransitionH',
			array(
				'label'       => esc_html__( 'Transition css', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'all .3s linear', 'essential-classy-addons-for-elementor' ),
				'selectors'   => array(
					'{{WRAPPER}} .ec-post-title-wrapper:hover' => 'transition:{{VALUE}};',
				),
			)
		);
		$this->add_control(
			'titleTransformH',
			array(
				'label'       => esc_html__( 'Transform css', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'skew(-20deg)', 'essential-classy-addons-for-elementor' ),
				'selectors'   => array(
					'{{WRAPPER}} .ec-post-title-wrapper:hover' => 'transform: {{VALUE}};-ms-transform: {{VALUE}};-moz-transform: {{VALUE}};-webkit-transform: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
		/*title end*/
	}

	protected function render() {

		$settings   = $this->get_settings_for_display();
		$titleTag   = ! empty( $settings['titleTag'] ) ? $settings['titleTag'] : 'h1';
		$titleLimit = ( ! empty( $settings['titleLimit'] ) ) ? $settings['titleLimit'] : '';

		$postId = get_the_ID();

		if ( is_archive() ) {
			add_filter(
				'get_the_archive_title',
				function ( $getTitleC ) {
					if ( is_category() ) {
						$getTitleC = single_cat_title( '', false );
					} elseif ( is_tag() ) {
						$getTitleC = single_tag_title( '', false );
					} elseif ( is_author() ) {
						$getTitleC = get_the_author();
					} elseif ( is_tax() ) {
						$getTitleC = single_term_title( '', false );
					} elseif ( is_post_type_archive() ) {
						$getTitleC = post_type_archive_title( '', false );
					} elseif ( is_search() ) {
						$getTitleC = get_search_query();
					}
					return $getTitleC;
				}
			);
			$getTitle = wp_trim_words( get_the_archive_title() );
			if ( ! empty( $titleLimit ) ) {
				$getTitle = wp_trim_words( get_the_archive_title(), $titleLimit );
			}
		} else {
			$getTitle = get_the_title( $postId );
			if ( ! empty( $titleLimit ) ) {
				$titleAfter = ! empty( $settings['titleAfter'] ) ? esc_attr( $settings['titleAfter'] ) : '';
				$getTitle   = substr( wp_trim_words( get_the_title( $postId ) ), 0, $titleLimit ) . $titleAfter;
			}
		}

		$href = '';
		if ( ! empty( $titleTag ) && $titleTag == 'a' ) {
			$href = 'href="' . get_the_permalink() . '"';
		}
		$output = '<' . \EcafeHelper::check_and_validate_html_tag( $titleTag ) . ' ' . $href . ' class="ec-post-title-wrapper">' . $getTitle . '</' . \EcafeHelper::check_and_validate_html_tag( $titleTag ) . '>';

		echo wp_kses_normalize_entities( $output );
	}
}
