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

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

	public function get_title() {
		return esc_html__( 'Meta Box : Url', 'essential-classy-addons-for-elementor' );
	}

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

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

	public function get_keywords() {
		return array( 'meta box', 'metabox', 'url', 'metabox url', 'link' );
	}
	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__( 'Meta Box : Url', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'metaboxUrl',
			array(
				'label'       => esc_html__( 'Select Field', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::SELECT,
				'label_block' => true,
				'multiple'    => false,
				'groups'      => \EcafeHelper::ecGetMetaBoxFields( 'url' ),
			)
		);
		$this->add_control(
			'metaboxTextType',
			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(
			'metaboxTextStatic',
			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(
					'metaboxTextType' => 'static',
				),
			)
		);
		$this->add_control(
			'metaboxTextDynamic',
			array(
				'label'       => esc_html__( 'Select Text Field', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::SELECT,
				'label_block' => true,
				'multiple'    => false,
				'groups'      => \EcafeHelper::ecGetMetaBoxFields( 'text' ),
				'condition'   => array(
					'metaboxTextType' => '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/meta-box-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		$this->start_controls_section(
			'metaUrlStyling',
			array(
				'label' => esc_html__( 'Meta URL', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'metaUrlTypography',
				'selector' => '{{WRAPPER}} .eca-metabox-url',
			)
		);
		$this->add_responsive_control(
			'metaUrlAlign',
			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-metabox-url' => 'justify-content:{{VALUE}};',
				),
			)
		);
		$this->start_controls_tabs( 'tabsmetaUrlStyle' );
		$this->start_controls_tab(
			'tabmetaUrlNorm',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'metaUrlColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-metabox-url' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabmetaUrlHover',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'metaUrlColorHover',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .eca-metabox-url:hover' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

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

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

	protected function render() {

		$settings = $this->get_settings_for_display();
		if ( defined( 'RWMB_VER' ) ) {
			if ( ! empty( $settings['metaboxUrl'] ) ) {
				echo '<a class="eca-metabox-url" href=';
					echo rwmb_meta( $settings['metaboxUrl'] );
				echo '>';
					$metaboxTextType    = ! empty( $settings['metaboxTextType'] ) ? $settings['metaboxTextType'] : 'static';
					$metaboxTextStatic  = ! empty( $settings['metaboxTextStatic'] ) ? $settings['metaboxTextStatic'] : '';
					$metaboxTextDynamic = ! empty( $settings['metaboxTextDynamic'] ) ? $settings['metaboxTextDynamic'] : '';

					$ecPrefixText  = ! empty( $settings['ecPrefixText'] ) ? $settings['ecPrefixText'] : '';
					$ecPostfixText = ! empty( $settings['ecPostfixText'] ) ? $settings['ecPostfixText'] : '';
				if ( ! empty( $ecPrefixText ) ) {
					echo '<div class="eca-meta-prefix">' . esc_html( $ecPrefixText ) . '</div>';
				}
				if ( ! empty( $metaboxTextType ) && $metaboxTextType == 'static' && ! empty( $metaboxTextStatic ) ) {
					echo esc_attr( $metaboxTextStatic );
				} elseif ( ! empty( $metaboxTextType ) && $metaboxTextType == 'dynamic' && ! empty( $metaboxTextDynamic ) ) {
					echo rwmb_meta( $metaboxTextDynamic );
				} elseif ( ( ! empty( $metaboxTextType ) && $metaboxTextType == 'static' ) || ( ! empty( $metaboxTextType ) && $metaboxTextType == 'dynamic' ) || ( ! empty( $metaboxTextDynamic ) || ! empty( $metaboxTextStatic ) ) ) {
					echo rwmb_meta( $settings['metaboxUrl'] );
				}
				if ( ! empty( $ecPostfixText ) ) {
					echo '<div class="eca-meta-postfix">' . esc_html( $ecPostfixText ) . '</div>';
				}
				echo '</a>';
			}
		}
	}
}
