// 视频设置模块
class VideoSettings {
    constructor() {
        this.qualityPriority = 'high'; // 'high' 或 'low'
    }

    // 初始化
    initialize() {
        // 从localStorage恢复设置
        this.qualityPriority = localStorage.getItem('videoQualityPriority') || 'high';

        // 创建视频设置UI
        this.createUI();

        // 更新UI状态
        this.updateUI();

        console.log('视频设置模块初始化完成');
    }

    // 创建UI
    createUI() {
        const videoContent = document.getElementById('video-content');
        if (!videoContent) return;

        videoContent.innerHTML = `
            <div style="margin-bottom: 15px;">
                <div style="margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between;">
                    <span style="color: white;">视频清晰度优先级</span>
                    <button class="mode-btn" id="qualityPriorityToggle" onclick="window.videoSettings.toggleQualityPriority()" style="min-width: 100px;">
                        ${this.qualityPriority === 'high' ? '高清优先' : '流畅优先'}
                    </button>
                </div>
                <div style="font-size: 12px; color: rgba(255, 255, 255, 0.6);">
                    <div>高清优先: 4K/1080p → 720p</div>
                    <div>流畅优先: 720p → 4K/1080p</div>
                </div>
            </div>
        `;
    }

    // 更新UI状态
    updateUI() {
        const qualityToggle = document.getElementById('qualityPriorityToggle');
        if (!qualityToggle) return;

        if (this.qualityPriority === 'high') {
            qualityToggle.style.background = 'linear-gradient(135deg, #00b09b, #96c93d)';
            qualityToggle.style.color = 'white';
            qualityToggle.style.borderColor = 'transparent';
            qualityToggle.textContent = '高清优先';
        } else {
            qualityToggle.style.background = 'rgba(40, 40, 40, 0.8)';
            qualityToggle.style.color = 'rgba(255, 255, 255, 0.7)';
            qualityToggle.style.borderColor = 'rgba(255, 255, 255, 0.2)';
            qualityToggle.textContent = '流畅优先';
        }
    }

    // 切换清晰度优先级
    toggleQualityPriority() {
        this.qualityPriority = this.qualityPriority === 'high' ? 'low' : 'high';
        localStorage.setItem('videoQualityPriority', this.qualityPriority);
        this.updateUI();
    }

    // 获取视频源优先级
    getVideoSourcePriority(sources) {
        if (!sources || !Array.isArray(sources)) return sources;

        if (this.qualityPriority === 'high') {
            // 高清优先：4K > 1080p > 720p
            return sources.sort((a, b) => {
                const qualityOrder = { '4K': 3, '1080p': 2, '720p': 1 };
                const aQuality = qualityOrder[a.quality] || 0;
                const bQuality = qualityOrder[b.quality] || 0;
                return bQuality - aQuality; // 降序排列
            });
        } else {
            // 流畅优先：720p > 1080p > 4K
            return sources.sort((a, b) => {
                const qualityOrder = { '4K': 3, '1080p': 2, '720p': 1 };
                const aQuality = qualityOrder[a.quality] || 0;
                const bQuality = qualityOrder[b.quality] || 0;
                return aQuality - bQuality; // 升序排列
            });
        }
    }
}
