<?php
/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://coolplugins.net/cool-formkit-for-elementor-forms/
 * @since      1.0.0
 *
 * @package    Cool_FormKit
 * @subpackage Cool_FormKit/frontend
 */

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

/**
 * The public-facing functionality of the plugin.
 *
 * @since      1.0.0
 * @package    Cool_FormKit
 * @subpackage Cool_FormKit/frontend
 */
if(!class_exists('CFKEF_Frontend')) { 
class CFKEF_Frontend {

    /**
     * The ID of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $plugin_name    The ID of this plugin.
     */
    private $plugin_name;

    /**
     * The version of this plugin.
     *
     * @since    1.0.0
     * @access   private
     * @var      string    $version    The current version of this plugin.
     */
    private $version;

    /**
     * Initialize the class and set its properties.
     *
     * @since    1.0.0
     * @param    string    $plugin_name       The name of this plugin.
     * @param    string    $version    The version of this plugin.
     */
    public function __construct($plugin_name, $version) {
        $this->plugin_name = $plugin_name;
        $this->version = $version;

         // Register custom form fields
        add_action('elementor/editor/after_enqueue_scripts',array($this,'load_helper_assets'));

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

        add_action( 'elementor_pro/forms/actions/register', array($this,'cfef_register_new_form_actions') );
    }

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

        if($this->is_field_enabled('conditional_getresponse')){
            wp_enqueue_script(
                'conditional-getresponse-editor',
                CFKEF_PLUGIN_URL . 'assets/js/conditional-getresponse.js',
                [ 'elementor-editor', 'jquery','elementor-pro' ],
                $this->version,
                true
            ); 
    
            wp_localize_script('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.
     *
     * @since 1.0.0
     * @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 cfef_register_new_form_actions($form_actions_registrar){
        if ($this->is_field_enabled('conditional_redirect')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/conditional-fields-redirection.php';

			$form_actions_registrar->register( new \Cfkef_Conditional_Fields_Redirection_Pro() );
            $form_actions_registrar->register( new \Cfkef_Conditional_Fields_Redirection_Two_Pro() );
			$form_actions_registrar->register( new \Cfkef_Conditional_Fields_Redirection_Three_Pro() );
			$form_actions_registrar->register( new \Cfkef_Conditional_Fields_Redirection_Four_Pro() );
            for ($i = 5; $i <= $redirect_conditionally; $i++) {
				$class_name = sprintf('\\Cfkef_Conditional_Fields_Redirection_%s_Pro', $i);
				$form_actions_registrar->register( new $class_name() );
			}

        }

        if ($this->is_field_enabled('conditional_email')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/conditional-fields-email.php';

            $form_actions_registrar->register( new \Cfkef_Conditional_Email_Action_Pro() );
			$form_actions_registrar->register( new \Cfkef_Conditional_Email_Action_Two_Pro() );
			$form_actions_registrar->register( new \Cfkef_Conditional_Email_Action_Three_Pro() );
			$form_actions_registrar->register( new \Cfkef_Conditional_Email_Action_Four_Pro() );
            for ($i = 5; $i <= $email_conditionally; $i++) {
				$class_name = sprintf('\\Cfkef_Conditional_Email_Action_%s_Pro', $i);
				$form_actions_registrar->register( new $class_name() );
			}
        }

        if($this->is_field_enabled('conditional_mailchimp')){
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/conditional-mailchimp.php';
            $form_actions_registrar->register( new \Conditional_Mailchimp() );
            $form_actions_registrar->register( new \Conditional_Mailchimp_2() );
            $form_actions_registrar->register( new \Conditional_Mailchimp_3() );
            $form_actions_registrar->register( new \Conditional_Mailchimp_4() );
            for ($i = 5; $i <= $mailchimp_conditionally; $i++) {
				$class_name = sprintf('\\Conditional_Mailchimp_%s', $i);
				$form_actions_registrar->register( new $class_name() );
			}
        }

        if($this->is_field_enabled('conditional_getresponse')){
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/conditional-getresponse.php';
            $form_actions_registrar->register( new \Conditional_Getresponse() );
            $form_actions_registrar->register( new \Conditional_Getresponse_2() );
            $form_actions_registrar->register( new \Conditional_Getresponse_3() );
            $form_actions_registrar->register( new \Conditional_Getresponse_4() );
            for ($i = 5; $i <= $getresponse_conditionally; $i++) {
				$class_name = sprintf('\\Conditional_Getresponse_%s', $i);
				$form_actions_registrar->register( new $class_name() );
			}
        }

        if($this->is_field_enabled('conditional_webhook')){
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/conditional-webhook.php';
            $form_actions_registrar->register( new \Conditional_Webhook() );
            $form_actions_registrar->register( new \Conditional_Webhook_2() );
            $form_actions_registrar->register( new \Conditional_Webhook_3() );
            $form_actions_registrar->register( new \Conditional_Webhook_4() );

            for ($i = 5; $i <= $conditionally_webhook; $i++) {
				$class_name = sprintf('\\Conditional_Webhook_%s', $i);
				$form_actions_registrar->register( new $class_name() );
			}
        }

        if($this->is_field_enabled('conditional_whatsapp_redirect')){
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/conditional-whatsapp-redirect.php';
            $form_actions_registrar->register( new \Conditional_Whatsapp_Redirect() );
            $form_actions_registrar->register( new \Conditional_Whatsapp_Redirect_2() );
            $form_actions_registrar->register( new \Conditional_Whatsapp_Redirect_3() );
            $form_actions_registrar->register( new \Conditional_Whatsapp_Redirect_4() );

            for ($i = 5; $i <= $conditionally_whatsapp; $i++) {
				$class_name = sprintf('\\Conditional_Whatsapp_Redirect_%s', $i);
				$form_actions_registrar->register( new $class_name() );
			}
        }


        if ($this->is_field_enabled('conditional_logic')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/create-conditional-fields.php';
            new Cfkef_Create_Conditional_Fields_Pro();
        }

        if ($this->is_field_enabled('conditional_submit_button')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/conditional-fields-submit-button.php';
			new Cfkef_Conditional_Submit_Button_Pro();
        }

        if ($this->is_field_enabled('radio_checkbox_styler')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/checkbox-radio-styler.php';
        }

        if ($this->is_field_enabled('label_styler')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/label-style-for-elementor-form.php';
        }

        if ($this->is_field_enabled('select2')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/select2.php';
        }

        if ($this->is_field_enabled('restrict_date')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/restrict-date.php';
        }
        
        if ($this->is_field_enabled('confirm_dialog')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/confirm-dialog-box.php';
        }
        if ($this->is_field_enabled('form_input_mask')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/class-fme-plugin.php';
            CoolFormkit\Includes\FME_Plugin::instance();
        }
        if($this->is_field_enabled('whatsapp_redirect')){
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/class-whatsapp-redirect.php';
            $form_actions_registrar->register(new Whatsapp_Redirect);
        }

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


    /**
     * Register custom form fields.
     *
     * @since 1.0.0
     * @param \ElementorPro\Modules\Forms\Registrars\Form_Fields_Registrar $form_fields_registrar The form fields registrar.
     * @return void
     */
    public function register_form_fields($form_fields_registrar) {
        if ($this->is_field_enabled('rating_field')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/rating-field.php';
            $form_fields_registrar->register(new \Cfkef_Rating_Field());
        }

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

            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/h-captcha.php';
            $form_fields_registrar->register(new \Cfkef_h_recaptcha());
        }


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

            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/cloudflare_recaptcha.php';
            $form_fields_registrar->register(new \Cfkef_Cloudflare_recaptcha());
        }


        if ($this->is_field_enabled('calculator_field')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/calculator-field.php';
            $form_fields_registrar->register(new \Cfkef_Calculator_Field());
        }

        if ($this->is_field_enabled('range_slider')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/create-range-field.php';
    		$form_fields_registrar->register( new \Cfkef_Create_range_field() );
        }

        if ($this->is_field_enabled('country_code')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/ccfef-country-code-addon.php';
            new Cfkef_COUNTRY_CODE_FIELD();

            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/create-country-field.php';
    		$form_fields_registrar->register( new \Cfkef_Country_Field() );

            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/create-state-field.php';
    		$form_fields_registrar->register( new \Cfkef_State_Field() );
        }

        if ($this->is_field_enabled('signature_field')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/signature-field.php';
    		$form_fields_registrar->register( new \Cfkef_Signature_Field() );
        }

        if ($this->is_field_enabled('image_radio')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/image-radio.php';
    		$form_fields_registrar->register( new \Image_radio() );
        }

        if ($this->is_field_enabled('label_styler')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/label-style-for-elementor-form.php';
    		$form_fields_registrar->register( new \Label_Style_For_Elementor_Form() );
        }

        if ($this->is_field_enabled('WYSIWYG')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/wysiwyg-field.php';
    		$form_fields_registrar->register( new \WYSIWYG_Editor_Field_Register() );
        }

        if ($this->is_field_enabled('currency_field')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/currency-field.php';
            $form_fields_registrar->register( new \Cfkef_Currency_Field() );
        }

        if ($this->is_field_enabled('month_week_field')) {
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/month-week-field.php';
            $form_fields_registrar->register( new \Cfkef_Month_Week_Field() );
        }
        
        require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/class-elementor-date-field-formatter.php';
        Cfkef_date_field_formatter::get_instance();

        if ($this->is_field_enabled('toggle_field')){
            require_once CFKEF_PLUGIN_DIR . 'includes/frontend/widgets/create-toggle-field.php';
            $form_fields_registrar->register( new \Cfkef_Toggle_Field() );
        }
    }
}
}
