/**
 * 移动端应用管理模块
 * 管理所有内置应用的启动、关闭、切换
 */

(function() {
    'use strict';

    var AppsManager = {
        _apps: [],
        _openApps: {},
        _elements: {},
        _currentApp: null,

        _appList: [
            { id: 'files', name: '文件管理', icon: 'files', color: '#4a9eff' },
            { id: 'settings', name: '设置', icon: 'settings', color: '#6c5ce7' },
            { id: 'browser', name: '浏览器', icon: 'browser', color: '#00b894' },
            { id: 'calculator', name: '计算器', icon: 'calculator', color: '#fdcb6e' },
            { id: 'notes', name: '便签', icon: 'notes', color: '#e17055' },
            { id: 'clock', name: '时钟', icon: 'clock', color: '#00cec9' },
            { id: 'weather', name: '天气', icon: 'weather', color: '#fdcb6e' },
            { id: 'camera', name: '相机', icon: 'camera', color: '#2d3436' },
            { id: 'photos', name: '照片', icon: 'photos', color: '#6c5ce7' },
            { id: 'media', name: '媒体', icon: 'media', color: '#e17055' },
            { id: 'terminal', name: '终端', icon: 'terminal', color: '#00b894' },
            { id: 'fingo', name: 'Fingo助手', icon: 'fingo', color: '#4a9eff' },
            { id: 'shop', name: '应用商店', icon: 'shop', color: '#fdcb6e' },
            { id: 'taskmanager', name: '进程管理', icon: 'taskmanager', color: '#00cec9' },
            { id: 'tips', name: '小贴士', icon: 'tips', color: '#e17055' }
        ],

        init: function() {
            this._apps = this._appList;
            this._elements.desktop = document.getElementById('desktopIcons');
            this._elements.start = document.getElementById('startApps');
            this._elements.search = document.getElementById('startSearch');
            this._elements.taskbar = document.getElementById('taskbarApps');
            this._elements.container = document.getElementById('app-container');
            this._elements.startMenu = document.getElementById('startMenu');
            this._elements.startBtn = document.getElementById('startBtn');

            this.bindEvents();
            this.renderDesktop();
            this.renderStartMenu();

            if (window.MobileStorage) {
                var saved = window.MobileStorage.get('openApps', []);
                var self = this;
                saved.forEach(function(appId) {
                    self.openApp(appId, true);
                });
            }

            console.log('[Apps] 初始化完成，应用数:', this._apps.length);
        },

        bindEvents: function() {
            var self = this;

            if (this._elements.startBtn) {
                this._elements.startBtn.addEventListener('click', function(e) {
                    e.stopPropagation();
                    self.toggleStartMenu();
                });
            }

            if (this._elements.search) {
                this._elements.search.addEventListener('input', function() {
                    self.renderStartMenu(this.value);
                });
            }

            document.addEventListener('click', function(e) {
                if (self._elements.startMenu &&
                    self._elements.startMenu.classList.contains('active')) {
                    if (!self._elements.startMenu.contains(e.target) &&
                        e.target !== self._elements.startBtn &&
                        !self._elements.startBtn.contains(e.target)) {
                        self._elements.startMenu.classList.remove('active');
                    }
                }
            });

            document.addEventListener('window-closed', function(e) {
                var appId = e.detail && e.detail.appId;
                if (appId) {
                    self.closeApp(appId);
                }
            });
        },

        renderDesktop: function() {
            if (!this._elements.desktop) return;

            var html = '';
            var self = this;
            this._apps.forEach(function(app) {
                html += `
                    <div class="desktop-icon" data-app="${app.id}">
                        <div class="icon-wrapper">
                            <img src="Theme/Icon/${app.icon}-color.svg" width="40" height="40" alt="${app.name}" onerror="this.style.display='none'">
                        </div>
                        <span class="app-name">${app.name}</span>
                    </div>
                `;
            });

            this._elements.desktop.innerHTML = html;

            var icons = this._elements.desktop.querySelectorAll('.desktop-icon');
            icons.forEach(function(icon) {
                icon.addEventListener('click', function() {
                    var appId = this.dataset.app;
                    if (appId) {
                        self.openApp(appId);
                    }
                });
            });
        },

        renderStartMenu: function(filter) {
            if (!this._elements.start) return;

            var list = this._apps;
            if (filter) {
                var f = filter.toLowerCase();
                list = this._apps.filter(function(app) {
                    return app.name.indexOf(f) !== -1 || app.id.indexOf(f) !== -1;
                });
            }

            var html = '';
            var self = this;
            list.forEach(function(app) {
                html += `
                    <div class="start-app-item" data-app="${app.id}">
                        <div class="app-icon">
                            <img src="Theme/Icon/${app.icon}-color.svg" width="32" height="32" alt="${app.name}" onerror="this.style.display='none'">
                        </div>
                        <span class="app-name">${app.name}</span>
                    </div>
                `;
            });

            this._elements.start.innerHTML = html;

            var items = this._elements.start.querySelectorAll('.start-app-item');
            items.forEach(function(item) {
                item.addEventListener('click', function() {
                    var appId = this.dataset.app;
                    if (appId) {
                        self._elements.startMenu.classList.remove('active');
                        self.openApp(appId);
                    }
                });
            });
        },

        toggleStartMenu: function() {
            if (this._elements.startMenu) {
                this._elements.startMenu.classList.toggle('active');
                if (this._elements.startMenu.classList.contains('active')) {
                    this.renderStartMenu();
                    if (this._elements.search) {
                        this._elements.search.value = '';
                        var self = this;
                        setTimeout(function() {
                            if (self._elements.search) {
                                self._elements.search.focus();
                            }
                        }, 100);
                    }
                }
            }
        },

        openApp: function(appId, silent) {
            var app = this._apps.find(function(a) { return a.id === appId; });
            if (!app) {
                console.warn('[Apps] 应用不存在:', appId);
                return;
            }

            if (this._openApps[appId]) {
                this.switchToApp(appId);
                return;
            }

            var win = this.createWindow(app);
            if (!win) return;

            this._openApps[appId] = {
                id: appId,
                name: app.name,
                element: win,
                timestamp: Date.now()
            };

            this.addToTaskbar(app);

            if (window.MobileStorage) {
                var openList = Object.keys(this._openApps);
                window.MobileStorage.set('openApps', openList);
            }

            var event = new CustomEvent('app-opened', {
                detail: { appId: appId, app: app }
            });
            document.dispatchEvent(event);

            if (!silent) {
                this.showToast('打开 ' + app.name);
            }

            console.log('[Apps] 打开应用:', appId);
        },

        createWindow: function(app) {
            if (!this._elements.container) return null;

            var existing = this._elements.container.querySelector(
                '.window-container[data-app="' + app.id + '"]'
            );
            if (existing) {
                existing.remove();
            }

            var win = document.createElement('div');
            win.className = 'window-container active';
            win.dataset.app = app.id;

            var iconHTML = `
                <img src="Theme/Icon/${app.icon}-color.svg" width="20" height="20" alt="" onerror="this.style.display='none'">
            `;

            var bodyContent = this.getAppContent(app.id);

            win.innerHTML = `
                <div class="window-title-bar">
                    <span class="window-title">
                        <span class="title-icon">${iconHTML}</span>
                        ${app.name}
                    </span>
                    <div class="window-controls">
                        <button class="window-close-btn" data-app="${app.id}">X</button>
                    </div>
                </div>
                <div class="window-body">
                    ${bodyContent}
                </div>
            `;

            var closeBtn = win.querySelector('.window-close-btn');
            if (closeBtn) {
                closeBtn.addEventListener('click', function() {
                    var id = this.dataset.app;
                    AppsManager.closeApp(id);
                });
            }

            this._elements.container.appendChild(win);
            this._currentApp = app.id;

            if (window.MobileWindowAdapter &&
                window.MobileWindowAdapter.adaptWindow) {
                window.MobileWindowAdapter.adaptWindow(app.id);
            }

            return win;
        },

        getAppContent: function(appId) {
            var contents = {
                'files': this._getFilesContent(),
                'settings': this._getSettingsContent(),
                'calculator': this._getCalculatorContent(),
                'notes': this._getNotesContent(),
                'clock': this._getClockContent(),
                'weather': this._getWeatherContent(),
                'terminal': this._getTerminalContent(),
                'camera': this._getCameraContent(),
                'photos': this._getPhotosContent(),
                'media': this._getMediaContent(),
                'fingo': this._getFingoContent(),
                'shop': this._getShopContent(),
                'taskmanager': this._getTaskManagerContent(),
                'tips': this._getTipsContent(),
                'browser': this._getBrowserContent()
            };

            return contents[appId] || this._getDefaultContent(appId);
        },

        _getDefaultContent: function(appId) {
            var app = this._apps.find(function(a) { return a.id === appId; });
            var name = app ? app.name : appId;
            return `
                <div style="padding:20px;text-align:center;color:rgba(255,255,255,0.6);">
                    <div style="font-size:48px;margin-bottom:16px;line-height:1;">[APP]</div>
                    <p>${name}</p>
                    <p style="font-size:12px;margin-top:8px;color:rgba(255,255,255,0.3);">移动端应用</p>
                    <p style="font-size:12px;margin-top:4px;color:rgba(255,255,255,0.2);">点击右上角 X 关闭</p>
                </div>
            `;
        },

        _getFilesContent: function() {
            return `
                <div style="padding:16px;color:rgba(255,255,255,0.6);">
                    <div style="display:flex;gap:8px;margin-bottom:12px;flex-wrap:wrap;">
                        <button class="app-btn" style="padding:6px 14px;border-radius:6px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:13px;">新建文件夹</button>
                        <button class="app-btn" style="padding:6px 14px;border-radius:6px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:13px;">上传</button>
                    </div>
                    <div style="border-top:1px solid rgba(255,255,255,0.04);padding-top:12px;">
                        <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid rgba(255,255,255,0.03);">
                            <span>[FILE] 文档.txt</span>
                            <span style="font-size:12px;color:rgba(255,255,255,0.3);">12KB</span>
                        </div>
                        <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid rgba(255,255,255,0.03);">
                            <span>[FOLDER] 照片</span>
                            <span style="font-size:12px;color:rgba(255,255,255,0.3);">文件夹</span>
                        </div>
                        <div style="display:flex;justify-content:space-between;padding:8px 0;">
                            <span>[FOLDER] 音乐</span>
                            <span style="font-size:12px;color:rgba(255,255,255,0.3);">文件夹</span>
                        </div>
                    </div>
                </div>
            `;
        },

        _getSettingsContent: function() {
            return `
                <div style="padding:16px;">
                    <div style="display:flex;align-items:center;gap:12px;padding:12px 0;border-bottom:1px solid rgba(255,255,255,0.04);">
                        <span style="font-size:20px;">[T]</span>
                        <div style="flex:1;">
                            <div style="font-size:14px;color:rgba(255,255,255,0.9);">深色主题</div>
                            <div style="font-size:12px;color:rgba(255,255,255,0.3);">切换深色/浅色模式</div>
                        </div>
                        <button class="theme-toggle-btn" style="width:44px;height:26px;border-radius:13px;border:none;background:rgba(74,158,255,0.3);cursor:pointer;position:relative;">
                            <span style="position:absolute;top:2px;left:2px;width:22px;height:22px;border-radius:50%;background:white;transition:all 0.3s;"></span>
                        </button>
                    </div>
                    <div style="display:flex;align-items:center;gap:12px;padding:12px 0;border-bottom:1px solid rgba(255,255,255,0.04);">
                        <span style="font-size:20px;">[K]</span>
                        <div style="flex:1;">
                            <div style="font-size:14px;color:rgba(255,255,255,0.9);">修改PIN码</div>
                            <div style="font-size:12px;color:rgba(255,255,255,0.3);">当前: 1234</div>
                        </div>
                        <span style="color:rgba(255,255,255,0.2);">></span>
                    </div>
                    <div style="display:flex;align-items:center;gap:12px;padding:12px 0;">
                        <span style="font-size:20px;">[I]</span>
                        <div style="flex:1;">
                            <div style="font-size:14px;color:rgba(255,255,255,0.9);">关于</div>
                            <div style="font-size:12px;color:rgba(255,255,255,0.3);">FluentOS 移动版 1.0</div>
                        </div>
                        <span style="color:rgba(255,255,255,0.2);">></span>
                    </div>
                </div>
            `;
        },

        _getCalculatorContent: function() {
            return `
                <div style="padding:16px;height:100%;display:flex;flex-direction:column;">
                    <div style="flex:1;display:flex;align-items:flex-end;justify-content:flex-end;padding:16px;font-size:40px;font-weight:200;color:rgba(255,255,255,0.9);">0</div>
                    <div style="display:grid;grid-template-columns:repeat(4,1fr);gap:8px;">
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">C</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">+/-</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">%</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(74,158,255,0.3);color:#4a9eff;font-size:20px;">/</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">7</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">8</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">9</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(74,158,255,0.3);color:#4a9eff;font-size:20px;">*</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">4</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">5</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">6</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(74,158,255,0.3);color:#4a9eff;font-size:20px;">-</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">1</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">2</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">3</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(74,158,255,0.3);color:#4a9eff;font-size:20px;">+</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;grid-column:span 2;">0</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:20px;">.</button>
                        <button style="padding:16px;border-radius:10px;border:none;background:rgba(74,158,255,0.4);color:#4a9eff;font-size:20px;font-weight:bold;">=</button>
                    </div>
                </div>
            `;
        },

        _getNotesContent: function() {
            return `
                <div style="padding:16px;height:100%;display:flex;flex-direction:column;">
                    <div style="display:flex;gap:8px;margin-bottom:12px;flex-wrap:wrap;">
                        <button style="padding:6px 14px;border-radius:6px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:13px;">新建</button>
                        <button style="padding:6px 14px;border-radius:6px;border:none;background:rgba(255,255,255,0.06);color:rgba(255,255,255,0.8);font-size:13px;">保存</button>
                    </div>
                    <input type="text" placeholder="标题" style="width:100%;padding:8px 12px;border-radius:8px;border:1px solid rgba(255,255,255,0.06);background:rgba(255,255,255,0.03);color:rgba(255,255,255,0.9);font-size:16px;margin-bottom:8px;outline:none;">
                    <textarea placeholder="写点什么..." style="flex:1;width:100%;padding:8px 12px;border-radius:8px;border:1px solid rgba(255,255,255,0.06);background:rgba(255,255,255,0.03);color:rgba(255,255,255,0.8);font-size:14px;resize:none;outline:none;min-height:120px;">欢迎使用便签</textarea>
                </div>
            `;
        },

        _getClockContent: function() {
            return `
                <div style="padding:16px;text-align:center;">
                    <div style="font-size:48px;font-weight:200;font-variant-numeric:tabular-nums;color:rgba(255,255,255,0.9);">12:00</div>
                    <div style="font-size:16px;color:rgba(255,255,255,0.3);">2024年01月01日 周一</div>
                    <div style="display:flex;gap:8px;margin-top:16px;overflow-x:auto;padding:4px 0;">
                        <button style="padding:8px 16px;border-radius:8px;border:none;background:rgba(74,158,255,0.2);color:#4a9eff;font-size:13px;white-space:nowrap;">[A] 闹钟</button>
                        <button style="padding:8px 16px;border-radius:8px;border:none;background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.6);font-size:13px;white-space:nowrap;">[T] 计时器</button>
                        <button style="padding:8px 16px;border-radius:8px;border:none;background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.6);font-size:13px;white-space:nowrap;">[S] 秒表</button>
                    </div>
                </div>
            `;
        },

        _getWeatherContent: function() {
            return `
                <div style="padding:16px;text-align:center;">
                    <div style="font-size:56px;">[S]</div>
                    <div style="font-size:40px;font-weight:200;color:rgba(255,255,255,0.9);">28 C</div>
                    <div style="font-size:16px;color:rgba(255,255,255,0.5);">晴</div>
                    <div style="display:grid;grid-template-columns:repeat(5,1fr);gap:8px;margin-top:16px;padding-top:16px;border-top:1px solid rgba(255,255,255,0.04);">
                        <div><div style="font-size:12px;color:rgba(255,255,255,0.3);">周一</div><div style="font-size:20px;">[S]</div><div style="font-size:12px;color:rgba(255,255,255,0.5);">28</div></div>
                        <div><div style="font-size:12px;color:rgba(255,255,255,0.3);">周二</div><div style="font-size:20px;">[C]</div><div style="font-size:12px;color:rgba(255,255,255,0.5);">26</div></div>
                        <div><div style="font-size:12px;color:rgba(255,255,255,0.3);">周三</div><div style="font-size:20px;">[R]</div><div style="font-size:12px;color:rgba(255,255,255,0.5);">22</div></div>
                        <div><div style="font-size:12px;color:rgba(255,255,255,0.3);">周四</div><div style="font-size:20px;">[S]</div><div style="font-size:12px;color:rgba(255,255,255,0.5);">30</div></div>
                        <div><div style="font-size:12px;color:rgba(255,255,255,0.3);">周五</div><div style="font-size:20px;">[S]</div><div style="font-size:12px;color:rgba(255,255,255,0.5);">32</div></div>
                    </div>
                </div>
            `;
        },

        _getTerminalContent: function() {
            return `
                <div style="padding:12px;background:#0d0d1a;font-family:'Courier New',monospace;font-size:13px;height:100%;overflow-y:auto;">
                    <div style="color:#4a9eff;">$ welcome to FluentOS Terminal</div>
                    <div style="color:rgba(255,255,255,0.5);">Type 'help' for commands</div>
                    <div style="color:rgba(255,255,255,0.8);margin-top:8px;">$ help</div>
                    <div style="color:rgba(255,255,255,0.3);margin-left:4px;">  apps   - 列出所有应用</div>
                    <div style="color:rgba(255,255,255,0.3);margin-left:4px;">  clear  - 清空屏幕</div>
                    <div style="color:rgba(255,255,255,0.3);margin-left:4px;">  date   - 显示日期</div>
                    <div style="color:rgba(255,255,255,0.3);margin-left:4px;">  theme  - 切换主题</div>
                    <div style="color:rgba(255,255,255,0.8);margin-top:8px;">$ <span style="background:rgba(255,255,255,0.05);padding:0 4px;">_</span></div>
                </div>
            `;
        },

        _getCameraContent: function() {
            return `
                <div style="padding:20px;text-align:center;display:flex;flex-direction:column;align-items:center;justify-content:center;height:100%;color:rgba(255,255,255,0.6);">
                    <div style="font-size:64px;margin-bottom:16px;line-height:1;">[C]</div>
                    <p>相机</p>
                    <p style="font-size:12px;color:rgba(255,255,255,0.3);">需要浏览器权限</p>
                    <button style="margin-top:12px;padding:10px 24px;border-radius:20px;border:none;background:#4a9eff;color:white;font-size:14px;cursor:pointer;">打开相机</button>
                </div>
            `;
        },

        _getPhotosContent: function() {
            return `
                <div style="padding:16px;">
                    <div style="display:grid;grid-template-columns:repeat(3,1fr);gap:4px;">
                        <div style="aspect-ratio:1;background:rgba(255,255,255,0.04);border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:24px;color:rgba(255,255,255,0.2);">[P]</div>
                        <div style="aspect-ratio:1;background:rgba(255,255,255,0.04);border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:24px;color:rgba(255,255,255,0.2);">[P]</div>
                        <div style="aspect-ratio:1;background:rgba(255,255,255,0.04);border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:24px;color:rgba(255,255,255,0.2);">[P]</div>
                        <div style="aspect-ratio:1;background:rgba(255,255,255,0.04);border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:24px;color:rgba(255,255,255,0.2);">[P]</div>
                        <div style="aspect-ratio:1;background:rgba(255,255,255,0.04);border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:24px;color:rgba(255,255,255,0.2);">[P]</div>
                        <div style="aspect-ratio:1;background:rgba(255,255,255,0.04);border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:24px;color:rgba(255,255,255,0.2);">[P]</div>
                    </div>
                </div>
            `;
        },

        _getMediaContent: function() {
            return `
                <div style="padding:16px;text-align:center;">
                    <div style="width:160px;height:160px;border-radius:12px;background:rgba(255,255,255,0.04);margin:0 auto 12px;display:flex;align-items:center;justify-content:center;font-size:48px;color:rgba(255,255,255,0.15);">[M]</div>
                    <div style="font-size:16px;font-weight:500;color:rgba(255,255,255,0.9);">未播放</div>
                    <div style="font-size:13px;color:rgba(255,255,255,0.3);">无歌曲</div>
                    <div style="display:flex;justify-content:center;gap:16px;margin-top:12px;">
                        <button style="width:44px;height:44px;border-radius:50%;border:none;background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.6);font-size:18px;cursor:pointer;">[<]</button>
                        <button style="width:48px;height:48px;border-radius:50%;border:none;background:#4a9eff;color:white;font-size:20px;cursor:pointer;">[>]</button>
                        <button style="width:44px;height:44px;border-radius:50%;border:none;background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.6);font-size:18px;cursor:pointer;">[>]</button>
                    </div>
                </div>
            `;
        },

        _getFingoContent: function() {
            return `
                <div style="padding:20px;text-align:center;display:flex;flex-direction:column;align-items:center;justify-content:center;height:100%;">
                    <div style="font-size:64px;margin-bottom:16px;line-height:1;">[F]</div>
                    <div style="font-size:18px;font-weight:500;color:rgba(255,255,255,0.9);">Fingo 助手</div>
                    <div style="font-size:13px;color:rgba(255,255,255,0.3);margin-bottom:12px;">AI 智能助手</div>
                    <div style="display:flex;gap:8px;width:100%;max-width:300px;">
                        <input type="text" placeholder="输入消息..." style="flex:1;padding:10px 14px;border-radius:20px;border:1px solid rgba(255,255,255,0.06);background:rgba(255,255,255,0.03);color:rgba(255,255,255,0.8);font-size:14px;outline:none;">
                        <button style="padding:10px 18px;border-radius:20px;border:none;background:#4a9eff;color:white;font-size:14px;cursor:pointer;">发送</button>
                    </div>
                </div>
            `;
        },

        _getShopContent: function() {
            return `
                <div style="padding:16px;">
                    <div style="display:grid;grid-template-columns:repeat(3,1fr);gap:8px;">
                        <div style="padding:12px;border-radius:8px;background:rgba(255,255,255,0.04);text-align:center;cursor:pointer;">
                            <div style="font-size:24px;">[A]</div>
                            <div style="font-size:12px;color:rgba(255,255,255,0.6);">应用1</div>
                        </div>
                        <div style="padding:12px;border-radius:8px;background:rgba(255,255,255,0.04);text-align:center;cursor:pointer;">
                            <div style="font-size:24px;">[A]</div>
                            <div style="font-size:12px;color:rgba(255,255,255,0.6);">应用2</div>
                        </div>
                        <div style="padding:12px;border-radius:8px;background:rgba(255,255,255,0.04);text-align:center;cursor:pointer;">
                            <div style="font-size:24px;">[A]</div>
                            <div style="font-size:12px;color:rgba(255,255,255,0.6);">应用3</div>
                        </div>
                    </div>
                </div>
            `;
        },

        _getTaskManagerContent: function() {
            return `
                <div style="padding:16px;">
                    <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid rgba(255,255,255,0.04);">
                        <span style="color:rgba(255,255,255,0.8);">应用名称</span>
                        <span style="color:rgba(255,255,255,0.3);">状态</span>
                    </div>
                    <div id="task-list">
                        <div style="display:flex;justify-content:space-between;padding:8px 0;border-bottom:1px solid rgba(255,255,255,0.03);">
                            <span style="color:rgba(255,255,255,0.7);">系统</span>
                            <span style="color:rgba(100,200,100,0.6);">运行中</span>
                        </div>
                    </div>
                </div>
            `;
        },

        _getTipsContent: function() {
            return `
                <div style="padding:16px;">
                    <div style="padding:12px;border-radius:8px;background:rgba(74,158,255,0.06);border-left:3px solid #4a9eff;margin-bottom:8px;">
                        <div style="font-size:14px;color:rgba(255,255,255,0.9);">FluentOS 移动版</div>
                        <div style="font-size:12px;color:rgba(255,255,255,0.3);">版本 1.0</div>
                    </div>
                    <div style="padding:12px;border-radius:8px;background:rgba(255,255,255,0.02);margin-bottom:8px;">
                        <div style="font-size:13px;color:rgba(255,255,255,0.7);">上滑可以打开应用抽屉</div>
                    </div>
                    <div style="padding:12px;border-radius:8px;background:rgba(255,255,255,0.02);">
                        <div style="font-size:13px;color:rgba(255,255,255,0.7);">长按返回键强制关闭应用</div>
                    </div>
                </div>
            `;
        },

        _getBrowserContent: function() {
            return `
                <div style="display:flex;flex-direction:column;height:100%;">
                    <div style="display:flex;gap:4px;padding:6px 8px;background:rgba(255,255,255,0.02);">
                        <button style="padding:4px 10px;border-radius:4px;border:none;background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.6);font-size:14px;">[<]</button>
                        <button style="padding:4px 10px;border-radius:4px;border:none;background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.6);font-size:14px;">[>]</button>
                        <input type="text" value="https://fluentos.local" style="flex:1;padding:4px 10px;border-radius:4px;border:1px solid rgba(255,255,255,0.04);background:rgba(255,255,255,0.02);color:rgba(255,255,255,0.7);font-size:12px;outline:none;">
                        <button style="padding:4px 10px;border-radius:4px;border:none;background:rgba(255,255,255,0.04);color:rgba(255,255,255,0.6);font-size:14px;">[R]</button>
                    </div>
                    <div style="flex:1;display:flex;align-items:center;justify-content:center;color:rgba(255,255,255,0.2);font-size:14px;">
                        [浏览器页面]
                    </div>
                </div>
            `;
        },

        switchToApp: function(appId) {
            var app = this._openApps[appId];
            if (!app) return;

            var allWindows = this._elements.container.querySelectorAll('.window-container');
            allWindows.forEach(function(win) {
                win.classList.remove('active');
                win.style.display = 'none';
            });

            app.element.style.display = 'flex';
            app.element.classList.add('active');
            this._currentApp = appId;

            if (window.MobileWindowAdapter) {
                window.MobileWindowAdapter.bringToTop(appId);
            }

            console.log('[Apps] 切换到:', appId);
        },

        closeApp: function(appId) {
            var app = this._openApps[appId];
            if (!app) return;

            if (app.element) {
                app.element.remove();
            }

            delete this._openApps[appId];
            this.removeFromTaskbar(appId);

            if (window.MobileStorage) {
                var openList = Object.keys(this._openApps);
                window.MobileStorage.set('openApps', openList);
            }

            var event = new CustomEvent('app-closed', {
                detail: { appId: appId }
            });
            document.dispatchEvent(event);

            console.log('[Apps] 关闭应用:', appId);
        },

        addToTaskbar: function(app) {
            if (!this._elements.taskbar) return;

            // 检查是否已存在
            var existing = this._elements.taskbar.querySelector(
                '.taskbar-app[data-app="' + app.id + '"]'
            );
            if (existing) return;

            var btn = document.createElement('button');
            btn.className = 'taskbar-app active';
            btn.dataset.app = app.id;
            btn.innerHTML = `
                <img src="Theme/Icon/${app.icon}-color.svg" width="20" height="20" alt="" onerror="this.style.display='none'">
            `;

            var self = this;
            btn.addEventListener('click', function() {
                var id = this.dataset.app;
                var win = self._openApps[id];
                if (win) {
                    if (win.element.style.display === 'none') {
                        win.element.style.display = 'flex';
                        win.element.classList.add('active');
                        this.classList.add('active');
                    } else {
                        win.element.style.display = 'none';
                        win.element.classList.remove('active');
                        this.classList.remove('active');
                    }
                }
            });

            this._elements.taskbar.appendChild(btn);
        },

        removeFromTaskbar: function(appId) {
            if (!this._elements.taskbar) return;
            var btn = this._elements.taskbar.querySelector(
                '.taskbar-app[data-app="' + appId + '"]'
            );
            if (btn) {
                btn.remove();
            }
        },

        showToast: function(message) {
            var existing = document.querySelector('.android-toast');
            if (existing) existing.remove();

            var toast = document.createElement('div');
            toast.className = 'android-toast';
            toast.textContent = message;
            document.body.appendChild(toast);

            setTimeout(function() {
                toast.style.opacity = '0';
                toast.style.transform = 'translateX(-50%) translateY(20px)';
                toast.style.transition = 'all 0.3s ease';
                setTimeout(function() {
                    if (toast.parentNode) toast.remove();
                }, 300);
            }, 1500);
        },

        getOpenApps: function() {
            return Object.keys(this._openApps);
        },

        getAppInfo: function(appId) {
            return this._apps.find(function(a) { return a.id === appId; });
        }
    };

    window.AppsManager = AppsManager;

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

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

})();