<?php
if (class_exists('Rswpbs') || class_exists('BlogDesignerForElementor')) {
    return;
}
function book_review_blog_amz_import_admin_notice() {
    $plugin_slug = 'rs-wp-books-showcase';
    $plugin_file = 'rs-wp-books-showcase/rs-wp-books-showcase.php';
    $plugin_installed = file_exists(WP_PLUGIN_DIR . '/' . $plugin_file);
    $plugin_active = is_plugin_active($plugin_file);

    $user_id = get_current_user_id();
    $dismissed_time = get_user_meta($user_id, 'book_review_blog_amz_notice_dismissed_time', true);
    $dismissed_forever = get_user_meta($user_id, 'book_review_blog_amz_notice_dismissed_forever', true);

    // Prevent showing the notice if dismissed forever
    if ($dismissed_forever) {
        return;
    }

    // If dismissed temporarily, check if 24 hours have passed
    if ($dismissed_time && (time() - $dismissed_time) < 86400) {
        return;
    }
    ?>
    <div class="notice notice-info is-dismissible book-review-blog-amz-import-admin-notice">
        <h3 class="amz-notice-heading"><?php echo esc_html__('🚀 Effortless Book Catalog + Affiliate Earnings! 📚💰', 'book-review-blog'); ?></h3>
        <p class="amz-notice-sub-heading"><strong>
            <?php echo esc_html__('Want to monetize your website effortlessly? Now, you can import 1,000+ books from Amazon to your website site in just 10 minutes – no manual work needed!', 'book-review-blog'); ?></strong>
        </p>
        <ul>
            <li><?php echo esc_html__('✅ ', 'book-review-blog'); ?><strong><?php echo esc_html__('Instant Book Catalog –', 'book-review-blog'); ?></strong> <?php echo esc_html__('Add hundreds (or thousands) of books with just a few clicks. No need to manually enter titles, descriptions, or images!', 'book-review-blog'); ?></li>

            <li><?php echo esc_html__('✅ ', 'book-review-blog'); ?><strong><?php echo esc_html__('Earn Commissions Automatically –', 'book-review-blog'); ?></strong> <?php echo esc_html__('Insert your Amazon Tracking ID and earn every time someone buys a book through your website.', 'book-review-blog'); ?></li>

            <li><?php echo esc_html__('✅ ', 'book-review-blog'); ?><strong><?php echo esc_html__('Works for Any Niche –', 'book-review-blog'); ?></strong> <?php echo esc_html__('Whether your site is about business, fitness, self-improvement, cooking, tech, or anything else, you can recommend relevant books to your audience.', 'book-review-blog'); ?></li>

            <li><?php echo esc_html__('✅ ', 'book-review-blog'); ?><strong><?php echo esc_html__('The Bigger Your Catalog, The More You Earn –', 'book-review-blog'); ?></strong> <?php echo esc_html__('A large book collection = higher chances of sales & commissions!', 'book-review-blog'); ?></li>

            <li><?php echo esc_html__('✅ ', 'book-review-blog'); ?><strong><?php echo esc_html__('No Tech Skills Needed –', 'book-review-blog'); ?></strong> <?php echo esc_html__('Set up everything easily with our step-by-step video guide included in the Import Books from Amazon page.', 'book-review-blog'); ?></li>
        </ul>
        <p><strong><?php echo esc_html__('Get Started in Just a Few Clicks!', 'book-review-blog'); ?></strong></p>
        <div class="rswpbs-amz-admin-notice-btn-wrapper">
            <?php if ($plugin_active) : ?>
                <a href="<?php echo esc_url(admin_url('edit.php?post_type=book&page=import-books-from-json')); ?>" class="import-books-from-amazon-btn button button-secondary"><span class="dashicons dashicons-amazon"></span>
                    <?php esc_html_e('Import Books from Amazon', 'book-review-blog'); ?>
                </a>
            <?php else : ?>
                <a href="#" class="import-books-from-amazon-btn button button-secondary book-review-blog-rswpbs-install" data-plugin="<?php echo esc_attr($plugin_slug); ?>">
                    <span class="dashicons dashicons-amazon"></span><?php echo esc_html__('Import Books from Amazon', 'book-review-blog'); ?>
                </a>
            <?php endif; ?>
        </div>
        <p><strong><?php echo esc_html__('💰 Start building your book catalog today and turn your website into a passive income machine!', 'book-review-blog'); ?></strong></p>
        <div class="rswpbs-notice-dismiss-links">
            <a href="#" class="book-review-blog-dismiss-forever" data-nonce="<?php echo wp_create_nonce('book_review_blog_amz_dismiss_forever'); ?>">Dismiss Forever</a> |
            <a href="#" class="book-review-blog-remind-later" data-nonce="<?php echo wp_create_nonce('book_review_blog_amz_remind_later'); ?>">Remind Me Later</a>
        </div>
    </div>
    <?php
}
add_action('admin_notices', 'book_review_blog_amz_import_admin_notice');


/**
 * Install RS WP Book Showcase
 */
add_action( 'wp_ajax_install_rswpbs_only', 'book_review_blog_install_activate_rswpbs_only' );

function book_review_blog_install_activate_rswpbs_only() {
    /**
     * Install Plugin.
     */
    include_once ABSPATH . '/wp-admin/includes/file.php';
    include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
    include_once ABSPATH . 'wp-admin/includes/plugin-install.php';

    if ( ! file_exists( WP_PLUGIN_DIR . '/rs-wp-books-showcase' ) ) {
        $api = plugins_api( 'plugin_information', array(
            'slug'   => sanitize_key( wp_unslash( 'rs-wp-books-showcase' ) ),
            'fields' => array(
                'sections' => false,
            ),
        ) );
        $skin     = new WP_Ajax_Upgrader_Skin();
        $upgrader = new Plugin_Upgrader( $skin );
        $result   = $upgrader->install( $api->download_link );
    }
    // Activate plugin.
    if ( current_user_can( 'activate_plugin' ) ) {
        $result = activate_plugin( 'rs-wp-books-showcase/rs-wp-books-showcase.php' );
    }
}

// Handle notice dismissal and store time
function book_review_blog_amz_dismiss_notice() {
    check_ajax_referer('book_review_blog_amz_dismiss_notice', 'security');
    update_user_meta(get_current_user_id(), 'book_review_blog_amz_notice_dismissed_time', time());
    wp_die();
}
add_action('wp_ajax_book_review_blog_amz_dismiss_notice', 'book_review_blog_amz_dismiss_notice');

/**
 * RS WP Book Showcase Installer Script
 */
function book_review_blog_rswpbs_installer_script(){
    wp_enqueue_script( 'book-review-blog-rswpbs-installer-script', get_theme_file_uri('/inc/install-rswpbs/install-rswpbs-script.js'), array( 'jquery' ), '', true );
    wp_localize_script( 'book-review-blog-rswpbs-installer-script', 'book_review_blog_rswpbs_ajax_object',
        array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ) )
    );
}
add_action( 'admin_enqueue_scripts', 'book_review_blog_rswpbs_installer_script' );

function book_review_blog_amz_dismiss_forever() {
    check_ajax_referer('book_review_blog_amz_dismiss_forever', 'security');
    update_user_meta(get_current_user_id(), 'book_review_blog_amz_notice_dismissed_forever', 1);
    wp_die();
}
add_action('wp_ajax_book_review_blog_amz_dismiss_forever', 'book_review_blog_amz_dismiss_forever');


function book_review_blog_amz_remind_later() {
    check_ajax_referer('book_review_blog_amz_remind_later', 'security');
    update_user_meta(get_current_user_id(), 'book_review_blog_amz_notice_dismissed_time', time());
    wp_die();
}
add_action('wp_ajax_book_review_blog_amz_remind_later', 'book_review_blog_amz_remind_later');
