<?php
/**
 * Display the admin notice for demo setup with popup.
 */
function author_webstite_templates_show_demo_setup_notice() {
    // Check if the notice should be dismissed forever
    if (get_option('author_website_templates_notice_dismissed_forever')) {
        return;
    }

    // Check if "Remind Me Later" is active (show after 2 days)
    $remind_time = get_option('author_website_templates_notice_remind_time');
    if ($remind_time && time() < $remind_time) {
        return;
    }

    ?>
    <div class="author-website-templates notice notice-success notice-info author-website-templates-notice author-website-templates-welcome-notice">
        <div class="author-website-templates-notice-wrapper">
            <div class="author-website-templates-notice-inner">
                <div class="notice-thumbnail-col">
                    <img src="<?php echo esc_url( RSWPTHEMES_AWT_PLUGIN_URL . 'includes/demo-setup/author-portfolio-pro-thumb.png');?>" alt="<?php esc_attr_e('author website templates', 'author-website-templates');?>">
                </div>
                <div class="notice-content-col">
                    <h3 class="author-website-templates-notice__title">Craft Your Author Website with Ease!</h3>
                    <p class="author-website-templates-notice__content notice-desc">
                        Ready to bring your author presence online? Click <strong>"Setup Demo"</strong> to instantly install a beautifully designed starter website, complete with sample books, blog posts, and author-specific templates—perfectly tailored for writers. This seamless process takes just a few minutes and sets up all the tools you need to showcase your work.
                    </p>
                    <p class="author-website-templates-notice__action">
                        <button id="setup-demo-import" class="button button-primary author-website-templates-notice__button">Setup Author Website Templates</button>
                    </p>
                    <div id="setup-demo-progress" class="author-website-templates-notice__progress"></div>
                    <p class="author-website-templates-notice__content notice-desc">
                        Once set up, editing your site is as simple as drafting a chapter—update your bio, books, and blog with ease using WordPress’s user-friendly tools. After completion, please <a href="<?php echo admin_url('options-permalink.php'); ?>">save your permalinks</a> to ensure everything runs smoothly.
                    </p>
                    <p class="author-website-templates-notice__links">
                        <a href="#" class="author-website-templates-notice__dismiss-forever" data-action="dismiss_forever">Dismiss Forever</a> |
                        <a href="#" class="author-website-templates-notice__remind-later" data-action="remind_later">Remind Me Later</a>
                    </p>
                </div>
            </div>
        </div>
        <!-- Popup Modal -->
        <div id="demo-setup-modal" class="demo-setup-modal" style="display: none;">
            <div class="modal-content">
                <span id="demo-modal-close" class="modal-close">×</span>
                <h3>Demo Setup Progress</h3>
                <div class="warning-notice" style="background: #fff3cd; padding: 10px; margin-bottom: 15px; border: 1px solid #ffeeba; border-radius: 4px;">
                    <strong>⚠️ Please Note:</strong> Keep this tab open during setup. Closing it will interrupt the installation process.
                </div>
                <div id="demo-progress-content"></div>
            </div>
        </div>
    </div>
    <style>
    .notice.notice-info.author-website-templates-welcome-notice .video-documentation span {
        display: inline-block;
        vertical-align: middle;
        margin-bottom: 3px;
        margin-right: 2px;
    }
    p.author-website-templates-notice__action {
        margin-bottom: 20px;
    }

    p.author-website-templates-notice__action button#setup-demo-import {
        padding: 7px 30px;
    }
    .demo-setup-modal {
        position: fixed;
        z-index: 1000;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background-color: rgba(0,0,0,0.5);
    }

    .modal-content {
        background-color: #fff;
        margin: 15% auto;
        padding: 20px;
        border: 1px solid #888;
        width: 80%;
        max-width: 500px;
        border-radius: 5px;
        position: relative;
    }

    .modal-close {
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
        cursor: pointer;
    }

    .modal-close:hover {
        color: #000;
    }
    .notice.notice-info.author-website-templates-welcome-notice .video-documentation {
        background: #ff2000 !important;
        color: #fff !important;
        font-weight: 700;
        padding: 7px 20px;
        text-transform: uppercase;
        border-color: #ff2000
    }

    div#setup-demo-progress {
        margin-bottom: 10px;
    }
    div#author-website-templates-settings label {
        display: block;
        font-weight: 600;
        margin-bottom: 5px;
    }

    div#author-website-templates-settings select {
        width: 80%;
        display: block;
    }

    div#author-website-templates-settings .components-base-control {
        margin-bottom: 10px;
    }
    .notice.notice-info.author-website-templates-welcome-notice {
        padding: 50px;
    }

    .notice.notice-info.author-website-templates-welcome-notice h3 {
        font-size: 24px;
        margin-top: 0;
        margin-bottom: 20px;
    }

    .notice.notice-info.author-website-templates-welcome-notice .welcome-message {
        margin-bottom: 20px;
        font-size: 1rem;
    }

    .author-website-templates-notice-inner {
        display: flex;
        flex-direction: row;
        align-items: center;
    }

    .author-website-templates-notice-inner .notice-thumbnail-col {
        max-width: 42%;
        width: 42%;
    }

    .author-website-templates-notice-inner .notice-thumbnail-col img {
        max-width: 100%;
    }

    .notice-content-col {
        max-width: 58%;
        width: 58%;
        padding-left: 50px;
    }

    .notice-content-col .notice-desc {
        font-size: 18px;
        width: 750px;
        margin-top: 0;
        margin-bottom: 20px;
    }

    .notice-content-col h3 {
        font-size: 22px;
    }

    .notice-content-col a.button.button-highlight {
        background: #f50012;
        border-color: #f50012;
    }

    a.button.button-highlight.upgrade-to-pro.button-primary {
        background: #fcd602;
        border-color: #fcd602;
        color: #000;
        font-weight: 700;
    }
    </style>
    <?php
}
add_action('admin_notices', 'author_webstite_templates_show_demo_setup_notice');

/**
 * Handle notice dismissal and remind actions via AJAX
 */
function handle_author_website_templates_notice_actions() {
    if (isset($_POST['action_type']) && check_admin_referer('author_website_templates_notice_action', 'notice_nonce')) {
        $action = sanitize_text_field($_POST['action_type']);

        if ($action === 'dismiss_forever') {
            update_option('author_website_templates_notice_dismissed_forever', true);
            wp_send_json_success();
        } elseif ($action === 'remind_later') {
            $remind_time = time() + (2 * 24 * 60 * 60); // 2 days
            update_option('author_website_templates_notice_remind_time', $remind_time);
            wp_send_json_success();
        }
    }
    wp_send_json_error();
}
add_action('wp_ajax_author_website_templates_notice_action', 'handle_author_website_templates_notice_actions');

// add_action('admin_init', function(){
//     delete_option( 'demo_setup_done' );
// });

/**
 * Enqueue JavaScript for demo setup process.
 */
function author_website_templates_enqueue_demo_setup_scripts($hook) {

    wp_enqueue_script('setup-demo-script', RSWPTHEMES_AWT_PLUGIN_URL . 'includes/demo-setup/demo-setup.js', ['jquery'], '1.0', true);

    // Localize script to pass AJAX URL
    wp_localize_script('setup-demo-script', 'setupDemoAjax', [
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce'    => wp_create_nonce('setup_demo_nonce'),
    ]);
}
add_action('admin_enqueue_scripts', 'author_website_templates_enqueue_demo_setup_scripts');
