<?php

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

abstract class Class_instant_popup_builder_manager
{

    static $popups = array();
    static $is_active = array();

    public $popup_type;
    public $popup_name;

    // Icon mapping for installed popup types
    protected static $popup_icons = [
        'text' => 'fa-file-lines',
        'image' => 'fa-image',
        'html' => 'fa-code',
        'subscription' => 'fa-envelope',
    ];

    public function __construct($popup_type, $popup_name)
    {

        $this->popup_type = $popup_type;
        $this->popup_name = $popup_name;
    }

    public static function add_popup($popup)
    {
        self::$popups[] = $popup;
    }

    public static function get_popups()
    {
        return self::$popups;
    }

    // Add active popup types
    public static function add_active_popup($is_active)
    {
        self::$is_active[] = $is_active;
    }

    // Get active popup type
    public static function get_active_popup()
    {
        return self::$is_active;
    }

    /**
     * Create popup and render html
     * @return string
     */

    public static function create_popup(string $type)
    {

        $method = 'render_create_popup_' . $type;
        foreach (self::get_popups() as $popup) {
            if (method_exists($popup, $method)) {
                return $popup->$method(); // call dynamically
            }
        }
        return false;
        // return  $method();
    }

    /**
     * Create popup and render html
     * @return string
     */

    public static function edit_popup(string $type)
    {

        $method = 'render_edit_popup_' . $type;
        foreach (self::get_popups() as $popup) {
            if (method_exists($popup, $method)) {
                return $popup->$method(); // call dynamically
            }
        }
        return false;
        // return  $method();
    }

    /**
     * Modal function to render differnt modal type
     *
     * @return string
     * @param string
     *
     */
    public static function trigger_modal($type, $id)
    {

        $method = 'trigger_popup_modal_' . $type;
        foreach (self::get_popups() as $popup) {
            if (method_exists($popup, $method)) {
                return $popup->$method($id); // call dynamically
            }
        }
        return false;
    }
    abstract function render_html();
}

class TextPopup extends Class_instant_popup_builder_manager
{

    public function __construct($popup_type, $popup_name)
    {
        parent::__construct($popup_type, $popup_name);
        self::add_popup($this);
        self::add_active_popup('text');
    }

    public function render_html()
    {
        // Concatenate the HTML with PHP variables properly escaped
        // return '<div class="input_field">
        //     <input type="radio" name="popup_type" required id="' . esc_attr($this->popup_type) . '" value="' . esc_attr($this->popup_name) . '">
        //     <label for="' . esc_attr($this->popup_type) . '">' . esc_html($this->popup_name) . '</label>
        // </div>';

        $icon = isset(self::$popup_icons[$this->popup_name]) ? self::$popup_icons[$this->popup_name] : 'fa-code';
        return '<div class="ipb_grid ipb_install" data-type="' . $this->popup_name . '">
            <div class="thumb">
                <i class="fa-solid ' . esc_attr($icon) . '"></i>
            </div>
            <div class="ipd_content">
                <h3>Text Popup</h3>
                <p>Allows Text Popup Creation</p>
            </div>
        </div>';
    }

    /**
     * Create text popup
     * @return string
     */
    public function render_create_popup_text()
    {

        return  include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-text.php';
    }

    /**
     * Edit popup screen
     * @return string
     */
    public function render_edit_popup_text()
    {

        return  include plugin_dir_path(dirname(__FILE__)) . '/partials/edit-template/edit-popup-text.php';
    }

    /**
     * Render Text modal
     * @return string
     *
     */
    public function trigger_popup_modal_text($id)
    {

        $popup_id = $id;

        return include_once plugin_dir_path(dirname(__FILE__)) . '/popup_modal/modal-text.php';
    }
}

new TextPopup('popup_type_text', 'text');

class ImagePopup extends Class_instant_popup_builder_manager
{

    public function __construct($popup_type, $popup_name)
    {
        parent::__construct($popup_type, $popup_name);
        self::add_popup($this);
        self::add_active_popup('image');
    }
    public function render_html()
    {
        // return "<div class='input_field'>
        //                 <input type='radio' name='popup_type' required id='{$this->popup_type}' value='{$this->popup_name}'>
        //                 <label for='{$this->popup_type}'>{$this->popup_name}</label>
        //             </div>";

        $icon = isset(self::$popup_icons[$this->popup_name]) ? self::$popup_icons[$this->popup_name] : 'fa-code';
        return '<div class="ipb_grid ipb_install" data-type="' . $this->popup_name . '">
                    <div class="thumb">
                        <i class="fa-solid ' . esc_attr($icon) . '"></i>
                    </div>
                    <div class="ipd_content">
                        <h3>Image Popup</h3>
                        <p>Create simple image Popup</p>
                    </div>
                </div>';
    }

    /**
     * Create Image popup
     *
     */
    public function render_create_popup_image()
    {

        return  include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-image.php';
    }

    /**
     * Edit popup screen
     * @return string
     */
    public function render_edit_popup_image()
    {

        return  include plugin_dir_path(dirname(__FILE__)) . '/partials/edit-template/edit-popup-image.php';
    }

    /**
     * Render Image modal
     * @return string
     *
     */
    public function trigger_popup_modal_image($id)
    {

        $popup_id = $id;

        return include_once plugin_dir_path(dirname(__FILE__)) . '/popup_modal/modal-image.php';
    }
}

new ImagePopup('popup_type_image', 'image');

// Html popup

class HtmlPopup extends Class_instant_popup_builder_manager
{

    public function __construct($popup_type, $popup_name)
    {
        parent::__construct($popup_type, $popup_name);
        self::add_popup($this);
        self::add_active_popup('html');
    }
    public function render_html()
    {
        // return "<div class='input_field'>
        //                 <input type='radio' name='popup_type' required id='{$this->popup_type}' value='{$this->popup_name}'>
        //                 <label for='{$this->popup_type}'>{$this->popup_name}</label>
        //             </div>";

        $icon = isset(self::$popup_icons[$this->popup_name]) ? self::$popup_icons[$this->popup_name] : 'fa-code';
        return '<div class="ipb_grid ipb_install" data-type="' . $this->popup_name . '">
                    <div class="thumb">
                        <i class="fa-solid ' . esc_attr($icon) . '"></i>
                    </div>
                    <div class="ipd_content">
                        <h3>HTML Popup</h3>
                        <p>Create Custom HTML / CSS Popups</p>
                    </div>
                </div>';
    }

    /**
     * Create Image popup
     *
     */
    public function render_create_popup_html()
    {

        return  include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-html.php';
    }

    /**
     * Edit popup screen
     * @return string
     */
    public function render_edit_popup_html()
    {

        return  include plugin_dir_path(dirname(__FILE__)) . '/partials/edit-template/edit-popup-html.php';
    }

    /**
     * Render Image modal
     * @return string
     *
     */
    public function trigger_popup_modal_html($id)
    {

        $popup_id = $id;

        return include_once plugin_dir_path(dirname(__FILE__)) . '/popup_modal/modal-html.php';
    }
}

new HtmlPopup('popup_type_html', 'html');
$instant_popup_builder_active_license = get_option('subscription_license_active');
$instant_popup_builder_ipd_active_plugin = get_option('instant_popup_subscription_activated');
if($instant_popup_builder_active_license != 'yes' || $instant_popup_builder_ipd_active_plugin == 'no'){

class SubscriptionPopup extends Class_instant_popup_builder_manager
{

    public function __construct($popup_type, $popup_name)
    {
        parent::__construct($popup_type, $popup_name);
        self::add_popup($this);
        self::add_active_popup('subscription');
        add_action('wp_ajax_ipb_load_subscription_popups_templates', [$this, 'ipb_load_subscription_popups_templates']);
    }
    public function render_html()
    {
        // return "<div class='input_field'>
        //                 <input type='radio' name='popup_type' required id='{$this->popup_type}' value='{$this->popup_name}'>
        //                 <label for='{$this->popup_type}'>{$this->popup_name}</label>
        //             </div>";
        $active_license = get_option('subscription_license_active');
        $ipd_active_plugin = get_option('instant_popup_subscription_activated');
        if($active_license != 'yes' || $ipd_active_plugin == 'no'){
        $icon = isset(self::$popup_icons[$this->popup_name]) ? self::$popup_icons[$this->popup_name] : 'fa-code';
        return '<div class="popup_subscription ipb_grid ipb_install" data-type="' . $this->popup_name . '" data-modal="subscription-template-selection">
                    <div class="thumb">
                        <i class="fa-solid ' . esc_attr($icon) . '"></i>
                    </div>
                    <div class="ipd_content">
                        <h3>Subscription Popup</h3>
                        <p>Capture Users emails</p>
                    </div>
                </div>';
        }
    }
    public function ipb_load_subscription_popups_templates()
		{
			// Verify nonce
			if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'display_ajax_action')) {
				wp_send_json_error('Security check failed');
				return;
			}

			// Load the template selection content
			$content = $this->render_create_popup_subscription_model();

			if ($content) {
				wp_send_json_success($content);
			} else {
				wp_send_json_error('Failed to load template content');
			}
		}
    /**
     * Create Image popup
     *
     */

    public function render_create_popup_subscription_model()
    {
         ob_start();
        include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-selection.php';
        return ob_get_clean();
    }

    public function render_create_popup_subscription()
    {
    // Check if a template is selected
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for template selection/display purposes only, value is sanitized
        if (isset($_GET['template']) && !empty($_GET['template'])) {
            // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for template selection/display purposes only, value is sanitized
            $template = sanitize_text_field(wp_unslash($_GET['template']));

            // Load the specific template based on selection
            switch ($template) {
                case 'basic':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-basic.php';
                case 'colorful':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-colorful.php';
                case 'classic':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-classic.php';
                case 'creative':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-creative.php';
                case 'glassmorphism':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-glassmorphism.php';
                case 'christmas':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-christmas.php';
                case 'black_friday':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-black_friday.php';
                case 'dark':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-dark.php';
                case 'exit_intent':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-exit_intent.php';
                case 'bottom':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-bottom.php';
                case 'simple_subscribe':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-simple_subscribe.php';
                case 'newsletter_horizontal':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-newsletter_horizontal.php';
                case 'blank':
                    return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-basic.php';
            default:
                // If unknown template, fall back to basic template
                return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-basic.php';
            }
        } else {
            // Show template selection page
            return include plugin_dir_path(__FILE__) . '/popup_template/template-selection.php';
        }
    }

    /**
     * Edit popup screen
     * @return string
     */
    public function render_edit_popup_subscription()
    {

        return include plugin_dir_path(dirname(__FILE__)) . '/popup_template/template-subscription-basic.php';
    }

    /**
     * Render Image modal
     * @return string
     *
     */
    public function trigger_popup_modal_subscription($id)
    {

        $popup_id = $id;

        return include_once plugin_dir_path(dirname(__FILE__)) . '/popup_modal/modal-subscription.php';
    }
}

new SubscriptionPopup('popup_type_subscription', 'subscription');
}
