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

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

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

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

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


	public function get_keywords() {
		return array( 'dropcap', 'drop cap', 'capital', 'first capital' );
	}

	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__( 'Dropcap', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'contentLayout',
			array(
				'label'   => esc_html__( 'Layout', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'layout1',
				'options' => array(
					'layout1' => esc_html__( 'Layout 1', 'essential-classy-addons-for-elementor' ),
					'layout2' => esc_html__( 'Layout 2', 'essential-classy-addons-for-elementor' ),
					'layout3' => esc_html__( 'Layout 3', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'contentSrc',
			array(
				'label'       => esc_html__( 'Content', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXTAREA,
				'default'     => esc_html__( 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using Content here.', 'essential-classy-addons-for-elementor' ),
				'placeholder' => esc_html__( 'Enter your content here', 'essential-classy-addons-for-elementor' ),
				'dynamic'     => array( 'active' => true ),
			)
		);
		$this->add_control(
			'contentSrcBackend',
			array(
				'label'       => esc_html__( 'Background Text Without Space', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'A', 'essential-classy-addons-for-elementor' ),
				'placeholder' => esc_html__( 'Enter your Text', 'essential-classy-addons-for-elementor' ),
				'dynamic'     => array( 'active' => true ),
				'condition'   => array(
					'contentLayout' => 'layout3',
				),
			)
		);
		$this->add_responsive_control(
			'contentSrcBackendAlign',
			array(
				'label'     => esc_html__( 'Background Text Alignment', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'csbalignleft'   => array(
						'title' => esc_html__( 'Left', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'csbaligncenter' => array(
						'title' => esc_html__( 'Center', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'csbaligright'   => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'csbaligncenter',
				'condition' => array(
					'contentLayout' => 'layout3',
				),
			)
		);
		$this->add_responsive_control(
			'dTextAlign',
			array(
				'label'     => esc_html__( 'Text Alignment', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'    => 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',
					),
					'right'   => array(
						'title' => esc_html__( 'Right', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
					'justify' => array(
						'title' => esc_html__( 'Justify', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-h-align-stretch',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .eca-dropcap-wrapper.layout3' => 'text-align:{{VALUE}};',
				),
				'condition' => array(
					'contentLayout' => 'layout3',
				),
			)
		);
		$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/dropcap/' );
		$ecwidgetdoclink  = esc_url( 'https://ecaddons.com/docs/dropcap-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		/*general*/
		$this->start_controls_section(
			'generalStyling',
			array(
				'label' => esc_html__( 'General', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'generalTypo',
				'selector' => '{{WRAPPER}} .eca-dropcap-wrapper',
			)
		);
		$this->add_control(
			'generalColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-dropcap-wrapper' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		/*general*/

		/*first latter*/
		$this->start_controls_section(
			'firstLatterStyling',
			array(
				'label'     => esc_html__( 'First Latter', 'essential-classy-addons-for-elementor' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'contentLayout' => array( 'layout1', 'layout2' ),
				),
			)
		);
		$this->add_responsive_control(
			'firstLatterPadding',
			array(
				'label'      => esc_html__( 'Padding', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-dropcap-wrapper:first-letter' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'firstLatterTypo',
				'selector' => '{{WRAPPER}} .eca-dropcap-wrapper:first-letter',
			)
		);
		$this->add_control(
			'firstLatterColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-dropcap-wrapper:first-letter' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		/*first latter*/

		/*background text*/
		$this->start_controls_section(
			'backgroundTextStyling',
			array(
				'label'     => esc_html__( 'Background Text', 'essential-classy-addons-for-elementor' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'contentLayout' => 'layout3',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'backgroundTextTypo',
				'selector' => '{{WRAPPER}} .eca-dropcap-wrapper.layout3:before ',
			)
		);
		$this->add_control(
			'backgroundTextColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-dropcap-wrapper.layout3:before ' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
		/*background text*/
	}

	protected function render() {

		$settings               = $this->get_settings_for_display();
		$contentLayout          = ! empty( $settings['contentLayout'] ) ? $settings['contentLayout'] : 'layout1';
		$contentSrc             = ! empty( $settings['contentSrc'] ) ? $settings['contentSrc'] : '';
		$contentSrcBackend      = ! empty( $settings['contentSrcBackend'] ) ? $settings['contentSrcBackend'] : '';
		$contentSrcBackendAlign = ! empty( $settings['contentSrcBackendAlign'] ) ? $settings['contentSrcBackendAlign'] : 'csbaligncenter';

		$DataAttr = $csba = '';
		if ( $contentLayout === 'layout3' && ! empty( $contentSrcBackend ) ) {
			$contentSrcBackend = preg_replace( '/[^a-zA-Z0-9_\s-]/', '', $contentSrcBackend );
			$DataAttr          = 'data-first-letter=' . $contentSrcBackend;
			$csba              = $contentSrcBackendAlign;
		}
		$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-dropcap-wrapper ' . esc_attr( $contentLayout ) . ' ' . esc_attr( $csba ) . ' ' . esc_attr( $tiltclass ) . ' ' . esc_attr( $tippyclass ) . '" ' . esc_attr( $DataAttr ) . ' ' . $tiltrender . ' ' . $tippyrender . '>' . wp_kses_post( $contentSrc ) . '</div>';

		echo wp_kses_normalize_entities( $Output );
	}
}
