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

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

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

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

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

	public function get_keywords() {
		return array( 'wck', 'wck phone', 'phone' );
	}
	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__( 'WCK : Phone', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'wckGroupName',
			array(
				'label'       => esc_html__( 'Group Name', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'Enter Group Name', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'wckPhone',
			array(
				'label'       => esc_html__( 'Phone Slug Name', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'Enter Phone Slug Name', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'wckTextType',
			array(
				'label'   => esc_html__( 'Source', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'static',
				'options' => array(
					'static'  => esc_html__( 'Static', 'essential-classy-addons-for-elementor' ),
					'dynamic' => esc_html__( 'Dynamic', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'wckTextStatic',
			array(
				'label'       => esc_html__( 'Text', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'Enter Name', 'essential-classy-addons-for-elementor' ),
				'condition'   => array(
					'wckTextType' => 'static',
				),
			)
		);
		$this->add_control(
			'wckTextDynamic',
			array(
				'label'       => esc_html__( 'Text Slug Name', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'Enter Slug Name', 'essential-classy-addons-for-elementor' ),
				'condition'   => array(
					'wckTextType' => 'dynamic',
				),
			)
		);
		$this->add_control(
			'ecPrefixText',
			array(
				'label'       => esc_html__( 'Prefix', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'Enter Prefix Text', 'essential-classy-addons-for-elementor' ),
				'separator'   => 'before',
			)
		);
		$this->add_control(
			'ecPostfixText',
			array(
				'label'       => esc_html__( 'Postfix', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => array( 'active' => true ),
				'placeholder' => esc_html__( 'Enter Postfix Text', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->end_controls_section();

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

		$this->start_controls_section(
			'wckPhoneStyling',
			array(
				'label' => esc_html__( 'Wck Phone', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'wckPhoneTypography',
				'selector' => '{{WRAPPER}} .eca-wck-phone',
			)
		);
		$this->add_responsive_control(
			'wckPhoneAlign',
			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',
					),
					'space-between' => array(
						'title' => esc_html__( 'Space Between', 'essential-classy-addons-for-elementor' ),
						'icon'  => 'eicon-h-align-stretch',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .eca-wck-phone' => 'justify-content:{{VALUE}};',
				),
			)
		);
		$this->start_controls_tabs( 'tabswckPhoneStyle' );
		$this->start_controls_tab(
			'tabwckPhoneNorm',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'wckPhoneColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-wck-phone' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabwckPhoneHover',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'wckPhoneColorHover',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-wck-phone:hover' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

		$this->start_controls_section(
			'wckPrefixStyling',
			array(
				'label' => esc_html__( 'Prefix', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'wckPrefixMinWidth',
			array(
				'label'      => esc_html__( 'Min. Width', '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,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .eca-wck-prefix' => 'min-width:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'wckPrefixMargin',
			array(
				'label'      => esc_html__( 'Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-wck-prefix' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'wckPrefixTypo',
				'selector' => '{{WRAPPER}} .eca-wck-prefix',
			)
		);
		$this->add_control(
			'wckPrefixColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-wck-prefix' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'wckPostfixStyling',
			array(
				'label' => esc_html__( 'Postfix', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'wckPostfixMinWidth',
			array(
				'label'      => esc_html__( 'Min. Width', '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,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .eca-wck-postfix' => 'min-width:{{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->add_responsive_control(
			'wckPostfixMargin',
			array(
				'label'      => esc_html__( 'Margin', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .eca-wck-postfix' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'wckPostfixTypo',
				'selector' => '{{WRAPPER}} .eca-wck-postfix',
			)
		);
		$this->add_control(
			'wckPostfixColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-wck-postfix' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( defined( 'WCK_PLUGIN_VERSION' ) ) {
			$wckGroupName = ! empty( $settings['wckGroupName'] ) ? $settings['wckGroupName'] : '';
			if ( $wckGroupName ) {
				if ( ! empty( $settings['wckPhone'] ) ) {
					echo '<a class="eca-wck-phone" href=';
					echo 'tel:';
						echo get_cfc_field( $wckGroupName, $settings['wckPhone'] );
					echo '>';
						$wckTextType    = ! empty( $settings['wckTextType'] ) ? $settings['wckTextType'] : 'static';
						$wckTextStatic  = ! empty( $settings['wckTextStatic'] ) ? $settings['wckTextStatic'] : '';
						$wckTextDynamic = ! empty( $settings['wckTextDynamic'] ) ? $settings['wckTextDynamic'] : '';
						$ecPrefixText   = ! empty( $settings['ecPrefixText'] ) ? $settings['ecPrefixText'] : '';
						$ecPostfixText  = ! empty( $settings['ecPostfixText'] ) ? $settings['ecPostfixText'] : '';
					if ( ! empty( $ecPrefixText ) ) {
						echo '<div class="eca-wck-prefix">' . esc_html( $ecPrefixText ) . '</div>';
					}
					if ( ! empty( $wckTextType ) && $wckTextType == 'static' && ! empty( $wckTextStatic ) ) {
						echo esc_attr( $wckTextStatic );
					} elseif ( ! empty( $wckTextType ) && $wckTextType == 'dynamic' && ! empty( $wckTextDynamic ) ) {
						echo get_cfc_field( $wckGroupName, $wckTextDynamic );
					} elseif ( ( ! empty( $wckTextType ) && $wckTextType == 'static' ) || ( ! empty( $wckTextType ) && $wckTextType == 'dynamic' ) || ( ! empty( $wckTextDynamic ) || ! empty( $wckTextStatic ) ) ) {
						echo get_cfc_field( $wckGroupName, $settings['wckPhone'] );
					}
					if ( ! empty( $ecPostfixText ) ) {
						echo '<div class="eca-wck-postfix">' . esc_html( $ecPostfixText ) . '</div>';
					}
					echo '</a>';
				}
			}
		}
	}
}
