<?php

/**
 * Eryndo Admin Notice Class
 *
 * This class handles admin notices for the Eryndo theme,
 * specifically for recommending the ZoloBlocks plugin installation.
 *
 * @package Eryndo
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * Eryndo_Admin_Notice Class
 *
 * Handles the display of admin notices for plugin recommendations.
 */
class Eryndo_Admin_Notice
{
    /**
     * Notice option name for dismissal tracking
     */
    const NOTICE_OPTION = 'eryndo_zoloblocks_notice_dismissed';

    /**
     * ZoloBlocks plugin file path
     */
    const ZOLOBLOCKS_PLUGIN = 'zoloblocks/zoloblocks.php';

    /**
     * Initialize the admin notice functionality
     */
    public function __construct()
    {
        add_action('admin_notices', [$this, 'show_zoloblocks_notice']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_notice_assets']);
        add_action('wp_ajax_eryndo_dismiss_notice', [$this, 'dismiss_notice_ajax']);
        add_action('after_switch_theme', [$this, 'reset_notice_on_activation']);
    }

    /**
     * Enqueue admin notice styles and scripts when the notice may display.
     *
     * @param string $hook_suffix Current admin screen hook suffix.
     */
    public function enqueue_admin_notice_assets($hook_suffix)
    {
        if ($this->is_zoloblocks_active() || $this->is_notice_dismissed()) {
            return;
        }

        $theme_version = wp_get_theme()->get('Version');

        wp_enqueue_style(
            'eryndo-admin-notice',
            get_template_directory_uri() . '/assets/css/admin-notice.css',
            array(),
            $theme_version
        );

        wp_enqueue_script(
            'eryndo-admin-notice',
            get_template_directory_uri() . '/assets/js/admin-notice.js',
            array('jquery'),
            $theme_version,
            true
        );
    }

    /**
     * Reset notice dismissal status when theme is activated
     */
    public function reset_notice_on_activation()
    {
        delete_option(self::NOTICE_OPTION);
    }

    /**
     * Check if ZoloBlocks plugin is active
     *
     * @return bool
     */
    private function is_zoloblocks_active()
    {
        return is_plugin_active(self::ZOLOBLOCKS_PLUGIN);
    }

    /**
     * Check if ZoloBlocks plugin is installed
     *
     * @return bool
     */
    private function is_zoloblocks_installed()
    {
        return file_exists(WP_PLUGIN_DIR . '/' . self::ZOLOBLOCKS_PLUGIN);
    }

    /**
     * Check if notice has been dismissed
     *
     * @return bool
     */
    private function is_notice_dismissed()
    {
        return get_option(self::NOTICE_OPTION, false);
    }

    /**
     * Show ZoloBlocks recommendation notice
     */
    public function show_zoloblocks_notice()
    {
        // Don't show if ZoloBlocks is already active
        if ($this->is_zoloblocks_active()) {
            return;
        }

        // Don't show if notice has been dismissed
        if ($this->is_notice_dismissed()) {
            return;
        }

        // Only show on admin pages
        if (!is_admin()) {
            return;
        }

        // Check if plugin is installed to determine the appropriate action
        $is_installed = $this->is_zoloblocks_installed();
        
        if ($is_installed) {
            // Plugin is installed but not activated - show activation URL
            $action_url = wp_nonce_url(
                self_admin_url('plugins.php?action=activate&plugin=' . urlencode(self::ZOLOBLOCKS_PLUGIN)),
                'activate-plugin_' . self::ZOLOBLOCKS_PLUGIN
            );
            $button_text = esc_html__('Activate ZoloBlocks', 'eryndo');
        } else {
            // Plugin is not installed - show installation URL
            $action_url = wp_nonce_url(
                self_admin_url('update.php?action=install-plugin&plugin=zoloblocks'),
                'install-plugin_zoloblocks'
            );
            $button_text = esc_html__('Install ZoloBlocks', 'eryndo');
        }

        // Documentation URL
        $docs_url = 'https://zoloblocks.com/';

        // Generate nonce for AJAX dismissal
        $dismiss_nonce = wp_create_nonce('eryndo_dismiss_notice');

        $label_show_details = esc_html__( 'Show Details', 'eryndo' );
        $label_hide_details = esc_html__( 'Hide Details', 'eryndo' );

        ?>
        <div class="notice notice-info is-dismissible eryndo-zoloblocks-notice" data-nonce="<?php echo esc_attr( $dismiss_nonce ); ?>">
            <div class="eryndo-notice-container">
                <div class="eryndo-notice-header">
                    <div class="eryndo-notice-icon" aria-hidden="true">
                        <span class="dashicons dashicons-admin-customizer"></span>
                    </div>
                    <div class="eryndo-notice-content">
                        <h3 class="eryndo-notice-title">
                            <?php echo esc_html__( 'Welcome to Eryndo Theme!', 'eryndo' ); ?>
                        </h3>
                        <p class="eryndo-notice-description">
                            <?php echo esc_html__( 'Unlock the full potential of your website with ZoloBlocks - featuring custom patterns, advanced blocks, and premium design elements.', 'eryndo' ); ?>
                        </p>
                    </div>
                </div>

                <div class="eryndo-notice-features">
                    <p class="eryndo-feature-item">
                        <span class="dashicons dashicons-layout" aria-hidden="true"></span>
                        <span><?php echo esc_html__( 'Custom Block Patterns', 'eryndo' ); ?></span>
                    </p>
                    <p class="eryndo-feature-item">
                        <span class="dashicons dashicons-dashboard" aria-hidden="true"></span>
                        <span><?php echo esc_html__( 'Enhanced Performance', 'eryndo' ); ?></span>
                    </p>
                    <p class="eryndo-feature-item">
                        <span class="dashicons dashicons-admin-tools" aria-hidden="true"></span>
                        <span><?php echo esc_html__( 'Advanced Customization', 'eryndo' ); ?></span>
                    </p>
                </div>

                <div class="eryndo-notice-actions">
                    <a href="<?php echo esc_url( $action_url ); ?>" class="button button-primary">
                        <span class="dashicons dashicons-download" aria-hidden="true"></span>
                        <?php echo esc_html( $button_text ); ?>
                    </a>
                    <a href="<?php echo esc_url( $docs_url ); ?>" class="button button-secondary" target="_blank" rel="noopener noreferrer">
                        <span class="dashicons dashicons-book-alt" aria-hidden="true"></span>
                        <?php echo esc_html__( 'Documentation', 'eryndo' ); ?>
                        <span class="screen-reader-text"><?php echo esc_html__( '(opens in a new tab)', 'eryndo' ); ?></span>
                    </a>
                    <button
                        type="button"
                        class="button button-secondary eryndo-notice-toggle"
                        aria-expanded="false"
                        aria-controls="eryndo-zoloblocks-details"
                        data-label-show="<?php echo esc_attr( $label_show_details ); ?>"
                        data-label-hide="<?php echo esc_attr( $label_hide_details ); ?>"
                    >
                        <span class="dashicons dashicons-arrow-down-alt2" aria-hidden="true"></span>
                        <span class="eryndo-toggle-label"><?php echo esc_html( $label_show_details ); ?></span>
                    </button>
                </div>

                <div id="eryndo-zoloblocks-details" class="eryndo-notice-details">
                    <div class="eryndo-details-content">
                        <h4><?php echo esc_html__( 'What you\'ll get with ZoloBlocks:', 'eryndo' ); ?></h4>
                        <ul class="eryndo-benefits-list">
                            <li><strong><?php echo esc_html__( '50+ Block Patterns', 'eryndo' ); ?></strong> — <?php echo esc_html__( 'Ready-to-use design templates', 'eryndo' ); ?></li>
                            <li><strong><?php echo esc_html__( 'Advanced Blocks', 'eryndo' ); ?></strong> — <?php echo esc_html__( 'Enhanced Gutenberg blocks with more options', 'eryndo' ); ?></li>
                            <li><strong><?php echo esc_html__( 'Performance Optimized', 'eryndo' ); ?></strong> — <?php echo esc_html__( 'Lightweight and fast-loading components', 'eryndo' ); ?></li>
                            <li><strong><?php echo esc_html__( 'Regular Updates', 'eryndo' ); ?></strong> — <?php echo esc_html__( 'New patterns and features added monthly', 'eryndo' ); ?></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Handle AJAX request to dismiss notice
     */
    public function dismiss_notice_ajax()
    {
        // Verify nonce
        if (!wp_verify_nonce($_POST['nonce'], 'eryndo_dismiss_notice')) {
            wp_die(__('Security check failed.', 'eryndo'));
        }

        // Update option to mark notice as dismissed
        update_option(self::NOTICE_OPTION, true);

        wp_die(); // Terminate AJAX request
    }
}

// Initialize the admin notice class
new Eryndo_Admin_Notice();
