<?php

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

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

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

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

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

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

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

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

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

        wp_die(); // Terminate AJAX request
    }
}

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