<?php
namespace Cool_FormKit_Pro\Widgets\HelloPlusAddons;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
/**
 * Class for managing the select field in Elementor forms.
 *
 * This class adds functionality to include select fields of Elementor forms.
 *
 * @package ccfef
 *
 * @version 1.0.0
 */

if(!class_exists('HelloPlus_SELECT2_ADD_ON')) { 
class HelloPlus_SELECT2_ADD_ON {

	/**
	 * Plugin instance.
	 *
	 * @var HelloPlus_SELECT2_ADD_ON
	 *
	 * @access private
	 * @var null
	 */
	private static $instance = null;

	/**
	 * Get the plugin instance.
	 *
	 * @return SELECT2_ADD_ON
	 * @static
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor method to initialize actions and assets.
	 */
	public function __construct() {
	
		add_action('wp_enqueue_scripts', array($this,'registers_scripts'));
		add_action( 'hello_plus/forms/render/item', array( $this, 'elementor_form_select_field_rendering'), 9, 3 );
		add_action( 'elementor/element/ehp-form/section_form_fields/before_section_end', array( $this, 'update_controls' ), 100, 2 );
		add_action( 'elementor/preview/init', array( $this, 'editor_inline_JS'));
		add_action( 'elementor/editor/before_enqueue_styles', array( $this, 'editor_assets'));
	}

	public function registers_scripts(){
		wp_register_script( 'helloplus-select2-library-script', CFKEF_PLUGIN_URL . 'assets/js/select2.min.js', array(), CFKEF_PLUGIN_VERSION, true );
		wp_register_script( 'helloplus-select2-frontend-script', CFKEF_PLUGIN_URL . 'assets/helloplus-addons/js/helloplus-select2frontend.js', array( 'elementor-frontend', 'jquery', 'select2-library-script' ), CFKEF_PLUGIN_VERSION, true );
		wp_register_style( 'helloplus-select2-library-style', CFKEF_PLUGIN_URL . 'assets/css/select2.min.css', array(), CFKEF_PLUGIN_VERSION, 'all' );
		wp_register_style( 'helloplus-select2-frontend-style', CFKEF_PLUGIN_URL . 'assets/css/select2frontend.css', array(), CFKEF_PLUGIN_VERSION, 'all' );
	}
	/**
	 * Render method to append div element containing all the user data attributes.
	 */
	public function elementor_form_select_field_rendering($field, $item, $form) {
		try {
			$reflection = new \ReflectionClass( $form );
			$property = $reflection->getProperty( 'widget' );
			$property->setAccessible( true );
			$widget = $property->getValue( $form );

		} catch ( \ReflectionException $e ) {
			return $field; 
		}

		$form_settings = $widget->get_settings_for_display();
		$border_color = !empty($form_settings['field_border_color']) ? $form_settings['field_border_color'] : '#646060';
		$border_width = !empty($form_settings['field_border_width']['top']) ? $form_settings['field_border_width']['top'] : '2';
		$border_unit = !empty($form_settings['field_border_width']['unit']) ? $form_settings['field_border_width']['unit'] : 'px';

		$logic_object = array();
		$multiSelect = "";
		$multiSelectLimit = "" ;

		if (isset($field['field_type']) && $field['field_type'] == 'select') {
			if (isset($field['select-field-control']) && $field['select-field-control'] == "yes") {

				wp_enqueue_script( 'helloplus-select2-library-script' );
				wp_enqueue_script( 'helloplus-select2-frontend-script' );
				wp_enqueue_style( 'helloplus-select2-library-style' );
				wp_enqueue_style( 'helloplus-select2-frontend-style' );
				

				$selectFieldControl = $field['select-field-control'];
				if(isset($field['allow_multiple']) && $field['allow_multiple'] == 'true'){
					$multiSelect = $field['allow_multiple'];
					$multiSelectLimit = isset($field['select_size']) ? $field['select_size'] : '';
				}
							
				$placeholder = isset($field['select-field-placeholder']) ? $field['select-field-placeholder'] : '';
				$customId = isset($field['custom_id']) ? $field['custom_id'] : '';
				$uniqueId = isset($field['_id']) ? $field['_id'] : '';
				$fieldType = isset($field['field_type']) ? $field['field_type'] : '';

				// Classes to apply
				$custom_classes = implode(' ', [
					'select_data_js',
					'cfefuniqueId_' . $uniqueId,
					'cfefselectFieldControl_' . $selectFieldControl,
					'cfefmultiSelect_' . $multiSelect,
					'cfefmultiSelectLimit_' . $multiSelectLimit,
					'cfefcustomId_' . $customId,
					'cfeffieldType_' . $fieldType,
					'cfefplaceholder_' . $placeholder,
					'cfefborderColor_' . $border_color,
					'cfefborderWidth_' . $border_width,
					'cfefborderUnit_' . $border_unit,
				]);

				$field['css_classes'] .= ' ' . $custom_classes;

				echo '<template class="' . esc_attr( $custom_classes ) . '"></template>';
			}
		}
		return $field;
	}

	public function editor_inline_JS() {
		wp_enqueue_script( 'helloplus-select2-editor-script', CFKEF_PLUGIN_URL . 'assets/addons/js/coolform-select2editor.js', array(), CFKEF_PLUGIN_VERSION, true );

		wp_enqueue_script( 'helloplus-select2-library-script' );
		wp_enqueue_script( 'helloplus-select2-frontend-script' );
		wp_enqueue_style( 'helloplus-select2-library-style' );
		wp_enqueue_style( 'helloplus-select2-frontend-style' );
	}

    /**
	 * Enqueue editor assets for the plugin.
	 */
	public function editor_assets() {
		wp_enqueue_style( 'helloplus-select2-editor-style', CFKEF_PLUGIN_URL . 'assets/css/select2editor.css', array(), CFKEF_PLUGIN_VERSION, 'all');
	}

	/**
	 * Update form widget controls to include for select field.
	 *
	 * Adds country code control to allow users to customize the select field.
	 *
	 * @since 1.0.0
	 * @access public
	 * @param \Elementor\Widget_Base $widget The form widget instance.
	 * @return void
	 */
	public function update_controls( $widget ) {
		$widget_name = $widget->get_name();
        if($widget_name !== 'ehp-form'){
            return;
        }

		$elementor = \Elementor\Plugin::instance();
		$control_data = $elementor->controls_manager->get_control_from_stack( $widget->get_unique_name(), 'form_fields' );
		if ( is_wp_error( $control_data ) ) {
			return;
		}

		$select_placeholder_desc = sprintf(
			"%s",
			esc_html__( 'Enter the placeholder by adding dash (-) between the each word e.g : Enter-placeholder.', 'cool-formkit'),
	
		);

		$field_controls = array(
			'select-field-control'   => array(
				'name'         => 'select-field-control',
				'label'        => esc_html__( 'SELECT2', 'cool-formkit' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'cool-formkit' ),
				'label_off'    => esc_html__( 'No', 'cool-formkit' ),
				'return_value' => 'yes',
				'default'      => 'no',
				'condition'    => array(
					'field_type' => 'select',
				),
				'tab'          => 'content',
				'inner_tab'    => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
			), 
		);

		$control_data['fields'] = array_merge( $control_data['fields'], $field_controls );
		$widget->update_control( 'form_fields', $control_data );
	}
}

// Instantiate the handler
HelloPlus_SELECT2_ADD_ON::get_instance();

}
