<?php

class Stripe_Express_Welcome {
    public function __construct() {
        add_action('admin_enqueue_scripts', array($this, 'enqueue_styles'));
        add_action('admin_footer', array($this, 'render_popup'));
        add_action('wp_ajax_stripe_express_dismiss_welcome', array($this, 'dismiss_welcome'));
    }

    public function should_show_popup() {
        $current_page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
        $is_stripe_page = ($current_page === 'stripe-express');
        return !get_option('stripe_express_welcome_dismissed') && $is_stripe_page;
    }

    public function enqueue_styles() {
        if (!$this->should_show_popup()) {
            return;
        }

        wp_enqueue_style(
            'stripe-express-welcome', 
            plugins_url('includes/assets/css/welcome.css', dirname(__FILE__)),
            array(),
            IT_STRIPE_EXPRESS_VERSION
        );

        wp_enqueue_script(
            'stripe-express-welcome',
            plugins_url('includes/assets/js/welcome.js', dirname(__FILE__)),
            array('jquery'),
            IT_STRIPE_EXPRESS_VERSION,
            true
        );

        wp_localize_script('stripe-express-welcome', 'stripeExpressWelcome', array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('stripe-express-welcome')
        ));
    }

    public function render_popup() {
        if (!$this->should_show_popup()) {
            return;
        }
        ?>
        <div class="stripe-express-welcome-overlay">
            <div class="stripe-express-welcome-popup">
                <span class="stripe-express-welcome-close">&times;</span>
                <h2>Welcome to Stripe Express!</h2>
                <p>We've developed dedicated Stripe payment solutions for popular platforms. If you're using any of these platforms, we recommend trying our specialized plugins for the best integration experience:</p>
                <div class="stripe-express-plugins">
                    <div class="stripe-express-plugin-item">
                        <h3>Elementor Stripe Plugin</h3>
                        <p>Perfect integration with Elementor page builder</p>
                        <a href="https://wordpress.org/plugins/pay-addons-for-elementor" target="_blank" class="button button-primary">Learn More</a>
                    </div>
                    <div class="stripe-express-plugin-item">
                        <h3>WooCommerce Stripe Plugin</h3>
                        <p>Seamless integration with WooCommerce</p>
                        <a href="https://wordpress.org/plugins/payment-addons-for-woocommerce" target="_blank" class="button button-primary">Learn More</a>
                    </div>
                    <div class="stripe-express-plugin-item">
                        <h3>Contact Form 7 Stripe Plugin</h3>
                        <p>Add payments to your Contact Form 7 forms</p>
                        <a href="https://wordpress.org/plugins/accept-stripe-for-contact-form-7/" target="_blank" class="button button-primary">Learn More</a>
                    </div>
                </div>
                <br/>
                <div>For more details, please visit our official website: <a href="https://payaddons.com" target="_blank">https://payaddons.com</div>
            </div>
        </div>
        <?php
    }

    public function dismiss_welcome() {
        check_ajax_referer('stripe-express-welcome', 'nonce');
        update_option('stripe_express_welcome_dismissed', true);
        wp_send_json_success();
    }
}