<?php
/**
 * Class: Module
 *
 * Description:
 *
 * @since 2.1.7
 */

namespace EACCustomWidgets\Includes\DisplayConditions;

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

use EACCustomWidgets\EAC_Plugin;
use EACCustomWidgets\Includes\DisplayConditions\Controller;

use Elementor\Controls_Manager;
use Elementor\Element_Base;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Group_Control_Border;
use Elementor\Repeater;
use Elementor\Plugin;

class Module {

	/**
	 * @var $target_elements
	 *
	 * La liste des éléments cibles
	 */
	private $target_elements = array( 'container', 'section', 'column', 'widget' );

	/**
	 * @var $controller
	 *
	 * Objet de la liste des controls pour chaque condition
	 * Active les filtres nécessaires pour l'affichage
	 * Appel les méthodes de compaison pour chaque control
	 */
	private $controller = null;

	/**
	 * @var $instance
	 *
	 * Garantir une seule instance de la class
	 */
	private static $instance = null;

	/** Constructeur */
	public function __construct() {
		add_action( 'elementor/element/after_section_end', array( $this, 'inject_controls' ), 9999, 2 );
		/**add_action( 'elementor/element/section/_section_responsive/after_section_end', array( $this, 'inject_controls' ), 10 );
		add_action( 'elementor/element/column/_section_responsive/after_section_end', array( $this, 'inject_controls' ), 10 );
		add_action( 'elementor/element/common/_section_responsive/after_section_end', array( $this, 'inject_controls' ), 10 );*/

		add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'enqueue_editor_scripts' ) );
	}

	/**
	 * enqueue_editor_scripts
	 *
	 * Enqueue le script dans l'éditeur
	 */
	public function enqueue_editor_scripts(): void {
		wp_enqueue_script( 'eac-indicator-conditions', EAC_Plugin::instance()->get_script_url( 'assets/js/elementor/eac-indicator-conditions' ), array(), EAC_PLUGIN_VERSION, true );
	}

	/** Singleton de la class */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * inject_controls
	 *
	 * Inject le control après la section 'section_effects' Advanced tab
	 * pour les colonnes
	 *
	 * @param Element_Base $element    L'élément en cours d'édition
	 * @param String       $section_id L'ID de l'élément
	 */
	public function inject_controls( $element, $section_id ): void {

		if ( ! $element instanceof Element_Base ) {
			return;
		}

		if ( '_section_responsive' !== $section_id || ! in_array( $element->get_type(), $this->target_elements, true ) ) {
			return;
		}

		if ( is_null( $this->controller ) ) {
			$this->controller = new Controller();
		}

		$element->start_controls_section(
			'eac_custom_element_condition',
			array(
				'label' => esc_html__( 'Display conditions', 'eac-components' ),
				'tab'   => Controls_Manager::TAB_ADVANCED,
			)
		);

			$element->add_control(
				'element_condition_active',
				array(
					'label'        => esc_html__( 'Display conditions', 'eac-components' ),
					'type'         => Controls_Manager::SWITCHER,
					'label_on'     => esc_html( 'On' ),
					'label_off'    => esc_html( 'Off' ),
					'return_value' => 'yes',
					'default'      => '',
					'prefix_class' => 'eac-conditions-',
				)
			);

			$element->add_control(
				'element_condition_usage',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw' => sprintf(
						/* translators: 1: Link opening tag, 2: Link closing tag. */
						esc_html__( 'Implement %1$sconditions to show/hide elements%2$s', 'eac-components' ),
						'<a href="https://elementor-addon-components.com/how-to-set-display-conditions-with-elementor/#list-of-available-conditions" target="_blank" rel="noopener noreferrer">',
						'</a>',
					),
					'content_classes' => 'elementor-descriptor',
					'condition'       => array( 'element_condition_active' => 'yes' ),
				)
			);

			$element->add_control(
				'element_condition_when',
				array(
					'label'       => esc_html__( 'Hide element when', 'eac-components' ),
					'type'        => Controls_Manager::SELECT,
					'options'     => array(
						'all' => esc_html__( 'All conditions are met', 'eac-components' ),
						'any' => esc_html__( 'Any of the conditions is met', 'eac-components' ),
					),
					'default'     => 'all',
					'label_block' => false,
					'render_type' => 'none',
					'separator'   => 'before',
					'condition'   => array( 'element_condition_active' => 'yes' ),
				)
			);

			$repeater = new Repeater();

			$repeater->add_control(
				'element_condition_key',
				array(
					'label'       => esc_html__( 'Condition', 'eac-components' ),
					'type'        => Controls_Manager::SELECT,
					'groups'      => $this->controller->get_conditions_list(),
					'default'     => 'day_week',
					'label_block' => true,
					'render_type' => 'none',
				)
			);

			$repeater->add_control(
				'element_condition_operateur_date',
				array(
					'label'       => esc_html__( 'Comparison operator', 'eac-components' ),
					'type'        => Controls_Manager::CHOOSE,
					'options'     => array(
						'less_than' => array(
							'title' => esc_html__( 'Less than', 'eac-components' ),
							'icon'  => 'eicon-chevron-left eac-condition-choose',
						),
						'equal'     => array(
							'title' => esc_html__( 'Equal', 'eac-components' ),
							'icon'  => 'fas fa-equals eac-condition-choose',
						),
						'not_equal' => array(
							'title' => esc_html__( 'Not equal', 'eac-components' ),
							'icon'  => 'fas fa-not-equal eac-condition-choose',
						),
						'more_than' => array(
							'title' => esc_html__( 'More than', 'eac-components' ),
							'icon'  => 'eicon-chevron-right eac-condition-choose',
						),
					),
					'default'     => 'equal',
					'toggle'      => false,
					'label_block' => true,
					'render_type' => 'none',
					'condition'   => array(
						'element_condition_key' => 'date_compare',
					),
				)
			);

			$repeater->add_control(
				'element_condition_operateur_range',
				array(
					'label'       => esc_html__( 'Comparison operator', 'eac-components' ),
					'type'        => Controls_Manager::CHOOSE,
					'options'     => array(
						'in'     => array(
							'title' => esc_html__( 'In the list', 'eac-components' ),
							'icon'  => 'eicon-order-end eac-condition-choose',
						),
						'not_in' => array(
							'title' => esc_html__( 'Out of the list', 'eac-components' ),
							'icon'  => 'eicon-nowrap eac-condition-choose',
						),
					),
					'default'     => 'in',
					'toggle'      => false,
					'label_block' => true,
					'render_type' => 'none',
					'condition'   => array(
						'element_condition_key!' => array( 'date_compare', 'logged_in_user', 'page_static' ),
					),
				)
			);

				// Ajout des controls pour les conditions
			$this->controller->add_controls_to_compare( $repeater );
			$condition_flat_list = wp_json_encode( $this->controller->get_conditions_flat_list() );

			$element->add_control(
				'element_condition_list',
				array(
					'type'        => Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'default'     => array(
						array(
							'element_condition_key' => 'day_week',
						),
					),
					'title_field' => "<# let labels = $condition_flat_list; "
						. 'let label = labels[element_condition_key]; #>'
						. '{{{ label }}}',
					'button_text' => esc_html__( 'Add condition', 'eac-components' ),
					'render_type' => 'none',
					'condition'   => array(
						'element_condition_active' => 'yes',
					),
				)
			);

			$element->add_control(
				'element_condition_fallback',
				array(
					'label'     => esc_html__( 'Fallback content', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'separator' => 'before',
					'condition' => array(
						'element_condition_active' => 'yes',
					),
				)
			);

			$element->add_control(
				'element_condition_fallback_active',
				array(
					'label'     => esc_html__( 'Enable', 'eac-components' ),
					'description'  => esc_html__( 'Fallback content is displayed if the result of the conditions is true.', 'eac-components' ),
					'type'      => Controls_Manager::CHOOSE,
					'options'   => array(
						'yes' => array(
							'title' => esc_html__( 'Yes', 'eac-components' ),
							'icon'  => 'eicon-check',
						),
						'no'  => array(
							'title' => esc_html__( 'No', 'eac-components' ),
							'icon'  => 'eicon-ban',
						),
					),
					'default'      => 'no',
					'toggle'       => false,
					'render_type'  => 'none',
					'condition'    => array( 'element_condition_active' => 'yes' ),
				)
			);

			$element->add_control(
				'element_condition_fallback_content',
				array(
					'label'       => esc_html__( 'Content', 'eac-components' ),
					'type'        => Controls_Manager::TEXTAREA,
					'default'     => esc_html__( 'This section is reserved for logged-in users.<br/>Please log in to access its contents.', 'eac-components' ),
					'dynamic'     => array( 'active' => true ),
					'label_block' => true,
					'render_type' => 'none',
					'condition'   => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_control(
				'element_condition_fallback_style',
				array(
					'label'     => esc_html__( 'Style', 'eac-components' ),
					'type'      => Controls_Manager::HEADING,
					'condition' => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_control(
				'element_condition_fallback_color',
				array(
					'label'     => esc_html__( 'Color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'global'    => array( 'default' => Global_Colors::COLOR_PRIMARY ),
					'selectors' => array( '.element-condition_fallback-{{ID}}' => 'color: {{VALUE}};' ),
					'condition' => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_group_control(
				Group_Control_Typography::get_type(),
				array(
					'name'      => 'element_condition_fallback_typo',
					'label'     => esc_html__( 'Typography', 'eac-components' ),
					'global'    => array( 'default' => Global_Typography::TYPOGRAPHY_PRIMARY ),
					'selector'  => '.element-condition_fallback-{{ID}}',
					'condition' => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_control(
				'element_condition_fallback_bgcolor',
				array(
					'label'     => esc_html__( 'Background color', 'eac-components' ),
					'type'      => Controls_Manager::COLOR,
					'global'    => array( 'default' => Global_Colors::COLOR_SECONDARY ),
					'selectors' => array( '.element-condition_fallback-{{ID}}' => 'background-color: {{VALUE}};' ),
					'condition' => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_responsive_control(
				'element_condition_fallback_width',
				array(
					'label'          => esc_html__( 'Width', 'eac-components' ),
					'type'           => Controls_Manager::SLIDER,
					'size_units'     => array( 'px', '%' ),
					'default'        => array(
						'size' => 70,
						'unit' => '%',
					),
					'tablet_default' => array(
						'unit' => '%',
					),
					'mobile_default' => array(
						'unit' => '%',
					),
					'range'          => array(
						'px' => array(
							'min'  => 100,
							'max'  => 1400,
							'step' => 50,
						),
						'%'  => array(
							'min'  => 10,
							'max'  => 100,
							'step' => 10,
						),
					),
					'label_block'    => true,
					'separator'      => 'before',
					'selectors'      => array( '.element-condition_fallback-{{ID}}' => 'inline-size: {{SIZE}}{{UNIT}};' ),
					'condition'      => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_responsive_control(
				'element_condition_fallback_height',
				array(
					'label'       => esc_html__( 'Height (px)', 'eac-components' ),
					'type'        => Controls_Manager::SLIDER,
					'size_units'  => array( 'px' ),
					'default'     => array(
						'size' => 200,
						'unit' => 'px',
					),
					'range'       => array(
						'px' => array(
							'min'  => 50,
							'max'  => 1000,
							'step' => 50,
						),
					),
					'label_block' => true,
					'selectors'   => array( '.element-condition_fallback-{{ID}}' => 'min-block-size: {{SIZE}}px;' ),
					'condition'   => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_control(
				'element_condition_fallback_margin',
				array(
					'label'              => esc_html__( 'Margin', 'eac-components' ),
					'type'               => Controls_Manager::DIMENSIONS,
					'allowed_dimensions' => array( 'top', 'bottom' ),
					'default'            => array(
						'top'      => 20,
						'bottom'   => 20,
						'unit'     => 'px',
						'isLinked' => false,
					),
					'range'              => array(
						'px' => array(
							'min'  => 0,
							'max'  => 200,
							'step' => 10,
						),
					),
					'selectors'          => array(
						'.element-condition_fallback-{{ID}}' => 'margin-block: {{TOP}}px {{BOTTOM}}px;',
					),
					'condition'          => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$start = is_rtl() ? 'right' : 'left';
			$end   = is_rtl() ? 'left' : 'right';
			$element->add_responsive_control(
				'element_condition_fallback_alignment',
				array(
					'label'     => esc_html__( 'Text alignment', 'eac-components' ),
					'type'      => Controls_Manager::CHOOSE,
					'options'   => array(
						'left'   => array(
							'title' => is_rtl() ? esc_html__( 'Right', 'eac-components' ) : esc_html__( 'Left', 'eac-components' ),
							'icon'  => "eicon-text-align-{$start}",
						),
						'center' => array(
							'title' => esc_html__( 'Center', 'eac-components' ),
							'icon'  => 'eicon-text-align-center',
						),
						'right'  => array(
							'title' => is_rtl() ? esc_html__( 'Left', 'eac-components' ) : esc_html__( 'Right', 'eac-components' ),
							'icon'  => "eicon-text-align-{$end}",
						),
					),
					'selectors_dictionary' => array(
						'left'   => '0 auto',
						'right'  => 'auto 0',
						'center' => 'auto',
					),
					'default'   => 'center',
					'selectors' => array(
						'.element-condition_fallback-{{ID}} div' => 'margin-inline: {{VALUE}};',
					),
					'condition' => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_group_control(
				Group_Control_Border::get_type(),
				array(
					'name'      => 'element_condition_fallback_border',
					'separator' => 'before',
					'selector'  => '.element-condition_fallback-{{ID}}',
					'condition' => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

			$element->add_control(
				'element_condition_fallback_radius',
				array(
					'label'              => esc_html__( 'Border radius', 'eac-components' ),
					'type'               => Controls_Manager::DIMENSIONS,
					'size_units'         => array( 'px', '%' ),
					'allowed_dimensions' => array( 'top', 'right', 'bottom', 'left' ),
					'selectors'          => array(
						'.element-condition_fallback-{{ID}}' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					),
					'condition'          => array(
						'element_condition_active' => 'yes',
						'element_condition_fallback_active' => 'yes',
					),
				)
			);

		$element->end_controls_section();
	}
}
