<?php
/**
 * ExploreXR Premium Add-ons Management Page
 * 
 * Separate page for managing add-ons and their global settings
 */

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

/**
 * Render the add-ons management page
 */
function explorexr_premium_addons_page() {
    // Get license handler
    if (!class_exists('ExploreXR_License_Handler')) {
        ?>
        <div class="wrap">
            <h1>ExploreXR Add-ons Manager</h1>
            <div class="notice notice-error">
                <p><strong>Error:</strong> License system not available. Please check your ExploreXR Premium installation.</p>
            </div>
        </div>
        <?php
        return;
    }
    
    $license_handler = ExploreXR_License_Handler::instance();
    
    // Get license data
    $license_data = $license_handler->get_license_data();
    $license_status = isset($license_data['status']) ? $license_data['status'] : 'inactive';
    $license_tier = isset($license_data['tier']) ? $license_data['tier'] : 'free';
    
    // Get available add-ons
    $addons = $license_handler->get_addons();
    $activated_addons = $license_handler->get_activated_addons();
    $max_addons = $license_handler->get_max_addons();
    
    // Get addon manager
    $addon_manager = class_exists('ExploreXR_Addon_Manager') ? ExploreXR_Addon_Manager::get_instance() : null;
    
    ?>
    <div class="wrap explorexr-addons-page">
        <?php 
        // Display WordPress admin notices BEFORE the header
        settings_errors();
        ?>
        
        <?php 
        $page_title = 'ExploreXR Add-ons Manager';
        include EXPLOREXR_PLUGIN_DIR . 'admin/templates/admin-header.php'; 
        ?>
        
        <!-- License Status Summary -->
        <div class="explorexr-license-summary">
            <?php if ($license_status === 'active'): ?>
                <div class="license-badge active">
                    <span class="dashicons dashicons-yes-alt"></span>
                    <span>License Active: <strong><?php echo esc_html(ucfirst($license_tier)); ?> Tier</strong></span>
                </div>
                <div class="addon-quota">
                    <span class="dashicons dashicons-admin-plugins"></span>
                    <span>Add-ons: <strong><?php echo esc_html(count($activated_addons)); ?> / <?php echo esc_html($max_addons < 0 ? 'Unlimited' : $max_addons); ?></strong></span>
                </div>
            <?php else: ?>
                <div class="license-badge inactive">
                    <span class="dashicons dashicons-warning"></span>
                    <span>No Active License</span>
                </div>
                <a href="<?php echo esc_url(admin_url('admin.php?page=explorexr-license')); ?>" class="button button-primary">
                    Activate License
                </a>
            <?php endif; ?>
        </div>
        
        <?php if ($license_status !== 'active'): ?>
            <div class="notice notice-warning">
                <p>
                    <strong>License Required:</strong> You need an active license to use add-ons. 
                    <a href="<?php echo esc_url(admin_url('admin.php?page=explorexr-license')); ?>">Activate your license</a> 
                    or <a href="https://expoxr.com/explorexr/pricing/" target="_blank">purchase a license</a> to get started.
                </p>
            </div>
        <?php endif; ?>
        
        <!-- Add-ons Selection Section -->
        <div class="explorexr-premium-addons-section">
            <h2><span class="dashicons dashicons-admin-plugins"></span> Available Add-ons</h2>
            
            <div class="explorexr-premium-addons-grid">
                <?php foreach ($addons as $addon_slug => $addon_data): 
                    $is_activated = in_array($addon_slug, $activated_addons);
                    $addon_name = isset($addon_data['name']) ? $addon_data['name'] : ucfirst($addon_slug);
                    $addon_description = isset($addon_data['description']) ? $addon_data['description'] : '';
                    $addon_icon = isset($addon_data['icon']) ? $addon_data['icon'] : 'dashicons-admin-plugins';
                    
                    // Check if addon plugin is installed
                    $addon_path = isset($addon_data['path']) ? $addon_data['path'] : '';
                    $addon_installed = !empty($addon_path) && file_exists(WP_PLUGIN_DIR . '/' . $addon_path);
                    $addon_active = $addon_installed && is_plugin_active($addon_path);
                ?>
                    <div class="explorexr-premium-addon-card <?php echo $is_activated ? 'activated' : ''; ?>" data-addon="<?php echo esc_attr($addon_slug); ?>">
                        <div class="addon-info">
                            <h3>
                                <span class="dashicons <?php echo esc_attr($addon_icon); ?>"></span>
                                <?php echo esc_html($addon_name); ?>
                            </h3>
                            <p><?php echo esc_html($addon_description); ?></p>
                            
                            <div class="addon-status">
                                <?php if (!$addon_installed): ?>
                                    <span class="status-text">Not Installed</span>
                                <?php elseif (!$addon_active): ?>
                                    <span class="status-text">Installed</span>
                                <?php elseif ($is_activated): ?>
                                    <span class="status-text">Active & Licensed</span>
                                <?php else: ?>
                                    <span class="status-text">Active (Unlicensed)</span>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <div class="addon-actions">
                            <?php if ($license_status === 'active'): ?>
                                <?php if ($addon_active): ?>
                                    <!-- Settings button for active addons -->
                                    <?php
                                    // Check if addon has settings page
                                    $settings_page = "explorexr-premium-{$addon_slug}-settings";
                                    $has_settings_page = false;
                                    
                                    // Check various possible settings page slugs
                                    $possible_settings_pages = array(
                                        "explorexr-premium-{$addon_slug}-settings",
                                        "explorexr-{$addon_slug}-settings",
                                        "explorexr-premium-{$addon_slug}",
                                    );
                                    
                                    foreach ($possible_settings_pages as $page_slug) {
                                        if (get_plugin_page_hook($page_slug, '')) {
                                            $settings_page = $page_slug;
                                            $has_settings_page = true;
                                            break;
                                        }
                                    }
                                    
                                    if ($has_settings_page): ?>
                                        <a href="<?php echo esc_url(admin_url('admin.php?page=' . $settings_page)); ?>" 
                                           class="button button-primary">
                                            <span class="dashicons dashicons-admin-settings"></span> Settings
                                        </a>
                                    <?php endif; ?>
                                    <!-- Don't show button if no settings page -->
                                <?php else: ?>
                                    <!-- Enable Plugin button for installed but inactive addons -->
                                    <?php if ($addon_installed): ?>
                                        <a href="<?php echo esc_url(wp_nonce_url(admin_url('plugins.php?action=activate&plugin=' . urlencode($addon_path)), 'activate-plugin_' . $addon_path)); ?>" 
                                           class="button button-primary">
                                            <span class="dashicons dashicons-yes"></span> Enable Plugin
                                        </a>
                                    <?php else: ?>
                                        <!-- Link to download addon from expoxr.com -->
                                        <a href="https://expoxr.com/explorexr/premium/" 
                                           target="_blank" 
                                           class="button button-secondary">
                                            <span class="dashicons dashicons-download"></span> Download Add-on
                                        </a>
                                    <?php endif; ?>
                                <?php endif; ?>
                            <?php else: ?>
                                <a href="https://expoxr.com/explorexr/premium/" 
                                   target="_blank" 
                                   class="button">
                                    <span class="dashicons dashicons-cart"></span> Get License
                                </a>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        

        
        <?php include EXPLOREXR_PLUGIN_DIR . 'admin/templates/admin-footer.php'; ?>
    </div>
    <?php
}
