<?php

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

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

/**
 * Cryzel_Admin_Notice Class
 *
 * Handles the display of admin notices for plugin recommendations.
 */
class Cryzel_Admin_Notice
{
    /**
     * Notice option name for dismissal tracking
     */
    const NOTICE_OPTION = 'cryzel_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_cryzel_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', 'cryzel');
        } 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', 'cryzel');
        }

        // Documentation URL (you can customize this)
        $docs_url = 'https://zoloblocks.com/';

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

        ?>
        <div class="notice notice-info is-dismissible cryzel-zoloblocks-notice" data-nonce="<?php echo esc_attr($dismiss_nonce); ?>">
            <div class="cryzel-notice-container">
                <div class="cryzel-notice-header">
                    <div class="cryzel-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="cryzel-notice-content">
                        <h3 class="cryzel-notice-title">
                            <?php echo esc_html__('Welcome to Cryzel Theme!', 'cryzel'); ?>
                        </h3>
                        <p class="cryzel-notice-description">
                            <?php echo esc_html__('Unlock the full potential of your website with ZoloBlocks - featuring custom patterns, advanced blocks, and premium design elements.', 'cryzel'); ?>
                        </p>
                    </div>
                </div>
                
                <div class="cryzel-notice-features">
                    <div class="cryzel-feature-item">
                        <div class="cryzel-feature-icon">🎨</div>
                        <span><?php echo esc_html__('Custom Block Patterns', 'cryzel'); ?></span>
                    </div>
                    <div class="cryzel-feature-item">
                        <div class="cryzel-feature-icon">⚡</div>
                        <span><?php echo esc_html__('Enhanced Performance', 'cryzel'); ?></span>
                    </div>
                    <div class="cryzel-feature-item">
                        <div class="cryzel-feature-icon">🔧</div>
                        <span><?php echo esc_html__('Advanced Customization', 'cryzel'); ?></span>
                    </div>
                </div>
                
                <div class="cryzel-notice-actions">
                    <a href="<?php echo esc_url($action_url); ?>" class="cryzel-btn cryzel-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="cryzel-btn cryzel-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', 'cryzel'); ?>
                    </a>
                    <button class="cryzel-btn cryzel-btn-outline cryzel-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', 'cryzel'); ?>
                    </button>
                </div>
                
                <div class="cryzel-notice-details" style="display: none;">
                    <div class="cryzel-details-content">
                        <h4><?php echo esc_html__('What you\'ll get with ZoloBlocks:', 'cryzel'); ?></h4>
                        <ul class="cryzel-benefits-list">
                            <li><strong><?php echo esc_html__('50+ Block Patterns', 'cryzel'); ?></strong> - <?php echo esc_html__('Ready-to-use design templates', 'cryzel'); ?></li>
                            <li><strong><?php echo esc_html__('Advanced Blocks', 'cryzel'); ?></strong> - <?php echo esc_html__('Enhanced Gutenberg blocks with more options', 'cryzel'); ?></li>
                            <li><strong><?php echo esc_html__('Performance Optimized', 'cryzel'); ?></strong> - <?php echo esc_html__('Lightweight and fast-loading components', 'cryzel'); ?></li>
                            <li><strong><?php echo esc_html__('Regular Updates', 'cryzel'); ?></strong> - <?php echo esc_html__('New patterns and features added monthly', 'cryzel'); ?></li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

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

        <style>
        .cryzel-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;
        }
        
        .cryzel-notice-container {
            padding: 24px;
        }
        
        .cryzel-notice-header {
            display: flex;
            align-items: flex-start;
            gap: 16px;
            margin-bottom: 20px;
        }
        
        .cryzel-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);
        }
        
        .cryzel-notice-content {
            flex: 1;
        }
        
        .cryzel-notice-title {
            margin: 0 0 8px 0;
            font-size: 18px;
            font-weight: 700;
            color: #1e293b;
            line-height: 1.3;
        }
        
        .cryzel-notice-description {
            margin: 0;
            color: #64748b;
            line-height: 1.6;
            font-size: 14px;
        }
        
        .cryzel-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);
        }
        
        .cryzel-feature-item {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 14px;
            color: #475569;
            font-weight: 500;
        }
        
        .cryzel-feature-icon {
            font-size: 18px;
            width: 24px;
            text-align: center;
        }
        
        .cryzel-notice-actions {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }
        
        .cryzel-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;
        }
        
        .cryzel-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;
        }
        
        .cryzel-btn:hover::before {
            left: 100%;
        }
        
        .cryzel-btn-primary {
            background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
            color: #fff;
            box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
        }
        
        .cryzel-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);
        }
        
        .cryzel-btn-secondary {
            background: #ffffff;
            color: #6366f1;
            border: 2px solid #e2e8f0;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }
        
        .cryzel-btn-secondary:hover {
            background: #f8fafc;
            color: #4f46e5;
            border-color: #6366f1;
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(99, 102, 241, 0.15);
        }
        
        .cryzel-btn-outline {
            background: transparent;
            color: #64748b;
            border: 1px solid #cbd5e1;
        }
        
        .cryzel-btn-outline:hover {
            background: #f1f5f9;
            color: #475569;
            border-color: #94a3b8;
        }
        
        .cryzel-notice-details {
            margin-top: 20px;
            padding: 20px;
            background: #f8fafc;
            border-radius: 8px;
            border: 1px solid #e2e8f0;
            animation: slideDown 0.3s ease-out;
        }
        
        .cryzel-notice-details.show {
            display: block !important;
        }
        
        .cryzel-details-content h4 {
            margin: 0 0 16px 0;
            font-size: 16px;
            font-weight: 600;
            color: #1e293b;
        }
        
        .cryzel-benefits-list {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        
        .cryzel-benefits-list li {
            padding: 8px 0;
            border-bottom: 1px solid #e2e8f0;
            color: #475569;
            line-height: 1.5;
        }
        
        .cryzel-benefits-list li:last-child {
            border-bottom: none;
        }
        
        .cryzel-benefits-list strong {
            color: #1e293b;
        }
        
        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .cryzel-notice-toggle svg {
            transition: transform 0.3s ease;
        }
        
        .cryzel-notice-toggle.active svg {
            transform: rotate(180deg);
        }
        
        @media (max-width: 768px) {
            .cryzel-notice-container {
                padding: 20px;
            }
            
            .cryzel-notice-header {
                flex-direction: column;
                text-align: center;
                gap: 12px;
            }
            
            .cryzel-notice-features {
                grid-template-columns: 1fr;
                gap: 12px;
            }
            
            .cryzel-notice-actions {
                flex-direction: column;
                gap: 10px;
            }
            
            .cryzel-btn {
                justify-content: center;
                width: 100%;
            }
        }
        </style>
        
        <script>
        jQuery(document).ready(function($) {
            // Toggle details functionality
            $('.cryzel-notice-toggle').on('click', function(e) {
                e.preventDefault();
                
                var $button = $(this);
                var $details = $('.cryzel-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', 'cryzel')); ?>');
                } else {
                    $details.slideDown(300);
                    $button.addClass('active');
                    $buttonText.text('<?php echo esc_js(__('Hide Details', 'cryzel')); ?>');
                }
            });
            
            // Enhanced dismiss functionality with animation
            $('.cryzel-zoloblocks-notice .notice-dismiss').on('click', function() {
                var $notice = $(this).closest('.cryzel-zoloblocks-notice');
                $notice.fadeOut(300, function() {
                    $notice.remove();
                });
            });
            
            // Add hover effects for feature items
            $('.cryzel-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
            $('.cryzel-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'], 'cryzel_dismiss_notice')) {
            wp_die(__('Security check failed.', 'cryzel'));
        }

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

        wp_die(); // Terminate AJAX request
    }
}

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