<?php

namespace Cool_FormKit_Pro\Widgets;

// hello plus for collect entries actions
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\Action\HelloPlus_Collect_Entries;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\Action\Mailchimp;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\Action\Getresponse;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\Action\Webhook;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\Action\Save_Form_Data;
use HelloPlus\Modules\Forms\Module;
use Elementor\Core\Common\Modules\Ajax\Module as Ajax;

use Cool_FormKit_Pro\Includes\Utils;
use HelloPlus\Modules\Forms\Registrars\Form_Actions_Registrar;

use Cool_FormKit_Pro\Widgets\HelloPlusAddons\Custom_Ehp_Form;

use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Create_Conditional_Fields_Pro;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Submit_Button_Pro;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_COUNTRY_CODE_FIELD;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Country_Field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_State_Field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Create_range_field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_WYSIWYG_Editor_Field_Register;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Rating_Field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Signature_Field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Calculator_Field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Image_Radio;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Currency_Field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cloudflare_recaptcha;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_h_recaptcha;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Month_Week_Field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_FME_Plugin;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Date;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_date_field_formatter;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Toggle_Field;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Label_Style_For_Elementor_Form;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Whatsapp_Redirect;

use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Mailchimp;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Mailchimp_2;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Mailchimp_3;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Mailchimp_4;

// getresponse conditionally
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Getresponse;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Getresponse_2;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Getresponse_3;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Getresponse_4;

use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Webhook;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Webhook_2;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Webhook_3;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Webhook_4;

use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Whatsapp_Redirect;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Whatsapp_Redirect_2;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Whatsapp_Redirect_3;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Whatsapp_Redirect_4;

// redirect conditionally 
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Fields_Redirection_Pro;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Fields_Redirection_Two_Pro;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Fields_Redirection_Three_Pro;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Fields_Redirection_Four_Pro;

// email conditionally
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Email_Action_Pro;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Email_Action_Two_Pro;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Email_Action_Three_Pro;
use Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Email_Action_Four_Pro;

if (!defined('ABSPATH')) {
    die;
}

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

    protected $plugin_name;

    public $actions_registrar_helloplus;
    /**
     * The current version of the plugin.
     *
     * @since    1.0.0
     * @access   protected
     * @var      string    $version    The current version of the plugin.
     */
    protected $version;

    /**
     * The loader instance.
     *
     * @since    1.0.0
     * @access   private
     * @var      HelloPlus_Addons_Loader    $instance    The loader instance.
     */
    private static $instance = null;

    private function __construct() {
        $this->version = CFKEF_PLUGIN_VERSION;

        if(is_plugin_active( 'hello-plus/hello-plus.php' )){
            add_action('plugins_loaded',function(){
                if ( class_exists( 'HelloPlus\Modules\Forms\Module' ) ) {
                    $forms_module = \HelloPlus\Modules\Forms\Module::instance();
                    if ( $forms_module && isset( $forms_module->actions_registrar ) ) {
                        $this->actions_registrar_helloplus = $forms_module->actions_registrar;
                    }
                }

                $this->load_addons();
    
                $this->load_entries();

                $this->register_action();

                $this->init_hello_plus();
            });

            add_action('elementor/element/ehp-form/section_integration/after_section_end',array($this,'show_actions_on_editor_side') , 10, 2 );
            add_action('elementor/editor/after_enqueue_scripts',array($this,'load_helper_assets'));

            add_action( 'elementor/ajax/register_actions', [ $this, 'register_helloplus_ajax_actions' ] );
            add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'add_editor_js' ) );
        }
    }

    public function add_editor_js() {
		if ( defined( 'ELEMENTOR_PLUGIN_BASE' ) ) {
			wp_enqueue_style(
				'elementor-fontawesome',
				plugin_dir_url( ELEMENTOR_PLUGIN_BASE ) . 'assets/lib/font-awesome/css/fontawesome.min.css',
				array(),
				ELEMENTOR_VERSION
			);
			wp_enqueue_style(
				'elementor-fontawesome-regular',
				plugin_dir_url( ELEMENTOR_PLUGIN_BASE ) . 'assets/lib/font-awesome/css/regular.min.css',
				array(),
				ELEMENTOR_VERSION
			);
			wp_enqueue_style(
				'elementor-fontawesome-solid',
				plugin_dir_url( ELEMENTOR_PLUGIN_BASE ) . 'assets/lib/font-awesome/css/solid.min.css',
				array(),
				ELEMENTOR_VERSION
			);
		}
	}

    public function register_helloplus_ajax_actions(Ajax $ajax){
        $ajax->register_ajax_action( 'ehp_forms_panel_action_data', [ $this, 'helloplus_forms_panel_action_data' ] );
    }

    public function helloplus_forms_panel_action_data( array $data ) {
		$document = Utils::_unstable_get_document_for_edit( $data['editor_post_id'] );

		if ( empty( $data['service'] ) ) {
			throw new \Exception( 'Service required.' );
		}

        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/mailchimp-handler.php';

        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/getresponse-handler.php';

		$integration = $this->actions_registrar_helloplus->get( $data['service'] );

		if ( ! $integration ) {
			throw new \Exception( 'Action not found.' );
		}

		return $integration->handle_panel_request( $data );
	}

    public function load_helper_assets(){
        if($this->is_field_enabled('conditional_mailchimp')){

            wp_enqueue_script(
                'helloplus-conditional-mailchimp-editor',
                CFKEF_PLUGIN_URL . 'assets/helloplus-addons/js/helloplus-conditional-mailchimp.js',
                [ 'elementor-editor', 'jquery','helloplus-forms-editor' ],
                $this->version,
                true
            ); 
        }

        if($this->is_field_enabled('conditional_getresponse')){

            wp_enqueue_script(
                'helloplus-conditional-getresponse-editor',
                CFKEF_PLUGIN_URL . 'assets/helloplus-addons/js/helloplus-conditional-getresponse.js',
                [ 'elementor-editor', 'jquery','helloplus-forms-editor' ],
                $this->version,
                true
            );

            wp_enqueue_style(
			'helloplus-conditional-getresponse-editor-style',
			CFKEF_PLUGIN_URL . 'assets/helloplus-addons/css/helloplus-conditional-getresponse.css',
			[],
			CFKEF_PLUGIN_VERSION,
			'all'
		);
        }

        wp_enqueue_script(
            'HelloPlus-editor-custom-integration',
            CFKEF_ASSETS_URL . 'js/HelloPlus-editor-custom-integration.js',
            [ 'elementor-editor' ],
            $this->version,
            true
        ); 

        wp_enqueue_style(
			'Cool_FormKit-forms-editor',
			CFKEF_STYLE_URL . 'Cool_FormKit-editor.css',
			[],
			$this->version,
			'all'
		);

        wp_enqueue_script( 'wp-i18n' );
        wp_add_inline_script( 'jquery', 'window.__ = window.__ || function( text ){ return text; };', 'before' );
	}
    /**
     * Check if a field is enabled in the settings.
     *
     * @param string $field_key The field key.
     * @return bool True if the field is enabled, false otherwise.
     */
    private function is_field_enabled($field_key) {
        $enabled_elements = get_option('cfkef_enabled_elements', array());
        return in_array(sanitize_key($field_key), array_map('sanitize_key', $enabled_elements));
    }
    
    
    public function load_addons(){
        if($this->is_field_enabled('conditional_logic')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-fields.php';
            new HelloPlus_Create_Conditional_Fields_Pro();
        }
        if($this->is_field_enabled('conditional_submit_button')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-fields-submit-button.php';
            new HelloPlus_Cfkef_Conditional_Submit_Button_Pro();    
        }
        if($this->is_field_enabled('country_code')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-country-code-addon.php';
            new HelloPlus_COUNTRY_CODE_FIELD();    

            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-country-field.php';
            new HelloPlus_Cfkef_Country_Field();

            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-state-field.php';
            new HelloPlus_State_Field();
        }
        if($this->is_field_enabled('range_slider')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-range-field.php';
            new HelloPlus_Cfkef_Create_range_field();
        }
        if($this->is_field_enabled('WYSIWYG')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-wysiwyg-field.php';
            new HelloPlus_WYSIWYG_Editor_Field_Register();
        }
        if($this->is_field_enabled('rating_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-rating-field.php';
            new HelloPlus_Rating_Field();
        }
        if($this->is_field_enabled('signature_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-signature-field.php';
            new HelloPlus_Signature_Field();
        }
        if($this->is_field_enabled('calculator_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-calculator-field.php';
            new HelloPlus_Cfkef_Calculator_Field();
        }
        if($this->is_field_enabled('image_radio')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-image-radio.php';
            new HelloPlus_Image_Radio();
        }
        if($this->is_field_enabled('currency_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-currency-field.php';
            new HelloPlus_Currency_Field();
        }
        if($this->is_field_enabled('cloudflare_recaptcha')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-cloudflare_recaptcha.php';
            new HelloPlus_Cloudflare_recaptcha();
        }
        if($this->is_field_enabled('h_recaptcha')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-h-captcha.php';
            new HelloPlus_h_recaptcha();
        }
        if($this->is_field_enabled('month_week_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-month-week-field.php';
            new HelloPlus_Month_Week_Field();
        }
        if($this->is_field_enabled('confirm_dialog')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-confirm-dialog-box.php';
        }
        if($this->is_field_enabled('form_input_mask')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-fme-plugin.php';
            HelloPlus_FME_Plugin::instance();
        }
        if($this->is_field_enabled('select2')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-select2.php';
        }
        if($this->is_field_enabled('radio_checkbox_styler')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-checkbox-radio-styler.php';
        }
        if($this->is_field_enabled('restrict_date')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-restrict-date.php';
        }
        if($this->is_field_enabled('label_styler')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-label-style-for-elementor-form.php';
            HelloPlus_Label_Style_For_Elementor_Form::get_instance();
        }

        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-radio-checkbox-field.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-date-field.php';
        new HelloPlus_Date();

        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-formlite-date-field-formatter.php';
        new HelloPlus_date_field_formatter();

        if ($this->is_field_enabled('toggle_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-toggle-field.php';
            new HelloPlus_Toggle_Field();
        }

        $spam_settings = (array) get_option('cfkef_block_spam_settings', []);
        if(isset($spam_settings['enabled']) && $spam_settings['enabled'] === '1'){
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-form-lite-spam-blocker.php';
        }
    }   

    public function load_entries(){
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/collect-entries.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/save-form-data.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/mailchimp-handler.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/integration-base.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/mailchimp.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/getresponse.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/getresponse-handler.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/rest-client.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/webhook.php';

        new Save_Form_Data();
        
        if (class_exists('HelloPlus\Modules\Forms\Module')) {
            $forms_module = \HelloPlus\Modules\Forms\Module::instance();

            if ($forms_module && isset($forms_module->actions_registrar)) {
                $forms_module->actions_registrar->register(new HelloPlus_Collect_Entries());
                $forms_module->actions_registrar->register(new Mailchimp());
                $forms_module->actions_registrar->register(new Getresponse());

                $forms_module->actions_registrar->register(new Webhook());
            }
        }
    }   

    public function register_action(){
        if($this->is_field_enabled('conditional_redirect')){
            $redirect_conditionally = get_option('cfefp_redirect_conditionally', '');

            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-fields-redirection.php';

            if (class_exists('HelloPlus\Modules\Forms\Module')) {
                $forms_module = \HelloPlus\Modules\Forms\Module::instance();

                if ($forms_module && isset($forms_module->actions_registrar)) {

                    $forms_module->actions_registrar->register(new HelloPlus_Cfkef_Conditional_Fields_Redirection_Pro());

                    $forms_module->actions_registrar->register(new HelloPlus_Cfkef_Conditional_Fields_Redirection_Two_Pro());

                    $forms_module->actions_registrar->register(new HelloPlus_Cfkef_Conditional_Fields_Redirection_Three_Pro());

                    $forms_module->actions_registrar->register(new HelloPlus_Cfkef_Conditional_Fields_Redirection_Four_Pro());

                    for ($i = 5; $i <= $redirect_conditionally; $i++) {
                        $class = new class($i) extends HelloPlus_Cfkef_Conditional_Fields_Redirection_Pro{
                            protected $i;

                            public function __construct($i) {
                                $this->i = $i;
                            }

                            public function get_name(): string {
                                return 'conditional_fields_redirection_' . $this->i . '_pro';
                            }

                            public function get_label(): string {
                                return esc_html__('Redirect Conditionally ' . $this->i, 'cool-formkit');
                            }

                            public function controler_id($control_id) {
                                return $control_id . '_cfefp_conditional_fields_' . $this->i;
                            }
                        };

                        $forms_module->actions_registrar->register($class);
		            }
                }
            }
        }
        if($this->is_field_enabled('conditional_email')){
            $email_conditionally = get_option('cfefp_email_conditionally', '');

            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-fields-email.php';   

            if (class_exists('HelloPlus\Modules\Forms\Module')){
                $forms_module = \HelloPlus\Modules\Forms\Module::instance();

                if ($forms_module && isset($forms_module->actions_registrar)){

                    $forms_module->actions_registrar->register( new HelloPlus_Cfkef_Conditional_Email_Action_Pro() );
                    $forms_module->actions_registrar->register( new HelloPlus_Cfkef_Conditional_Email_Action_Two_Pro() );
                    $forms_module->actions_registrar->register( new HelloPlus_Cfkef_Conditional_Email_Action_Three_Pro() );
                    $forms_module->actions_registrar->register( new HelloPlus_Cfkef_Conditional_Email_Action_Four_Pro() );
        
                    for ($i = 5; $i <= $email_conditionally; $i++) {
                    $class = new class($i) extends HelloPlus_Cfkef_Conditional_Email_Action_Pro{
                            protected $i;
        
                            public function __construct($i) {
                                $this->i = $i;
                            }
        
                            public function get_name(): string {
                                return 'email_conditional_action_' . $this->i;
                            }
        
                            public function get_label(): string {
                                return esc_html__('Email Conditionally ' . $this->i, 'cool-formkit');
                            }
        
                            public function controler_id($control_id) {
                                return $control_id . '_cfefp_email_action_' . $this->i;
                            }
                        };
        
                        $forms_module->actions_registrar->register($class);

                    }   
                }
            }
        }
        if($this->is_field_enabled('whatsapp_redirect')){
            if (class_exists('HelloPlus\Modules\Forms\Module')){
                $forms_module = \HelloPlus\Modules\Forms\Module::instance();
                if ($forms_module && isset($forms_module->actions_registrar)){

                    require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-whatsapp-redirect.php';
                    $forms_module->actions_registrar->register(new HelloPlus_Whatsapp_Redirect);
                }
            }
        }
        if($this->is_field_enabled('conditional_mailchimp')){
            $mailchimp_conditionally = get_option('cfefp_mailchimp_conditionally', 5);
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-mailchimp.php';

            if (class_exists('HelloPlus\Modules\Forms\Module')){
                $forms_module = \HelloPlus\Modules\Forms\Module::instance();
                if ($forms_module && isset($forms_module->actions_registrar)){
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Mailchimp);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Mailchimp_2);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Mailchimp_3);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Mailchimp_4);

                    for ($i = 5; $i <= $mailchimp_conditionally; $i++) {
                    $class = new class($i) extends HelloPlus_Conditional_Mailchimp{
                            protected $i;
        
                            public function __construct($i) {
                                $this->i = $i;
                            }
        
                            public function get_name(): string {
                                return 'conditional_mailchimp_' . $this->i;
                            }
        
                            public function get_label(): string {
                                return esc_html__('Conditional Mailchimp ' . $this->i, 'cool-formkit');
                            }
        
                            public function controler_id($control_id) {
                                return $control_id . '_cond_mc_' . $this->i;
                            }
                        };
        
                        $forms_module->actions_registrar->register($class);

                    } 
                }
            }
        }

        if($this->is_field_enabled('conditional_getresponse')){

            $mailchimp_conditionally = get_option('cfefp_getresponse_conditionally', 5);
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-getresponse.php';

            if (class_exists('HelloPlus\Modules\Forms\Module')){
                $forms_module = \HelloPlus\Modules\Forms\Module::instance();
                if ($forms_module && isset($forms_module->actions_registrar)){
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Getresponse);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Getresponse_2);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Getresponse_3);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Getresponse_4);

                    for ($i = 5; $i <= $mailchimp_conditionally; $i++) {
                    $class = new class($i) extends HelloPlus_Conditional_Getresponse{
                            protected $i;
        
                            public function __construct($i) {
                                $this->i = $i;
                            }
        
                            public function get_name(): string {
                                return 'conditional_getresponse_' . $this->i;
                            }
        
                            public function get_label(): string {
                                return esc_html__('Conditional Getresponse ' . $this->i, 'cool-formkit');
                            }
        
                            public function controler_id($control_id) {
                                return $control_id . '_cond_rp_' . $this->i;
                            }
                        };
        
                        $forms_module->actions_registrar->register($class);

                    } 
                }
            }


        }

        if($this->is_field_enabled('conditional_whatsapp_redirect')){
            $conditionally_whatsapp = get_option('cfefp_whatsapp_conditionally', 5);
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-whatsapp-redirect.php';

            if (class_exists('HelloPlus\Modules\Forms\Module')){
                $forms_module = \HelloPlus\Modules\Forms\Module::instance();
                if ($forms_module && isset($forms_module->actions_registrar)){
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Whatsapp_Redirect);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Whatsapp_Redirect_2);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Whatsapp_Redirect_3);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Whatsapp_Redirect_4);

                    for ($i = 5; $i <= $conditionally_whatsapp; $i++) {
                    $class = new class($i) extends HelloPlus_Conditional_Whatsapp_Redirect{
                            protected $i;
        
                            public function __construct($i) {
                                $this->i = $i;
                            }
        
                            public function get_name(): string {
                                return 'conditional_whatsapp_' . $this->i;
                            }
        
                            public function get_label(): string {
                                return esc_html__('Conditional Whatsapp ' . $this->i, 'cool-formkit');
                            }
        
                            public function controler_id($control_id) {
                                return $control_id . '_conditional_whatsapp_' . $this->i;
                            }
                        };
        
                        $forms_module->actions_registrar->register($class);

                    } 
                }
            }
        }
        if($this->is_field_enabled('conditional_webhook')){
            $webhook_conditionally = get_option('cfefp_webhook_conditionally', 5);
            require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-webhook.php';

            if (class_exists('HelloPlus\Modules\Forms\Module')){
                $forms_module = \HelloPlus\Modules\Forms\Module::instance();
                if ($forms_module && isset($forms_module->actions_registrar)){
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Webhook);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Webhook_2);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Webhook_3);
                    $forms_module->actions_registrar->register(new HelloPlus_Conditional_Webhook_4);

                    for ($i = 5; $i <= $webhook_conditionally; $i++) {
                    $class = new class($i) extends HelloPlus_Conditional_Webhook{
                            protected $i;
        
                            public function __construct($i) {
                                $this->i = $i;
                            }
        
                            public function get_name(): string {
                                return 'conditional_webhook_' . $this->i;
                            }
        
                            public function get_label(): string {
                                return esc_html__('Conditional Webhook ' . $this->i, 'cool-formkit');
                            }
        
                            public function controler_id($control_id) {
                                return $control_id . '_conditional_webhook_' . $this->i;
                            }
                        };
        
                        $forms_module->actions_registrar->register($class);

                    } 
                }
            }
        }
    }

    public function show_actions_on_editor_side( $element, $args ) {
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-fields-redirection.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-fields-email.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-whatsapp-redirect.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/mailchimp.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-mailchimp.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/getresponse.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-getresponse.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/rest-client.php';

        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/action/webhook.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-webhook.php';
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-conditional-whatsapp-redirect.php';

        $custom_actions   = [];
        $action_instances = [];

        // === 1. REDIRECTION Actions ===
        if($this->is_field_enabled('conditional_redirect')){

            $redirect_count = (int) get_option( 'cfefp_redirect_conditionally', 4 );

            for ( $i = 1; $i <= $redirect_count; $i++ ) {
                $instance = new class( $i ) extends \Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Fields_Redirection_Pro {
                    protected $i;
                    public function __construct( $i ) { $this->i = $i; }
                    public function get_name(): string {
                        return 'conditional_fields_redirection_' . $this->i . '_pro';
                    }
                    public function get_label(): string {
                        return esc_html__( 'Redirect Conditionally ' . $this->i, 'cool-formkit' );
                    }
                    public function controler_id( $id ) {
                        return $id . '_cfefp_conditional_fields_' . $this->i;
                    }
                };

                $custom_actions[ $instance->get_name() ] = $instance->get_label();
                $action_instances[] = $instance;
            }
        }

        // === 2. EMAIL Actions ===
        if($this->is_field_enabled('conditional_email')){
            $email_count = (int) get_option( 'cfefp_email_conditionally', 4 );

            for ( $i = 1; $i <= $email_count; $i++ ) {
                $instance = new class( $i ) extends \Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Cfkef_Conditional_Email_Action_Pro {
                    protected $i;
                    public function __construct( $i ) { $this->i = $i; }
                    public function get_name(): string {
                        return 'email_conditional_action_' . $this->i;
                    }
                    public function get_label(): string {
                        return esc_html__( 'Email Conditionally ' . $this->i, 'cool-formkit' );
                    }
                    public function controler_id_email( $id ) {
                        return $id . '_cfefp_email_action_' . $this->i;
                    }
                };

                $custom_actions[ $instance->get_name() ] = $instance->get_label();
                $action_instances[] = $instance;
            }
        }
        

        // === 3. Mailchimp Actions ===
        if($this->is_field_enabled('conditional_mailchimp')){

            $mailchimp_conditionally = (int) get_option('cfefp_mailchimp_conditionally', 5);
            for ( $i = 1; $i <= $mailchimp_conditionally; $i++ ) {
                $instance = new class( $i ) extends \Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Mailchimp {
                    protected $i;
                    public function __construct( $i ) { $this->i = $i; }
                    public function get_name(): string {
                        return 'conditional_mailchimp_' . $this->i;
                    }
                    public function get_label(): string {
                        return esc_html__( 'Conditional Mailchimp ' . $this->i, 'cool-formkit' );
                    }
                    public function controler_id( $id ) {
                        return $id . '_cond_mc_' . $this->i;
                    }
                };

                $custom_actions[ $instance->get_name() ] = $instance->get_label();
                $action_instances[] = $instance;
            }

        }

        // === 4. Getresponse Actions ===
        if($this->is_field_enabled('conditional_getresponse')){

            $getresponse_conditionally = (int) get_option('cfefp_getresponse_conditionally', 5);
            for ( $i = 1; $i <= $getresponse_conditionally; $i++ ) {
                $instance = new class( $i ) extends \Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Getresponse {
                    protected $i;
                    public function __construct( $i ) { $this->i = $i; }
                    public function get_name(): string {
                        return 'conditional_getresponse_' . $this->i;
                    }
                    public function get_label(): string {
                        return esc_html__( 'Conditional GetResponse ' . $this->i, 'cool-formkit' );
                    }
                    public function controler_id( $id ) {
                        return $id . '_cond_rp_' . $this->i;
                    }
                };

                $custom_actions[ $instance->get_name() ] = $instance->get_label();
                $action_instances[] = $instance;
            }

        }

        if($this->is_field_enabled('whatsapp_redirect')){

            $instance = new HelloPlus_Whatsapp_Redirect();
            $custom_actions[ $instance->get_name() ] = $instance->get_label();
            $action_instances[] = $instance;
        }


        if($this->is_field_enabled('conditional_mailchimp')){

            $instance = new Mailchimp();
            $custom_actions[ $instance->get_name() ] = $instance->get_label();
            $action_instances[] = $instance;
        }

        if($this->is_field_enabled('conditional_webhook')){
            // === 3. Webhook Actions ===
            $webhook_conditionally = (int) get_option('cfefp_webhook_conditionally', 5);
            for ( $i = 1; $i <= $webhook_conditionally; $i++ ) {
                $instance = new class( $i ) extends \Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Webhook {
                    protected $i;
                    public function __construct( $i ) { $this->i = $i; }
                    public function get_name(): string {
                        return 'conditional_webhook_' . $this->i;
                    }
                    public function get_label(): string {
                        return esc_html__( 'Conditional Webhook ' . $this->i, 'cool-formkit' );
                    }
                    public function controler_id( $id ) {
                        return $id . '_conditional_webhook_' . $this->i;
                    }
                };

                $custom_actions[ $instance->get_name() ] = $instance->get_label();
                $action_instances[] = $instance;
            }

        }

        if($this->is_field_enabled('conditional_whatsapp_redirect')){

            // === 3. Conditional Whatsapp Redirct Actions ===
            $conditionally_whatsapp = (int) get_option('cfefp_whatsapp_conditionally', 5);
            for ( $i = 1; $i <= $conditionally_whatsapp; $i++ ) {
                $instance = new class( $i ) extends \Cool_FormKit_Pro\Widgets\HelloPlusAddons\HelloPlus_Conditional_Whatsapp_Redirect {
                    protected $i;
                    public function __construct( $i ) { $this->i = $i; }
                    public function get_name(): string {
                        return 'conditional_whatsapp_' . $this->i;
                    }
                    public function get_label(): string {
                        return esc_html__( 'Conditional Whatsapp ' . $this->i, 'cool-formkit' );
                    }
                    public function controler_id( $id ) {
                        return $id . '_conditional_whatsapp_' . $this->i;
                    }
                };

                $custom_actions[ $instance->get_name() ] = $instance->get_label();
                $action_instances[] = $instance;
            }

        }

        $instance = new HelloPlus_Whatsapp_Redirect();
        $custom_actions[ $instance->get_name() ] = $instance->get_label();
        $action_instances[] = $instance;

        $instance = new Getresponse();
        $custom_actions[ $instance->get_name() ] = $instance->get_label();
        $action_instances[] = $instance;

        $instance = new Webhook();
        $custom_actions[ $instance->get_name() ] = $instance->get_label();
        $action_instances[] = $instance;

        // === 3. Add Dropdown in Editor
        $element->start_controls_section(
            'cool_formkit_conditional_actions_section',
            [
                'label' => esc_html__( 'Cool Actions After Submit', 'cool-formkit' ),
            ]
        );

        $element->add_control( 'cool_formkit_submit_actions', [
            'label'       => __( 'Actions After Submit', 'cool-formkit' ),
            'type'        => \Elementor\Controls_Manager::SELECT2,
            'multiple'    => true,
            'label_block' => true,
            'options'     => $custom_actions,
            'default'     => [ 'conditional_fields_redirection_1_pro' ],
            'render_type' => 'template',
        ] );

        $element->end_controls_section();

        // === 4. Register All Controls with Condition
        foreach ( $action_instances as $instance ) {
            if ( method_exists( $instance, 'register_settings_section' ) ) {
                // Inside each register_settings_section(), use:
                // 'condition' => [ 'cool_formkit_submit_actions' => $this->get_name() ]
                $instance->register_settings_section( $element );
            }
        }
    }

    public function init_hello_plus(){
        add_action('elementor/init', array($this, 'load_hello_plus_dependencies'), 0);
        add_action('elementor/widgets/register', array($this, 'register_custom_widgets'), 20);
    }

    public function load_hello_plus_dependencies() {
        require_once CFKEF_PLUGIN_DIR . 'widgets/helloplus-addons/helloplus-custom-ehp-form.php';
	}

    public function register_custom_widgets($widgets_manager) {
		try {
			$widgets_manager->unregister_widget_type('ehp-form');
		} catch (\Exception $e) {
			// silently fail if not found
		}
		try {
			$widgets_manager->register_widget_type(new Custom_Ehp_Form());
		} catch (\Exception $e) {
			// silently fail on error
		}
	}
    /**
     * Get the instance of this class.
     *
     * @since    1.0.0
     * @return   CFKEF_Loader    The instance of this class.
     */
    public static function get_instance() {
        if (null == self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Retrieve the version number of the plugin.
     *
     * @since    1.0.0
     * @return   string    The version number of the plugin.
     */
    public function get_version() {
        return $this->version;
    }

}

    }