<?php
/**
 * 主题管理类
 * 
 * 支持两种模式:
 * 1. 默认模式: 使用 user/ 目录下的现有前端文件
 * 2. 主题模式: 使用 user/themes/{主题名}/ 目录下的主题文件
 */
class ThemeManager {
    private static $instance = null;
    private $theme = 'default';
    private $themePath = '';
    private $themeUrl = '';
    private $isDefault = true;
    
    private function __construct() {
        global $conf;
        $this->theme = isset($conf['theme']) && !empty($conf['theme']) ? $conf['theme'] : 'default';
        $this->isDefault = ($this->theme === 'default');
        
        if ($this->isDefault) {
            $this->themePath = __DIR__ . '/';
            $this->themeUrl = '';
        } else {
            $this->themePath = __DIR__ . '/themes/' . $this->theme . '/';
            $this->themeUrl = 'themes/' . $this->theme . '/';
        }
    }
    
    public static function getInstance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * 获取当前主题名称
     */
    public function getTheme() {
        return $this->theme;
    }
    
    /**
     * 是否为默认主题
     */
    public function isDefault() {
        return $this->isDefault;
    }
    
    /**
     * 获取主题路径
     */
    public function getPath($file = '') {
        return $this->themePath . $file;
    }
    
    /**
     * 获取主题URL
     */
    public function getUrl($file = '') {
        return $this->themeUrl . $file;
    }
    
    /**
     * 获取主题CSS文件URL
     */
    public function getCss($file = 'style.css') {
        if ($this->isDefault) {
            return 'css/' . $file;
        }
        return $this->getUrl('css/' . $file);
    }
    
    /**
     * 获取主题JS文件URL
     */
    public function getJs($file = 'main.js') {
        if ($this->isDefault) {
            return 'js/' . $file;
        }
        return $this->getUrl('js/' . $file);
    }
    
    /**
     * 获取主题页面文件路径
     */
    public function getPage($page) {
        if ($this->isDefault) {
            return __DIR__ . '/' . $page . '.php';
        }
        
        $pageFile = $this->getPath('pages/' . $page . '.php');
        if (file_exists($pageFile)) {
            return $pageFile;
        }
        
        return __DIR__ . '/' . $page . '.php';
    }
    
    /**
     * 加载主题页面
     */
    public function loadPage($page, $data = array()) {
        $pageFile = $this->getPage($page);
        if (file_exists($pageFile)) {
            extract($data);
            include $pageFile;
        } else {
            echo '<div class="alert alert-warning">页面不存在</div>';
        }
    }
    
    /**
     * 获取CSS链接标签
     */
    public function cssLink($file = 'style.css', $version = '') {
        $url = $this->getCss($file);
        $v = $version ? '?v=' . $version : '';
        return '<link rel="stylesheet" href="' . $url . $v . '">';
    }
    
    /**
     * 获取JS脚本标签
     */
    public function jsScript($file = 'main.js', $version = '') {
        $url = $this->getJs($file);
        $v = $version ? '?v=' . $version : '';
        return '<script src="' . $url . $v . '"></script>';
    }
    
    /**
     * 获取所有可用主题
     */
    public static function getAllThemes() {
        $themes = array();
        
        $themesDir = __DIR__ . '/themes/';
        if (is_dir($themesDir)) {
            $dirs = scandir($themesDir);
            foreach ($dirs as $dir) {
                if ($dir != '.' && $dir != '..' && is_dir($themesDir . $dir)) {
                    $configFile = $themesDir . $dir . '/config.json';
                    $config = array(
                        'id' => $dir,
                        'name' => $dir,
                        'version' => '1.0',
                        'author' => '未知',
                        'description' => '',
                        'preview' => '',
                        'is_default' => ($dir === 'default')
                    );
                    
                    if (file_exists($configFile)) {
                        $json = file_get_contents($configFile);
                        $themeConfig = json_decode($json, true);
                        if ($themeConfig) {
                            $config = array_merge($config, $themeConfig);
                        }
                    }
                    
                    $themes[] = $config;
                }
            }
        }
        
        return $themes;
    }
    
    /**
     * 检查主题是否存在
     */
    public static function themeExists($theme) {
        if ($theme === 'default') {
            return true;
        }
        return is_dir(__DIR__ . '/themes/' . $theme);
    }
}

/**
 * 获取主题管理器实例
 */
function getTheme() {
    return ThemeManager::getInstance();
}
