<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
/**
 * Class for managing the radio/checkbox field in Elementor forms.
 *
 * This class adds functionality to include radio/checkbox fields of Elementor forms.
 *
 * @package Cool_FormKit
 *
 * @version 1.1.7
 */

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

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

	/**
	 * Get the plugin instance.
	 *
	 * @return CHECKLIST_RADIO_STYLES_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, 'register_scripts'));
		add_action( 'elementor_pro/forms/render/item', array( $this, 'elementor_form_radio_field_rendering'), 9, 3 );
		add_action( 'elementor/element/form/section_form_fields/before_section_end', array( $this, 'update_controls'), 100, 2 );
		add_action( 'elementor/preview/init', array( $this, 'editor_inline_JS'));
	}

	public function elementor_form_radio_field_rendering($field, $item, $form) {
		$logic_object = array();

		if (isset($field['field_type']) && in_array($field['field_type'], array('radio', 'checkbox'))) {
			if (isset($field['cr-style']) && $field['cr-style'] !== "cfkef-default") {
				wp_enqueue_script( 'checkbox-radio-frontend-script');
				wp_enqueue_style( 'checkbox-radio-frontend-style' );	

				$size = isset($field['cr-style-size']['size']) ? $field['cr-style-size']['size'] : 12;
				$style = isset($field['cr-style']) ? $this->get_style_class($field['cr-style']) : '';
				$styleColor = isset($field['cr-style-color']) ? esc_attr($field['cr-style-color']) : '';
				$styleDefaultColor = isset($field['cr-style-default-color']) ? esc_attr($field['cr-style-default-color']) : '';
				$customId = isset($field['custom_id']) ? $field['custom_id'] : '';
				$uniqueId = isset($field['_id']) ? $field['_id'] : '';
				$fieldType = isset($field['field_type']) ? $field['field_type'] : '';
	
				$field['css_classes'] .= ' editor_data_js';
				$field['css_classes'] .= ' cfefuniqueId_' . esc_attr($uniqueId);
				$field['css_classes'] .= ' cfefstyle_' . esc_attr($style);
				$field['css_classes'] .= ' cfefsize_' . esc_attr($size);
				$field['css_classes'] .= ' cfefstyleDefaultColor_' . esc_attr($styleDefaultColor);
				$field['css_classes'] .= ' cfefstyleColor_' . esc_attr($styleColor);
				$field['css_classes'] .= ' cfefcustomId_' . esc_attr($customId);
				$field['css_classes'] .= ' cfeffieldType_' . esc_attr($fieldType);
			}
		}
	
		return $field;
	}
	/**
     * Get the rating icon HTML.
     *
     * @since 1.0.0
     * @param string $icon
     * @param array $custom_icon
     * @return string
     */
    private function get_style_class($style) {
        switch ($style) {
			case 'cfkef-default':
				return 'cfkef-default';
            case 'cfkef-swtch-Input':
                return 'cfkef-swtch-Input';
            case 'cfkef-cirChk-Input':
                return 'cfkef-cirChk-Input';
            case 'cfkef-cov-Input':
                return 'cfkef-cov-Input';
            case 'cfkef-sqChk-Input':
                return 'cfkef-sqChk-Input';
            case 'cfkef-chcklist-Input':
                return 'cfkef-chcklist-Input';
			case 'cfkef-sq-Input':
				return 'cfkef-sq-Input';
			case 'cfkef-ci-Input':
				return 'cfkef-ci-Input';
            default:
                return 'cfkef-default';
        }   
    }

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

		wp_enqueue_script( 'checkbox-radio-frontend-script');
		wp_enqueue_style( 'checkbox-radio-frontend-style' );
	}

	public function register_scripts() {
		// wp_register_script( 'checkbox-radio-frontend-script', CFKEF_PLUGIN_URL . 'assets/js/checkbox-radio-frontend.min.js', array(), CFKEF_PLUGIN_VERSION, true );
		wp_register_script( 'checkbox-radio-frontend-script', CFKEF_PLUGIN_URL . 'assets/js/checkbox-radio-frontend.js', array('elementor-frontend'), CFKEF_PLUGIN_VERSION, true );
		wp_register_style( 'checkbox-radio-frontend-style', CFKEF_PLUGIN_URL . 'assets/css/checkbox-radio-frontend.min.css', array(), CFKEF_PLUGIN_VERSION, 'all' );		
	}


	/**
	 * Update form widget controls to include for radio/checkbox field.
	 *
	 * Adds country code control to allow users to customize the radio/checkbox field.
	 *
	 * @since 1.0.0
	 * @access public
	 * @param \Elementor\Widget_Base $widget The form widget instance.
	 * @return void
	 */
	public function update_controls( $widget ) {
		$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;
		}

		$field_controls = array(
			'cr-style' => array(
				'name'         => 'cr-style',
				'label'        => esc_html__( 'Style', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::SELECT,
                'options' => array(
					'cfkef-default' => __('Default', 'cool-formkit'),
					'cfkef-sq-Input' => __('Square', 'cool-formkit'),
					'cfkef-ci-Input' => __('Circle', 'cool-formkit'),
					'cfkef-sqChk-Input' => __('Square Check', 'cool-formkit'),
					'cfkef-cirChk-Input' => __('Circle Check', 'cool-formkit'),
                    'cfkef-swtch-Input' => __('Switch', 'cool-formkit'), 
					'cfkef-chcklist-Input' => __('Checklist', 'cool-formkit'),                   
                    'cfkef-cov-Input' => __('Cover', 'cool-formkit'),			
				),
				'default'      => 'cfkef-default',
				'condition'    => array(
					'field_type' => array('radio', 'checkbox'),					
				),
				'tab'          => 'content',
				'inner_tab'    => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'ai'           => array(
					'active' => false,
				),
			),    

			'cr-style-size' => array(
				'name'         => 'cr-style-size',
				'label'        => esc_html__( 'Size', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => ['px'],
				'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 20,
                        'step' => 1,
                    ],
                ],
				'default' => [
                    'unit' => 'px',
                    'size' => 0,
                ],
				'condition'    => array(
					'field_type' => array('radio', 'checkbox'),
				    'cr-style!' => 'cfkef-default'						
				),
				'tab'          => 'content',
				'inner_tab'    => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'ai'           => array(
					'active' => false,
				),
				'selectors'    => array(
					'{{WRAPPER}} :where(.elementor-field-type-radio,.elementor-field-type-checkbox).elementor-field-group{{CURRENT_ITEM}}' => '--radio-size: {{SIZE}}px',
				),
			),

			'cr-style-default-color' => array(
				'name'         => 'cr-style-default-color',
				'label'        => esc_html__( 'Unselected Color', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'default' => '#E0E0E0',
				'condition'    => array(
					'field_type' => array('radio', 'checkbox'),
					'cr-style!' => 'cfkef-default'			
				),
				'tab'          => 'content',
				'inner_tab'    => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'ai'           => array(
					'active' => false,
				),
				'selectors'    => array(
					'{{WRAPPER}} :where(.elementor-field-type-radio,.elementor-field-type-checkbox).elementor-field-group{{CURRENT_ITEM}}' => '--radio-default-color: {{VALUE}}',
				),
			),

			'cr-style-color' => array(
				'name'         => 'cr-style-color',
				'label'        => esc_html__( 'Selected Color', 'cool-formkit' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'default' => '#E0E0E0',
				'condition'    => array(
					'field_type' => array('radio', 'checkbox'),
					'cr-style!' => 'cfkef-default'							
				),
				'tab'          => 'content',
				'inner_tab'    => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'ai'           => array(
					'active' => false,
				),
				'selectors'    => array(
					'{{WRAPPER}} :where(.elementor-field-type-radio,.elementor-field-type-checkbox).elementor-field-group{{CURRENT_ITEM}}' => '--radio-color: {{VALUE}}',
				),
			),
		);

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