<?php
namespace Cool_FormKit_Pro\Widgets\HelloPlusAddons;

use HelloPlus\Includes\Utils;

if ( ! defined( 'ABSPATH' ) ) {
	header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
	exit();
}

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


	private static $instance = null;

	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	public function __construct() { 

		add_action( 'elementor/element/ehp-form/section_form_fields/before_section_end', array( $this, 'add_custom_setting_to_form_repeater_fields' ), 100, 2 );
		add_action( 'elementor/element/ehp-form/section_form_fields/before_section_end', array( $this, 'add_custom_label_switcher_control' ) );
		add_action('wp_enqueue_scripts', array($this,'register_scripts'));
		add_action( 'hello_plus/forms/render/item', array( $this, 'render_attribute' ), 9, 3 );
		add_action( 'elementor/preview/init', array( $this, 'editor_inline_JS' ) );
		// add_action( 'elementor/element/ehp-form/section_form_style/before_section_end', array( $this, 'add_styles_for_label' ), 10, 1 );
		add_action( 'elementor/element/ehp-form/section_field_style/before_section_end', array( $this, 'add_styles_for_icon' ), 10, 1 );
		// add_action( 'elementor/kit/register_tabs', array( $this, 'site_form_fild_settings' ), 10, 1 );
	}

	function add_custom_label_switcher_control( $element ) {
		$element->add_control(
			'label_styles',
			array(
				'label'     => __( 'Styles', 'cool-formkit' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => 'none',
				'options'   => array(
					'none'       => esc_html__( 'Default', 'cool-formkit' ),
					'Style_Four' => esc_html__( 'Style 1', 'cool-formkit' ),
					'filled'     => esc_html__( 'Style 2', 'cool-formkit' ),
					'clean'      => esc_html__( 'Style 3', 'cool-formkit' ),
					'outline'    => esc_html__( 'Style 4', 'cool-formkit' ),
				),
				'condition' => array(
					'show_labels' => 'true',
				),

			)
		);
	}

	public function add_custom_setting_to_form_repeater_fields( $widget ) {
		$elementor = Utils::elementor();
		$control_data = $elementor->controls_manager->get_control_from_stack( $widget->get_unique_name(), 'form_fields' );
		if ( is_wp_error( $control_data ) ) {
			return;
		}
	
		$field_controls = array(
			'field_icon_switcher' => array(
				'name' =>'field_icon_switcher',
                'label' => esc_html__( 'Show Icon', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => esc_html__( 'Show', 'cool-formkit' ),
				'label_off' => esc_html__( 'Hide', 'cool-formkit' ),
				'return_value' => 'yes',
				'default' => 'no',
				'inner_tab' => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'condition'=>[
					'field_type' =>['text', 'email', 'textarea', 'password', 'url', 'number', 'date' ,'time','select','state'],
					'select-field-control!' => 'yes', 
				]
			),

			'field_icon' => array(
				'name' => 'field_icon',
				'label' => esc_html__( 'Icon', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::ICONS,
				'default' => [
					'value' => 'fas fa-user',
					'library' => 'fa-solid',
				],
				'inner_tab' => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'condition' =>[ 
					'field_icon_switcher' => 'yes',
					'field_type' =>['text', 'email', 'textarea', 'password', 'url', 'number', 'date' ,'time','select','state'],
					'select-field-control!' => 'yes', 
				]
				),

				'field_icon_tel_switcher' => array(
					'name' => 'field_icon_tel_switcher',
					'label' => esc_html__( 'Show Icon', 'cool-formkit' ),
					'type' => \Elementor\Controls_Manager::SWITCHER,
					'label_on' => esc_html__( 'Show', 'cool-formkit' ),
					'label_off' => esc_html__( 'Hide', 'cool-formkit' ),
					'return_value' => 'yes',
					'default' => 'no',
					'inner_tab' => 'form_fields_content_tab',
					'tabs_wrapper' => 'form_fields_tabs',
					'condition' => [
						'field_type' => ['ehp-tel'],
						'ccfef-country-code-field!' => 'yes', 
					],
				),

				'field_tel_icon' => array(
					'name' => 'field_tel_icon',
					'label' => esc_html__( 'Icon', 'cool-formkit' ),
					'type' => \Elementor\Controls_Manager::ICONS,
					'default' => [
						'value' => 'fas fa-user',
						'library' => 'fa-solid',
					],
					'inner_tab' => 'form_fields_content_tab',
					'tabs_wrapper' => 'form_fields_tabs',
					'condition' =>[ 
						'field_icon_tel_switcher' => 'yes',
						'ccfef-country-code-field!' => 'yes', 
					]
			),
		);

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

	public function render_attribute( $field, $item, $form ) {
		try {
			$reflection = new \ReflectionClass( $form );
			$property = $reflection->getProperty( 'widget' );
			$property->setAccessible( true );
			$widget = $property->getValue( $form );

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

		$fieldSetting = $widget->get_settings_for_display();
		$repeater_fields = isset($fieldSetting['form_fields']) ? $fieldSetting['form_fields'] : [];
		$allFieldsType = isset( $field['field_type'] ) ? $field['field_type'] : '';
		$iconSwitcher = isset($field['field_icon_switcher']) ? $field['field_icon_switcher'] : 'no';
		$fieldIcon = isset($field['field_icon']) ? $field['field_icon'] : '';
		$labelstyle = isset( $fieldSetting['label_styles'] ) ? $fieldSetting['label_styles'] : '';
		$iconposition = isset($fieldSetting['lsfef_icon_position']) ? $fieldSetting['lsfef_icon_position'] : '';
		//tel specific switcher
		$iconTelSwitcher = isset($field['field_icon_tel_switcher']) ? $field['field_icon_tel_switcher'] : 'no';
		$fieldTelIcon = isset($field['field_tel_icon']) ? $field['field_tel_icon'] : '';
		$icon_clss = '';
		$icon_position_clss = '';
		if($iconSwitcher === "yes" || $iconTelSwitcher === "yes" ){
			$icon_position_clss = 'icon_' . $iconposition .'_side';
			$icon_clss = "icon_clss";
		}
			$customSwitcher = isset( $fieldSetting['show_labels'] ) ? $fieldSetting['show_labels'] : '';

			if((isset($field['field_icon_switcher']) && $field['field_icon_switcher'] === 'yes') || (isset($field['field_icon_tel_switcher']) && $field['field_icon_tel_switcher'] === 'yes') || ($customSwitcher === 'true' && $labelstyle !== 'none')){
				wp_enqueue_style( 'helloplus-lsfef-custom-styles' );		
				wp_enqueue_script( 'helloplus-lsfef-custom-js' );

			}

			$inputField = array( 'text', 'email', 'textarea', 'password', 'url', 'number', 'date' ,'time' ,'ehp-tel','select','country','state');			
			if ( in_array( $allFieldsType, $inputField ) && $customSwitcher === 'true' 
			// && $labelstyle !== 'none'
			 ) {
				$labelStyleClss = 'label-' . $labelstyle . ' lsfef-form-style';
				$widget->add_render_attribute(
					'field-group' . $item,
					array(
						'class'            => $labelStyleClss,
						'data-lsfef-style' => $labelstyle,
					)
				);
			}

		$widget->add_render_attribute(
			'field-group' . $item,
			array(
				'class' => [$icon_clss, $icon_position_clss],
			)
		);
		if ( $iconSwitcher === 'yes' && !empty( $fieldIcon )  && (!isset($_POST['action']) || \Elementor\Plugin::$instance->editor->is_edit_mode()) ) {
			echo "<div class='icon_div'>";
			\Elementor\Icons_Manager::render_icon( $fieldIcon, ['aria-hidden' => 'true'] );
			echo "</div>";
		}
		if ( $iconTelSwitcher === 'yes' && !empty( $fieldTelIcon )  && (!isset($_POST['action']) || \Elementor\Plugin::$instance->editor->is_edit_mode()) ) {
			echo "<div class='icon_div'>";
			\Elementor\Icons_Manager::render_icon( $fieldTelIcon, ['aria-hidden' => 'true'] );
			echo "</div>";
		}

		return $field;
	}	
	public function site_form_fild_settings( $element ) {


		$field_padding_existing_control = $element->get_controls( 'form_field_padding' );
		
		if ( $field_padding_existing_control && isset( $field_padding_existing_control['selectors'] ) ) {
			// Merge existing selectors with custom selectors
			$field_padding_existing_control['selectors'] = array_merge(
				$field_padding_existing_control['selectors'],
				array(
					'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--lsfef-input-padding-top : {{TOP}}{{UNIT}} !important; --lsfef-input-padding-left : {{LEFT}}{{UNIT}} !important ;  --lsfef-input-padding-right : {{RIGHT}}{{UNIT}};  --lsfef-input-padding-bottom: {{BOTTOM}}{{UNIT}} !important;',
				)
			);
			// Update the control with merged selectors
			$element->update_control( 'form_field_padding', $field_padding_existing_control );
		
		}

		$field_sites_border_width = $element->get_controls( 'form_field_border_width' );
		
		
		if ( $field_sites_border_width && isset( $field_sites_border_width['selectors'] ) ) {
			// Merge existing selectors with custom selectors
			$field_sites_border_width['selectors'] = array_merge(
				$field_sites_border_width['selectors'],
				array(
					'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--lsfef-field-border-top : {{TOP}}{{UNIT}} !important; --lsfef-field-border-left:{{LEFT}}{{UNIT}} !important ;  --lsfef-field-border-right : {{RIGHT}}{{UNIT}} !important;  --lsfef-field-border-bottom: {{BOTTOM}}{{UNIT}} !important;',
				)
			);
			// Update the control with merged selectors
			$element->update_control( 'form_field_border_width', $field_sites_border_width );
			$field_sites_border_site_radius = $element->get_controls( 'form_field_border_radius' );
		
		
		if ( $field_sites_border_site_radius && isset( $field_sites_border_site_radius['selectors'] ) ) {
			// Merge existing selectors with custom selectors
			$field_sites_border_site_radius['selectors'] = array_merge(
				$field_sites_border_site_radius['selectors'],
				array(
					'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--lsfef-field-border-top-radius : {{TOP}}{{UNIT}} !important; --lsfef-field-border-left-radius:{{LEFT}}{{UNIT}} !important ;  --lsfef-field-border-right-radius : {{RIGHT}}{{UNIT}} !important;  --lsfef-field-border-bottom-radius: {{BOTTOM}}{{UNIT}} !important;',
				)
			);
			// Update the control with merged selectors
			$element->update_control( 'form_field_border_radius', $field_sites_border_site_radius );
		
		}
		}
// form label typography font size
$form_label_typography_font_size = $element->get_controls( 'form_label_typography_font_size' );
		

		
if ( $form_label_typography_font_size && isset( $form_label_typography_font_size['selectors'] ) ) {
	// Merge existing selectors with custom selectors
	$form_label_typography_font_size['selectors'] = array_merge(
		$form_label_typography_font_size['selectors'],
		array(
			'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--lsfef-site-label-spacing: {{SIZE}}{{UNIT}} !important',
		)
		
	);
	// Update the control with merged selectors
	$element->update_control( 'form_label_typography_font_size', $form_label_typography_font_size );

}


//form  field typography setting 
$form_field_typography_font_size = $element->get_controls( 'form_field_typography_font_size' );
			
if ( $form_field_typography_font_size && isset( $form_field_typography_font_size['selectors'] ) ) {
	// Merge existing selectors with custom selectors
	$form_field_typography_font_size['selectors'] = array_merge(
		$form_field_typography_font_size['selectors'],
		array(
			'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--lsfef-site-field-font-size: {{SIZE}}{{UNIT}} !important',
		)
		
	);
	// Update the control with merged selectors
	$element->update_control( 'form_field_typography_font_size', $form_field_typography_font_size );

}

// form-field-site-background-color
$form_field_background_color = $element->get_controls( 'form_field_background_color' );
			
if ( $form_field_background_color && isset( $form_field_background_color['selectors'] ) ) {
	// Merge existing selectors with custom selectors
	$form_field_background_color['selectors'] = array_merge(
		$form_field_background_color['selectors'],
		array(
			'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--lsfef-site-field-background-color: {{VALUE}} !important',
		)
		
	);
	// Update the control with merged selectors
	$element->update_control( 'form_field_background_color', $form_field_background_color );

}

	}

	public function add_styles_for_label( $element ) {
		
		$column_gap_existing_control = $element->get_controls( 'column_gap' );

		if ( $column_gap_existing_control && isset( $column_gap_existing_control['selectors'] ) ) {
				// Merge existing selectors with custom selectors
				$column_gap_existing_control['selectors'] = array_merge(
					$column_gap_existing_control['selectors'],
					array(
						'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--lsfef-input-offset-left : {{SIZE}}{{UNIT}}  !important',
					)
				);
				// Update the control with merged selectors
				$element->update_control( 'column_gap', $column_gap_existing_control );
		}

		$label_spacing_existing_control = $element->get_controls( 'label_spacing' );
		

		if ( $label_spacing_existing_control && isset( $label_spacing_existing_control['selectors'] ) ) {
				// Merge existing selectors with custom selectors
				$label_spacing_existing_control['selectors'] = array_merge(
					$label_spacing_existing_control['selectors'],
					array(
						'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--lsfef-input-label-spacing : {{SIZE}}{{UNIT}} !important',
					)
				);
				// Update the control with merged selectors
				$element->update_control( 'label_spacing', $label_spacing_existing_control ); 	
		}

		$label_color_existing_control = $element->get_controls( 'label_color' );

		if ( $label_color_existing_control && isset( $label_color_existing_control['selectors'] ) ) {
			// Merge existing selectors with custom selectors
			$label_color_existing_control['selectors'] = array_merge(
				$label_color_existing_control['selectors'],
				array(
					'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--ls-border-color :{{VALUE}};',
				)
			);
			// Update the control with merged selectors
			$element->update_control( 'label_color', $label_color_existing_control );
		}

		$label_font_size = $element->get_controls( 'label_typography_font_size' );

		if ( $label_font_size && isset( $label_font_size['selectors'] ) ) {
						// Merge existing selectors with custom selectors
			$label_font_size['selectors'] = array_merge(
				$label_font_size['selectors'],
				array(
					'{{WRAPPER}} .ehp-form__field-group.lsfef-form-style' => '--ls-label-font-size :{{SIZE}}{{UNIT}};',
				)
			);
			// Update the control with merged selectors
			$element->update_control( 'label_typography_font_size', $label_font_size );
		}

	}

	public function add_styles_for_icon( $element ) {

		

		$element->add_control(
			'lsfef_icon_position',
			array(
				'label' => __('Icon Alignment', 'cool-formkit'), 
                'type' => \Elementor\Controls_Manager::CHOOSE,
				'options' => [
					'left' => [
						'title' => esc_html__( 'Left', 'cool-formkit' ),
						'icon' => 'eicon-text-align-left',
					],
					'right' => [
						'title' => esc_html__( 'Right', 'cool-formkit' ),
						'icon' => 'eicon-text-align-right',
					],
				],
				'default' => 'left',
				'toggle' => true,
			)
			);

		$element->add_control(
			'lsfef_icon_color',
			array(
				'label'     => __( 'Icon Color', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ehp-form__field-group .icon_div > i ,{{WRAPPER}} .ehp-form__field-group .icon_div > svg ' => '--lsfef-icon-color: {{VALUE}}',
				],
			)
		);

		$element->add_control(
			'lsfef_icon_background_color',
			array(
				'label'     => __( 'Icon Background Color', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .ehp-form__field-group .icon_div ' => '--lsfef-icon-background-color: {{VALUE}}',
				],
			)
		);

		$element->add_control(
					'icon_size',
			[
				'label' => esc_html__( 'Icon Size', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'px','custom' ],
				'range' => [
					'px' => [
						'min' => 10,
						'max' => 50,
						'step' => 1,
					],
	
				],
				'default' => [
					'unit' => 'px',
					'size' => 30,
				],
				'selectors' => [
					'{{WRAPPER}} .ehp-form__field-group .icon_div > i, {{WRAPPER}} .ehp-form__field-group .icon_div > svg' => '--lsfef-icon-size: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$element->add_control(
			'box_shadow_for_input_switcher',
			array(
				'label'     => __( 'Input Box Shadow (Hover Effect)', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => esc_html__( 'Show', 'cool-formkit' ),
				'label_off' => esc_html__( 'Hide', 'cool-formkit' ),
				'return_value' => 'yes',
				'default' => 'no',
				
			)
		);

		$element->add_control(
			'box_shadow_for_input',
			array(
				'label'     => __( 'Box Shadow Effect', 'cool-formkit' ),
				'type'      => \Elementor\Controls_Manager::BOX_SHADOW,
				'selectors' => [
					'{{WRAPPER}} .ehp-form__field-group .elementor-field-textual' => 
					'--box-shadow-hr: {{HORIZONTAL}}px; --box-shadow-vr: {{VERTICAL}}px; --box-shadow-blr: {{BLUR}}px; --box-shadow-sprd: {{SPREAD}}px; --box-shadow-color: {{COLOR}};',
				],
				'condition' => array(
					'box_shadow_for_input_switcher' => 'yes'
				)
			)
		);

		$field_typography_existing_control = $element->get_controls( 'field_typography_font_size' );
		if ( $field_typography_existing_control && isset( $field_typography_existing_control['selectors'] ) ) {

			$field_typography_existing_control['selectors'] = array_merge(
				$field_typography_existing_control['selectors'],

				array(
					'{{WRAPPER}} .ehp-form__field-group' => '--typography-font-size: {{SIZE}}{{UNIT}};',
				)
			);
			$element->update_control( 'field_typography_font_size', $field_typography_existing_control );
		}


		$field_border_width = $element->get_controls( 'field_border_width' );

		if ( $field_border_width && isset( $field_border_width['selectors'] ) ) {
			// Add a `render_callback` to avoid warning if a direction key is missing

			$field_border_width['render_callback'] = function( $settings, $control ) {
				$value = isset( $settings[ $control['name'] ] ) ? $settings[ $control['name'] ] : [];

				$defaults = [
					'top' => '0',
					'right' => '0',
					'bottom' => '0',
					'left' => '0',
					'unit' => 'px',
				];

				$value = wp_parse_args( $value, $defaults );
				
				return sprintf(
					'--field-border-left-width: %s%s !important; --field-border-top-width: %s%s !important; --field-border-right-width: %s%s !important; --field-border-bottom-width: %s%s !important;',
					$value['left'], $value['unit'],
					$value['top'], $value['unit'],
					$value['right'], $value['unit'],
					$value['bottom'], $value['unit']
				);
			};

			// Clear default dynamic selector and use the callback instead
			unset( $field_border_width['selectors'] );

			$element->update_control( 'field_border_width', $field_border_width );
		}

		$field_background_color = $element->get_controls( 'field_background_color' );
        if (  $field_background_color && isset( $field_background_color['selectors'] ) ) {
         
			$field_background_color['selectors'] = array_merge(
                $field_background_color['selectors'],
                array(
                    '{{WRAPPER}} .ehp-form__field-group ' => '--field-background-color: {{VALUE}};',
                )
            );
            $element->update_control( 'field_background_color',  $field_background_color );
        }

		$field_border_radius = $element->get_controls( 'field_border_radius' );
        if (  $field_border_radius && isset( $field_border_radius['selectors'] ) ) {
         
			$field_border_radius['selectors'] = array_merge(
                $field_border_radius['selectors'],
                array(
                    '{{WRAPPER}} .ehp-form__field-group ' => '--field-border-left-radius : {{LEFT}}{{UNIT}} !important ; --field-border-top-radius : {{TOP}}{{UNIT}} !important ; --field-border-right-radius:{{RIGHT}}{{UNIT}} !important; --field-border-bottom-radius:{{BOTTOM}}{{UNIT}} !important;',
                )
            );
            $element->update_control( 'field_border_radius',  $field_border_radius);
        }
		
	}

	public function editor_inline_JS() {
		// wp_enqueue_script( 'labelstyle-editor-script', CFKEF_PLUGIN_URL . 'assets/js/label_style_editor.js', array(), CFKEF_PLUGIN_VERSION, true );

		wp_enqueue_style( 'helloplus-lsfef-custom-styles' );	
		wp_enqueue_script( 'helloplus-lsfef-custom-js');
	}

	public function register_scripts() {
		// Detect if we are in the Elementor Preview iframe (safe for elementor-frontend)
		$is_preview = ( isset( $_GET['elementor-preview'] ) || isset( $_GET['preview'] ) );

		$deps = array('jquery');
		if ( $is_preview || ! is_admin() ) {
			$deps[] = 'elementor-frontend'; // ✅ only in preview or frontend
		}
		
		wp_register_script(
			'helloplus-lsfef-custom-js',
			CFKEF_PLUGIN_URL . 'assets/helloplus-addons/js/helloplus-label_style.js',
			$deps,
			CFKEF_PLUGIN_VERSION,
			true
		);

		wp_register_style(
			'helloplus-lsfef-custom-styles',
			// CFKEF_PLUGIN_URL . 'assets/helloplus-addons/css/helloplus-label_style.css',
			CFKEF_PLUGIN_URL . 'assets/helloplus-addons/css/helloplus-label_style.min.css',
			array(),
			CFKEF_PLUGIN_VERSION
		);
	}
}
}
