<?php

namespace Cool_FormKit_Pro\Widgets;


use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Create_Conditional_Fields_Pro;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Submit_Button_Pro;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_COUNTRY_CODE_FIELD;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Country_Field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Create_range_field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_WYSIWYG_Editor_Field_Register;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Rating_Field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Signature_Field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Calculator_Field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Image_Radio;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_FME_Plugin;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_State_Field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_CHECKLIST_RADIO_STYLES_ADD_ON;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Currency_Field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Month_Week_Field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cloudflare_recaptcha;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_h_recaptcha;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_date_field_formatter;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Toggle_Field;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Whatsapp_Redirect;

use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Mailchimp;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Mailchimp_2;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Mailchimp_3;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Mailchimp_4;

// Conditional Getresponse
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Getresponse;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Getresponse_2;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Getresponse_3;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Getresponse_4;

use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Webhook;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Webhook_2;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Webhook_3;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Webhook_4;

use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Whatsapp_Redirect;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Whatsapp_Redirect_2;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Whatsapp_Redirect_3;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Conditional_Whatsapp_Redirect_4;

// Conditional Redirection
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Fields_Redirection_Pro;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Fields_Redirection_Two_Pro;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Fields_Redirection_Three_Pro;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Fields_Redirection_Four_Pro;

// Conditional Email
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Email_Action_Pro;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Email_Action_Two_Pro;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Email_Action_Three_Pro;
use Cool_FormKit_Pro\Widgets\Addons\CoolForm_Cfkef_Conditional_Email_Action_Four_Pro;

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

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

    protected $plugin_name;

    /**
     * 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      CoolForm_Addons_Loader    $instance    The loader instance.
     */
    private static $instance = null;

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

        $this->load_addons();
        add_action('elementor/editor/after_enqueue_scripts',array($this,'load_helper_assets'));

        add_action('cool_formkit/forms/fields/register', array($this, 'register_form_fields'));

        add_action( 'cool_form/forms/actions/register', array($this,'register_new_form_actions') );

    }

    public function load_helper_assets(){
        if($this->is_field_enabled('conditional_mailchimp')){
            wp_enqueue_script(
                'coolform-conditional-mailchimp-editor',
                CFKEF_PLUGIN_URL . 'assets/addons/js/coolform-conditional-mailchimp.js',
                [ 'elementor-editor', 'jquery','Cool_FormKit-forms-editor' ],
                $this->version,
                true
            ); 
    
            wp_localize_script('coolform-conditional-mailchimp-editor', 'CoolMailchimpConfig', [
                'mailchimp_conditionally' => (int) get_option('cfefp_mailchimp_conditionally', 5),
            ]);
        }

        if($this->is_field_enabled('conditional_getresponse')){
            wp_enqueue_script(
                'coolform-conditional-getresponse-editor',
                CFKEF_PLUGIN_URL . 'assets/addons/js/coolform-conditional-getresponse.js',
                [ 'elementor-editor', 'jquery','Cool_FormKit-forms-editor' ],
                $this->version,
                true
            ); 
    
            wp_localize_script('coolform-conditional-getresponse-editor', 'CoolGetresponseConfig', [
                'getresponse_conditionally' => (int) get_option('cfefp_getresponse_conditionally', 5),
            ]);
        }
        
        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 register_form_fields($form_fields_registrar){
        if($this->is_field_enabled('country_code')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-country-code-addon.php';
            new CoolForm_COUNTRY_CODE_FIELD();

            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-country-field.php';
            $form_fields_registrar->register(new CoolForm_Cfkef_Country_Field());

            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-state-field.php';
            $form_fields_registrar->register(new CoolForm_State_Field());
        }
        if($this->is_field_enabled('range_slider')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-range-field.php';
            $form_fields_registrar->register(new CoolForm_Cfkef_Create_range_field());
        }
        if($this->is_field_enabled('WYSIWYG')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-wysiwyg-field.php';
            $form_fields_registrar->register(new CoolForm_WYSIWYG_Editor_Field_Register());
        }
        if($this->is_field_enabled('rating_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-rating-field.php';
            $form_fields_registrar->register(new CoolForm_Rating_Field());
        }
        if($this->is_field_enabled('signature_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-signature-field.php';
            $form_fields_registrar->register(new CoolForm_Signature_Field());
        }
        if($this->is_field_enabled('calculator_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-calculator-field.php';
            $form_fields_registrar->register(new CoolForm_Cfkef_Calculator_Field());
        }
        if($this->is_field_enabled('image_radio')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-image-radio.php';
            $form_fields_registrar->register(new CoolForm_Image_Radio());
        }
        if($this->is_field_enabled('currency_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-currency-field.php';
            $form_fields_registrar->register(new CoolForm_Currency_Field());
        }
        if($this->is_field_enabled('month_week_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-month-week-field.php';
            $form_fields_registrar->register(new CoolForm_Month_Week_Field());
        }
        if($this->is_field_enabled('cloudflare_recaptcha')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-cloudflare_recaptcha.php';
            $form_fields_registrar->register(new CoolForm_Cloudflare_recaptcha());
        }
        if($this->is_field_enabled('h_recaptcha')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-h-captcha.php';
            $form_fields_registrar->register(new CoolForm_h_recaptcha());
        }

        require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-form-date-field-formatter.php';
        new CoolForm_date_field_formatter();

        if ($this->is_field_enabled('toggle_field')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-toggle-field.php';
            $form_fields_registrar->register(new CoolForm_Toggle_Field());
        }
    }

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

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

            $actions_registrar->register( new CoolForm_Cfkef_Conditional_Fields_Redirection_Pro() );
            $actions_registrar->register( new CoolForm_Cfkef_Conditional_Fields_Redirection_Two_Pro() );
			$actions_registrar->register( new CoolForm_Cfkef_Conditional_Fields_Redirection_Three_Pro() );
			$actions_registrar->register( new CoolForm_Cfkef_Conditional_Fields_Redirection_Four_Pro() );

            for ($i = 5; $i <= $redirect_conditionally; $i++) {
			$class = new class($i) extends CoolForm_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;
                    }
                };

			    $actions_registrar->register($class);
		    }

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

            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-conditional-fields-email.php';   
            $actions_registrar->register( new CoolForm_Cfkef_Conditional_Email_Action_Pro() );
            $actions_registrar->register( new CoolForm_Cfkef_Conditional_Email_Action_Two_Pro() );
            $actions_registrar->register( new CoolForm_Cfkef_Conditional_Email_Action_Three_Pro() );
            $actions_registrar->register( new CoolForm_Cfkef_Conditional_Email_Action_Four_Pro() );

            for ($i = 5; $i <= $email_conditionally; $i++) {
			$class = new class($i) extends CoolForm_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($control_id) {
                        return $control_id . '_cfefp_email_action_' . $this->i;
                    }
                };

			    $actions_registrar->register($class);
		    }
        }
        if($this->is_field_enabled('whatsapp_redirect')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-whatsapp-redirect.php';
            $actions_registrar->register(new CoolForm_Whatsapp_Redirect);
        }
        if($this->is_field_enabled('conditional_mailchimp')){
            $mailchimp_conditionally = get_option('cfefp_mailchimp_conditionally', 5);

            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-conditional-mailchimp.php';
            $actions_registrar->register(new CoolForm_Conditional_Mailchimp);
            $actions_registrar->register(new CoolForm_Conditional_Mailchimp_2);
            $actions_registrar->register(new CoolForm_Conditional_Mailchimp_3);
            $actions_registrar->register(new CoolForm_Conditional_Mailchimp_4);

            for ($i = 5; $i <= $mailchimp_conditionally; $i++) {
			$class = new class($i) extends CoolForm_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;
                    }
                };

			    $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/addons/coolform-conditional-getresponse.php';
            $actions_registrar->register(new CoolForm_Conditional_Getresponse);
            $actions_registrar->register(new CoolForm_Conditional_Getresponse_2);
            $actions_registrar->register(new CoolForm_Conditional_Getresponse_3);
            $actions_registrar->register(new CoolForm_Conditional_Getresponse_4);

            for ($i = 5; $i <= $mailchimp_conditionally; $i++) {
			$class = new class($i) extends CoolForm_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;
                    }
                };
            }
        }
        
        if($this->is_field_enabled('conditional_webhook')){
            $conditionally_webhook = get_option('cfefp_webhook_conditionally', 5);
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-conditional-webhook.php';

            $actions_registrar->register(new CoolForm_Conditional_Webhook);
            $actions_registrar->register(new CoolForm_Conditional_Webhook_2);
            $actions_registrar->register(new CoolForm_Conditional_Webhook_3);
            $actions_registrar->register(new CoolForm_Conditional_Webhook_4);

            for ($i = 5; $i <= $conditionally_webhook; $i++) {
			$class = new class($i) extends CoolForm_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;
                    }
                };

			    $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/addons/coolform-conditional-whatsapp-redirect.php';

            $actions_registrar->register(new CoolForm_Conditional_Whatsapp_Redirect);
            $actions_registrar->register(new CoolForm_Conditional_Whatsapp_Redirect_2);
            $actions_registrar->register(new CoolForm_Conditional_Whatsapp_Redirect_3);
            $actions_registrar->register(new CoolForm_Conditional_Whatsapp_Redirect_4);

            for ($i = 5; $i <= $conditionally_whatsapp; $i++) {
			$class = new class($i) extends CoolForm_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;
                    }

                };

			    $actions_registrar->register($class);
		    }
        }   
    }
    
    public function load_addons(){
        if($this->is_field_enabled('conditional_logic')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-conditional-fields.php';
            new CoolForm_Create_Conditional_Fields_Pro();
        }
        if($this->is_field_enabled('conditional_submit_button')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-conditional-fields-submit-button.php';
            new CoolForm_Cfkef_Conditional_Submit_Button_Pro();    
        }
        if($this->is_field_enabled('confirm_dialog')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-confirm-dialog-box.php';
        }
        if($this->is_field_enabled('form_input_mask')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-fme-plugin.php';
            CoolForm_FME_Plugin::instance();
        }
        if($this->is_field_enabled('radio_checkbox_styler')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-checkbox-radio-styler.php';
            CoolForm_CHECKLIST_RADIO_STYLES_ADD_ON::get_instance();
        }
        if($this->is_field_enabled('restrict_date')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-restrict-date.php';
        }
        if($this->is_field_enabled('select2')){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-select2.php';
        }
        // if($this->is_field_enabled('label_styler')){
        // }
        $spam_settings = (array) get_option('cfkef_block_spam_settings', []);
        if(isset($spam_settings['enabled']) && $spam_settings['enabled'] === '1'){
            require_once CFKEF_PLUGIN_DIR . 'widgets/addons/coolform-form-spam-blocker.php';
        }
    }   
    /**
     * 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;
    }

}

}

