/**
 * 移动端主题切换模块
 * 支持深色/浅色主题切换
 */

(function() {
    'use strict';

    var ThemeManager = {
        currentTheme: 'dark',

        /**
         * 初始化主题
         */
        init: function() {
            if (window.MobileStorage) {
                var saved = window.MobileStorage.get('theme', 'dark');
                this.currentTheme = saved;
            } else {
                try {
                    var stored = localStorage.getItem('fluentos_mobile');
                    if (stored) {
                        var data = JSON.parse(stored);
                        if (data.theme) {
                            this.currentTheme = data.theme;
                        }
                    }
                } catch (e) {
                    // 忽略
                }
            }
            this.applyTheme(this.currentTheme);
            console.log('[Theme] 初始化完成，当前主题:', this.currentTheme);
            return this;
        },

        /**
         * 应用主题
         */
        applyTheme: function(theme) {
            var html = document.documentElement;
            html.classList.remove('theme-dark', 'theme-light');
            html.classList.add('theme-' + theme);
            this.currentTheme = theme;

            if (window.MobileStorage) {
                window.MobileStorage.set('theme', theme);
            } else {
                try {
                    var stored = localStorage.getItem('fluentos_mobile');
                    var data = stored ? JSON.parse(stored) : {};
                    data.theme = theme;
                    localStorage.setItem('fluentos_mobile', JSON.stringify(data));
                } catch (e) {
                    // 忽略
                }
            }

            var event = new CustomEvent('theme-changed', {
                detail: { theme: theme }
            });
            document.dispatchEvent(event);

            return this;
        },

        /**
         * 切换主题
         */
        toggle: function() {
            var newTheme = this.currentTheme === 'dark' ? 'light' : 'dark';
            this.applyTheme(newTheme);
            return newTheme;
        },

        /**
         * 设置深色主题
         */
        setDark: function() {
            return this.applyTheme('dark');
        },

        /**
         * 设置浅色主题
         */
        setLight: function() {
            return this.applyTheme('light');
        },

        /**
         * 获取当前主题
         */
        getTheme: function() {
            return this.currentTheme;
        },

        /**
         * 是否为深色主题
         */
        isDark: function() {
            return this.currentTheme === 'dark';
        },

        /**
         * 是否为浅色主题
         */
        isLight: function() {
            return this.currentTheme === 'light';
        }
    };

    window.ThemeManager = ThemeManager;

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            ThemeManager.init();
        });
    } else {
        ThemeManager.init();
    }

    console.log('[Theme] 模块已加载');

})();