<?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('wp_ajax_eryndo_dismiss_notice', [$this, 'dismiss_notice_ajax']);
        add_action('after_switch_theme', [$this, 'reset_notice_on_activation']);
    }

    /**
     * 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');

        ?>
        <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">
                        <svg width="28" height="28" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M12 2L13.09 8.26L20 9L13.09 9.74L12 16L10.91 9.74L4 9L10.91 8.26L12 2Z" fill="currentColor"/>
                            <path d="M19 15L19.5 17L21 17.5L19.5 18L19 20L18.5 18L17 17.5L18.5 17L19 15Z" fill="currentColor"/>
                        </svg>
                    </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">
                    <div class="eryndo-feature-item">
                        <div class="eryndo-feature-icon">🎨</div>
                        <span><?php echo esc_html__('Custom Block Patterns', 'eryndo'); ?></span>
                    </div>
                    <div class="eryndo-feature-item">
                        <div class="eryndo-feature-icon">⚡</div>
                        <span><?php echo esc_html__('Enhanced Performance', 'eryndo'); ?></span>
                    </div>
                    <div class="eryndo-feature-item">
                        <div class="eryndo-feature-icon">🔧</div>
                        <span><?php echo esc_html__('Advanced Customization', 'eryndo'); ?></span>
                    </div>
                </div>
                
                <div class="eryndo-notice-actions">
                    <a href="<?php echo esc_url($action_url); ?>" class="eryndo-btn eryndo-btn-primary">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M12 2L2 7L12 12L22 7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                            <path d="M2 17L12 22L22 17" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                            <path d="M2 12L12 17L22 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                        </svg>
                        <?php echo $button_text; ?>
                    </a>
                    <a href="<?php echo esc_url($docs_url); ?>" class="eryndo-btn eryndo-btn-secondary" target="_blank" rel="noopener">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M14 2H6A2 2 0 0 0 4 4V20A2 2 0 0 0 6 22H18A2 2 0 0 0 20 20V8L14 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                            <path d="M14 2V8H20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                        </svg>
                        <?php echo esc_html__('Documentation', 'eryndo'); ?>
                    </a>
                    <button class="eryndo-btn eryndo-btn-outline eryndo-notice-toggle" type="button">
                        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path d="M6 9L12 15L18 9" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                        </svg>
                        <?php echo esc_html__('Show Details', 'eryndo'); ?>
                    </button>
                </div>
                
                <div class="eryndo-notice-details" style="display: none;">
                    <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>

        <script type="text/javascript">
        jQuery(document).ready(function($) {
            // Handle notice dismissal
            $(document).on('click', '.eryndo-zoloblocks-notice .notice-dismiss', function() {
                var nonce = $('.eryndo-zoloblocks-notice').data('nonce');
                
                $.ajax({
                    url: ajaxurl,
                    type: 'POST',
                    data: {
                        action: 'eryndo_dismiss_notice',
                        nonce: nonce
                    }
                });
            });
        });
        </script>

        <style>
        .eryndo-zoloblocks-notice {
            border-left: 4px solid #6366f1;
            background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
            margin: 5px 0 15px;
            padding: 0;
            border-radius: 8px;
            overflow: hidden;
        }
        
        .eryndo-notice-container {
            padding: 24px;
        }
        
        .eryndo-notice-header {
            display: flex;
            align-items: flex-start;
            gap: 16px;
            margin-bottom: 20px;
        }
        
        .eryndo-notice-icon {
            flex-shrink: 0;
            width: 48px;
            height: 48px;
            background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
        }
        
        .eryndo-notice-content {
            flex: 1;
        }
        
        .eryndo-notice-title {
            margin: 0 0 8px 0;
            font-size: 18px;
            font-weight: 700;
            color: #1e293b;
            line-height: 1.3;
        }
        
        .eryndo-notice-description {
            margin: 0;
            color: #64748b;
            line-height: 1.6;
            font-size: 14px;
        }
        
        .eryndo-notice-features {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 16px;
            margin-bottom: 24px;
            padding: 20px;
            background: rgba(99, 102, 241, 0.05);
            border-radius: 8px;
            border: 1px solid rgba(99, 102, 241, 0.1);
        }
        
        .eryndo-feature-item {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 14px;
            color: #475569;
            font-weight: 500;
        }
        
        .eryndo-feature-icon {
            font-size: 18px;
            width: 24px;
            text-align: center;
        }
        
        .eryndo-notice-actions {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }
        
        .eryndo-btn {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 12px 20px;
            border-radius: 6px;
            text-decoration: none;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.3s ease;
            border: none;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        
        .eryndo-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }
        
        .eryndo-btn:hover::before {
            left: 100%;
        }
        
        .eryndo-btn-primary {
            background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
            color: #fff;
            box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
        }
        
        .eryndo-btn-primary:hover {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
            color: #fff;
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
        }
        
        .eryndo-btn-secondary {
            background: #ffffff;
            color: #6366f1;
            border: 2px solid #e2e8f0;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }
        
        .eryndo-btn-secondary:hover {
            background: #f8fafc;
            color: #4f46e5;
            border-color: #6366f1;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(99, 102, 241, 0.15);
        }
        
        .eryndo-btn-outline {
            background: transparent;
            color: #64748b;
            border: 1px solid #cbd5e1;
        }
        
        .eryndo-btn-outline:hover {
            background: #f1f5f9;
            color: #475569;
            border-color: #94a3b8;
        }
        
        .eryndo-notice-details {
            margin-top: 20px;
            padding: 20px;
            background: #f8fafc;
            border-radius: 8px;
            border: 1px solid #e2e8f0;
            animation: slideDown 0.3s ease-out;
        }
        
        .eryndo-notice-details.show {
            display: block !important;
        }
        
        .eryndo-details-content h4 {
            margin: 0 0 16px 0;
            font-size: 16px;
            font-weight: 600;
            color: #1e293b;
        }
        
        .eryndo-benefits-list {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        .eryndo-benefits-list li {
            padding: 8px 0;
            border-bottom: 1px solid #e2e8f0;
            color: #475569;
            line-height: 1.5;
        }
        
        .eryndo-benefits-list li:last-child {
            border-bottom: none;
        }
        
        .eryndo-benefits-list strong {
            color: #1e293b;
        }
        
        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .eryndo-notice-toggle svg {
            transition: transform 0.3s ease;
        }
        
        .eryndo-notice-toggle.active svg {
            transform: rotate(180deg);
        }
        
        @media (max-width: 768px) {
            .eryndo-notice-container {
                padding: 20px;
            }
            
            .eryndo-notice-header {
                flex-direction: column;
                text-align: center;
                gap: 12px;
            }
            
            .eryndo-notice-features {
                grid-template-columns: 1fr;
                gap: 12px;
            }
            
            .eryndo-notice-actions {
                flex-direction: column;
                gap: 10px;
            }
            
            .eryndo-btn {
                justify-content: center;
                width: 100%;
            }
        }
        </style>
        
        <script>
        jQuery(document).ready(function($) {
            // Toggle details functionality
            $('.eryndo-notice-toggle').on('click', function(e) {
                e.preventDefault();
                
                var $button = $(this);
                var $details = $('.eryndo-notice-details');
                var $buttonText = $button.find('span') || $button;
                
                if ($details.is(':visible')) {
                    $details.slideUp(300);
                    $button.removeClass('active');
                    $buttonText.text('<?php echo esc_js(__('Show Details', 'eryndo')); ?>');
                } else {
                    $details.slideDown(300);
                    $button.addClass('active');
                    $buttonText.text('<?php echo esc_js(__('Hide Details', 'eryndo')); ?>');
                }
            });
            
            // Enhanced dismiss functionality with animation
            $('.eryndo-zoloblocks-notice .notice-dismiss').on('click', function() {
                var $notice = $(this).closest('.eryndo-zoloblocks-notice');
                $notice.fadeOut(300, function() {
                    $notice.remove();
                });
            });
            
            // Add hover effects for feature items
            $('.eryndo-feature-item').hover(
                function() {
                    $(this).css({
                        'transform': 'translateY(-2px)',
                        'transition': 'transform 0.2s ease'
                    });
                },
                function() {
                    $(this).css({
                        'transform': 'translateY(0)',
                        'transition': 'transform 0.2s ease'
                    });
                }
            );
            
            // Add entrance animation
            $('.eryndo-zoloblocks-notice').hide().fadeIn(600);
        });
        </script>
        
        <?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();
