<?php
/**
 * Switchboard Settings Renderer
 * 
 * Handles rendering of module settings UI by loading category-specific files.
 * This keeps the main switchboard.php file clean and makes it easy to add new modules.
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class Switchboard_Settings_Renderer {
    
    /**
     * Get settings HTML for a module
     * 
     * @param string $module Module slug
     * @param array $settings Module settings
     * @param array $module_info Module information (category, etc.)
     * @return string HTML for settings panel
     */
    public static function render($module, $settings, $module_info = []) {
        // Check if module has render_settings() method
        $module_instance = self::get_module_instance($module, $module_info);
        if ($module_instance && method_exists($module_instance, 'render_settings')) {
            ob_start();
            $module_instance->render_settings($settings);
            $html = ob_get_clean();

            if (!empty($html)) {
                return $html;
            }
        }

        // No settings available
        return '<p class="description">This module has no configurable settings. Just toggle it on or off!</p>';
    }
    
    /**
     * Get module instance by loading module file
     * 
     * @param string $module Module slug
     * @param array $module_info Module information
     * @return object|null Module instance or null
     */
    private static function get_module_instance($module, $module_info) {
        // Determine category
        $category = isset($module_info['category']) ? strtolower($module_info['category']) : '';
        
        // Map category names to directory names
        $category_map = [
            'admin' => 'admin',
            'content' => 'content',
            'media' => 'media',
            'optimization' => 'optimization',
            'security' => 'security',
            'user management' => 'user-management',
            'utilities' => 'utilities'
        ];
        
        $dir_category = isset($category_map[$category]) ? $category_map[$category] : 'utilities';
        $module_file = SWITCHBOARD_PATH . "modules/{$dir_category}/{$module}.php";
        
        // Check if module file exists
        if (!file_exists($module_file)) {
            return null;
        }
        
        // Get expected class name
        // Convert module slug to class name: breadcrumbs -> SwitchboardBreadcrumbs
        $class_name = 'Switchboard' . str_replace(' ', '', ucwords(str_replace('-', ' ', $module)));
        
        // Check if class already exists (module might be loaded)
        if (class_exists($class_name)) {
            // Try to get existing instance
            // Some modules store instance globally
            global ${$class_name};
            if (isset(${$class_name}) && is_object(${$class_name})) {
                return ${$class_name};
            }
            
            // Try to create new instance
            try {
                return new $class_name();
            } catch (Exception $e) {
                return null;
            }
        }
        
        // Load the module file temporarily
        require_once $module_file;
        
        // Check if class exists after loading
        if (!class_exists($class_name)) {
            return null;
        }
        
        // Create and return instance
        try {
            return new $class_name();
        } catch (Exception $e) {
            return null;
        }
    }
}
