<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
use Elementor\Core\Schemes\Typography;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;

use EcafeAddons\EcafeElementLoad;

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

class Ecafe_Text_Background extends Widget_Base {

	public function get_name() {
		return 'ecafe-text-background';
	}

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

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-text-background-icon';
	}

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


	public function get_keywords() {
		return array( 'text background', 'gif', 'background', 'creative text', 'text style enhancer' );
	}

	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__( 'Text Background', '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__( 'Title', 'essential-classy-addons-for-elementor' ),
				'placeholder' => esc_html__( 'Enter Title', 'essential-classy-addons-for-elementor' ),
				'separator'   => 'before',
			)
		);
		$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->start_controls_tabs( 'tabsBgtyle' );
		$this->start_controls_tab(
			'tabBgN',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'tbNormalBg',
				'label'    => esc_html__( 'Normal Background', 'essential-classy-addons-for-elementor' ),
				'types'    => array( 'classic' ),
				'selector' => '{{WRAPPER}} .eca-text-background-wrapper .eca-tb-title',
			)
		);
		$this->add_control(
			'tbNormalBgTransition',
			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}} .eca-text-background-wrapper .eca-tb-title' => 'transition:{{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabBgH',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'tbHoverBg',
				'label'    => esc_html__( 'Hover Background', 'essential-classy-addons-for-elementor' ),
				'types'    => array( 'classic' ),
				'selector' => '{{WRAPPER}} .eca-text-background-wrapper .eca-tb-title:hover',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->add_responsive_control(
			'contentAlign',
			array(
				'label'     => esc_html__( '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',
					),
				),
				'default'   => 'center',
				'separator' => 'before',
				'selectors' => array(
					'{{WRAPPER}} .eca-text-background-wrapper' => 'justify-content:{{VALUE}};',
				),
			)
		);
		$this->end_controls_section();

		/*Tilt Effect Options*/
		include ECAFE_PATH . 'element/widgets/ecafe-tilt-options.php';

		/*Tippy Tooltip Options*/
		include ECAFE_PATH . 'element/widgets/ecafe-tippy-options.php';

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

		/*text style start*/
		$this->start_controls_section(
			'sectionTitleStyling',
			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}} .eca-text-background-wrapper .eca-tb-title' => '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}} .eca-text-background-wrapper .eca-tb-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'titleTypography',
				'selector' => '{{WRAPPER}} .eca-text-background-wrapper .eca-tb-title',
			)
		);
		$this->end_controls_section();
		/*text style start*/
	}

	protected function render() {

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

		$OPTitle = '';
		$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-tb-title' );

				$OPTitle = '<' . \EcafeHelper::check_and_validate_html_tag( $TitleTag ) . ' ' . $this->get_render_attribute_string( 'titlelink' ) . '>' . esc_html( $Title ) . '</' . \EcafeHelper::check_and_validate_html_tag( $TitleTag ) . '>';
			} else {
				$OPTitle = '<' . \EcafeHelper::check_and_validate_html_tag( $TitleTag ) . ' class="eca-tb-title">' . esc_html( $Title ) . '</' . \EcafeHelper::check_and_validate_html_tag( $TitleTag ) . '>';
			}
			$tiltclass  = '';
			$tiltrender = '';
			if ( isset( $settings['tiltEffect'] ) && $settings['tiltEffect'] === 'yes' ) {
				/*Tilt Effect Render*/
				include ECAFE_PATH . 'element/widgets/ecafe-tilt-render.php';
			}
			$tippyclass  = '';
			$tippyrender = '';
			if ( isset( $settings['tippyTooltip'] ) && $settings['tippyTooltip'] === 'yes' ) {
				/*Tippy Tooltip Render*/
				include ECAFE_PATH . 'element/widgets/ecafe-tippy-render.php';
			}
			$output     .= '<div class="eca-text-background-wrapper ' . esc_attr( $tiltclass ) . ' ' . esc_attr( $tippyclass ) . '" ' . $tiltrender . ' ' . $tippyrender . '>';
				$output .= $OPTitle;
			$output     .= '</div>';
		}

		echo wp_kses_normalize_entities( $output );
	}
}
