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

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

	public function get_title() {
		return esc_html__( 'Meta Box : Wysiwyg', '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', 'wysiwyg', 'metabox wysiwyg', 'visual' );
	}
	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 : Wysiwyg', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'metaboxWsiwyg',
			array(
				'label'       => esc_html__( 'Select Field', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::SELECT,
				'label_block' => true,
				'multiple'    => false,
				'groups'      => \EcafeHelper::ecGetMetaBoxFields( 'wysiwyg' ),
			)
		);
		$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(
			'metaWysiwygStyling',
			array(
				'label' => esc_html__( 'Meta Wysiwyg', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'metaWysiwygTypography',
				'selector' => '{{WRAPPER}} .ec-metabox-wsiwyg-wrapper,{{WRAPPER}} .ec-metabox-wsiwyg-wrapper p',
			)
		);
		$this->add_responsive_control(
			'metaWysiwygAlign',
			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}} .ec-metabox-wsiwyg-wrapper,{{WRAPPER}} .ec-metabox-wsiwyg-wrapper p' => 'text-align:{{VALUE}};',
				),
			)
		);
		$this->add_responsive_control(
			'metaWysiwygCAlign',
			array(
				'label'     => esc_html__( 'Content 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}} .ec-metabox-wsiwyg-wrapper' => 'justify-content:{{VALUE}};',
				),
			)
		);
		$this->start_controls_tabs( 'tabsmetaWysiwygStyle' );
		$this->start_controls_tab(
			'tabmetaWysiwygNorm',
			array(
				'label' => esc_html__( 'Normal', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'metaWysiwygColor',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-metabox-wsiwyg-wrapper,{{WRAPPER}} .ec-metabox-wsiwyg-wrapper p' => 'color: {{VALUE}};',
				),
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tabmetaWysiwygHover',
			array(
				'label' => esc_html__( 'Hover', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'metaWysiwygColorHover',
			array(
				'label'     => esc_html__( 'Color', 'essential-classy-addons-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .ec-metabox-wsiwyg-wrapper:hover,{{WRAPPER}} .ec-metabox-wsiwyg-wrapper:hover p' => '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' ) ) {
			$metaboxWsiwyg = ! empty( $settings['metaboxWsiwyg'] ) ? $settings['metaboxWsiwyg'] : '';
			if ( $metaboxWsiwyg ) {
				echo '<div class="ec-metabox-wsiwyg-wrapper">';
					$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>';
				}
					$value = rwmb_meta( $metaboxWsiwyg );
					echo do_shortcode( wpautop( $value ) );
				if ( ! empty( $ecPostfixText ) ) {
					echo '<div class="eca-meta-postfix">' . esc_html( $ecPostfixText ) . '</div>';
				}
				echo '</div>';
			}
		}
	}
}
