// 音效设置模块
class SoundSettings {
    constructor() {
        this.soundEnabled = true;
        this.cialloEnabled = true;
        this.volume = 0.8; // 默认80%音量
    }

    // 初始化
    async initialize() {
        // 从localStorage恢复设置
        this.soundEnabled = ConfigManager.getBoolConfig('soundEffectsEnabled', true);
        this.cialloEnabled = ConfigManager.getBoolConfig('cialloEffectEnabled', true);
        this.volume = ConfigManager.getConfig('soundEffectsVolume', 0.8);

        // 加载音效列表
        if (window.soundEffectManager) {
            await window.soundEffectManager.loadSoundEffects();
        }

        // 创建音效设置UI
        this.createUI();

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

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

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

        soundContent.innerHTML = `
            <div style="margin-bottom: 15px;">
                <div style="margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between;">
                    <span>点击音效</span>
                    <button class="mode-btn" id="soundToggle" onclick="window.soundSettings.toggleSoundEffects()" style="min-width: 100px;">
                        已开启
                    </button>
                </div>
                <div id="soundStatus" style="font-size: 12px; color: rgba(255,255,255,0.6); display: none;">
                    检查音效状态中...
                </div>
            </div>

            <div style="margin-bottom: 15px;">
                <div style="margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between;">
                    <span>选择音效</span>
                    <select id="soundEffectSelect" class="settings-select" onchange="window.soundSettings.changeSoundEffect(this.value)">
                        <!-- 音效选项将通过JavaScript动态添加 -->
                    </select>
                </div>
                <div id="soundEffectDescription" style="font-size: 12px; color: rgba(255,255,255,0.6); margin-top: 5px;">
                    <!-- 音效描述将通过JavaScript动态更新 -->
                </div>
            </div>

            <div style="margin-bottom: 15px;">
                <div style="margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between;">
                    <span>文本特效</span>
                    <button class="mode-btn" id="cialloToggle" onclick="window.soundSettings.toggleCialloEffect()" style="min-width: 100px;">
                        已开启
                    </button>
                </div>
                <div id="cialloStatus" style="font-size: 12px; color: rgba(255,255,255,0.6);">
                    点击时显示文本特效
                </div>
            </div>

            <div style="margin-bottom: 15px;">
                <div style="margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between;">
                    <span>选择文本</span>
                    <select id="textEffectSelect" class="settings-select" onchange="window.soundSettings.changeTextEffect(this.value)">
                        <!-- 文本特效选项将通过JavaScript动态添加 -->
                    </select>
                </div>
            </div>

            <div style="margin-bottom: 15px;">
                <div style="margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between;">
                    <span>音效音量</span>
                    <span id="volumeValue" style="color: rgba(255,255,255,0.8);">${Math.round(this.volume * 100)}%</span>
                </div>
                <div style="display: flex; align-items: center; gap: 10px;">
                    <span style="font-size: 12px; color: rgba(255,255,255,0.6);">🔈</span>
                    <input type="range" id="soundVolumeSlider" min="0" max="100" value="${Math.round(this.volume * 100)}"
                           class="volume-slider"
                           oninput="window.soundSettings.updateVolume(this.value)"
                           onchange="window.soundSettings.testVolume()">
                    <span style="font-size: 12px; color: rgba(255,255,255,0.6);">🔊</span>
                </div>
            </div>
        `;
    }

    // 更新UI状态
    updateUI() {
        const soundToggle = document.getElementById('soundToggle');
        const cialloToggle = document.getElementById('cialloToggle');
        const volumeSlider = document.getElementById('soundVolumeSlider');
        const volumeValue = document.getElementById('volumeValue');
        const soundEffectSelect = document.getElementById('soundEffectSelect');
        const soundEffectDescription = document.getElementById('soundEffectDescription');
        const textEffectSelect = document.getElementById('textEffectSelect');

        if (soundToggle) {
            if (this.soundEnabled) {
                soundToggle.style.background = 'linear-gradient(135deg, #00b09b, #96c93d)';
                soundToggle.style.color = 'white';
                soundToggle.style.borderColor = 'transparent';
                soundToggle.textContent = '已开启';
            } else {
                soundToggle.style.background = 'rgba(40, 40, 40, 0.8)';
                soundToggle.style.color = 'rgba(255, 255, 255, 0.7)';
                soundToggle.style.borderColor = 'rgba(255, 255, 255, 0.2)';
                soundToggle.textContent = '已关闭';
            }
        }

        if (cialloToggle) {
            if (this.cialloEnabled) {
                cialloToggle.style.background = 'linear-gradient(135deg, #00b09b, #96c93d)';
                cialloToggle.style.color = 'white';
                cialloToggle.style.borderColor = 'transparent';
                cialloToggle.textContent = '已开启';
            } else {
                cialloToggle.style.background = 'rgba(40, 40, 40, 0.8)';
                cialloToggle.style.color = 'rgba(255, 255, 255, 0.7)';
                cialloToggle.style.borderColor = 'rgba(255, 255, 255, 0.2)';
                cialloToggle.textContent = '已关闭';
            }
        }

        if (volumeSlider) {
            volumeSlider.value = Math.round(this.volume * 100);
        }

        if (volumeValue) {
            volumeValue.textContent = `${Math.round(this.volume * 100)}%`;
        }

        // 更新音效选择下拉菜单
        if (soundEffectSelect && window.soundEffectManager) {
            // 清空现有选项
            soundEffectSelect.innerHTML = '';
            
            // 添加音效选项
            const soundEffects = window.soundEffectManager.getSoundEffects();
            const selectedSound = window.soundEffectManager.getSelectedSound();
            
            soundEffects.forEach(effect => {
                const option = document.createElement('option');
                option.value = effect.file;
                option.textContent = effect.name;
                if (effect.file === selectedSound) {
                    option.selected = true;
                }
                soundEffectSelect.appendChild(option);
            });
        }

        // 更新音效描述
        if (soundEffectDescription && window.soundEffectManager) {
            const soundEffects = window.soundEffectManager.getSoundEffects();
            const selectedSound = window.soundEffectManager.getSelectedSound();
            const selectedEffect = soundEffects.find(effect => effect.file === selectedSound);
            
            if (selectedEffect) {
                soundEffectDescription.textContent = selectedEffect.description;
            }
        }

        // 更新文本特效选择下拉菜单
        if (textEffectSelect && window.cialloEffectManager) {
            // 清空现有选项
            textEffectSelect.innerHTML = '';
            
            // 添加文本特效选项
            const textVariants = window.cialloEffectManager.getTextVariants();
            const selectedText = window.cialloEffectManager.getSelectedText();
            
            textVariants.forEach(text => {
                const option = document.createElement('option');
                option.value = text;
                // 直接使用文本作为显示内容
                option.textContent = text;
                if (text === selectedText) {
                    option.selected = true;
                }
                textEffectSelect.appendChild(option);
            });
        }
    }

    // 更改音效
    changeSoundEffect(soundFile) {
        if (window.soundEffectManager) {
            window.soundEffectManager.setSelectedSound(soundFile);
            
            // 更新音效描述
            const soundEffectDescription = document.getElementById('soundEffectDescription');
            if (soundEffectDescription) {
                const soundEffects = window.soundEffectManager.getSoundEffects();
                const selectedEffect = soundEffects.find(effect => effect.file === soundFile);
                
                if (selectedEffect) {
                    soundEffectDescription.textContent = selectedEffect.description;
                }
            }
            
            // 如果音效开启，播放测试音效
            if (this.soundEnabled) {
                this.playTestSound();
            }
        }
    }

    // 更改文本特效
    changeTextEffect(text) {
        if (window.cialloEffectManager) {
            window.cialloEffectManager.setSelectedText(text);
            console.log(`文本特效已更改为: ${text}`);
        }
    }

    // 切换音效
    toggleSoundEffects() {
        this.soundEnabled = !this.soundEnabled;
        ConfigManager.setBoolConfig('soundEffectsEnabled', this.soundEnabled);

        // 更新音效管理器
        if (window.soundEffectManager) {
            window.soundEffectManager.setEnabled(this.soundEnabled);
        }

        this.updateUI();

        // 如果开启，播放测试音效
        if (this.soundEnabled && window.soundEffectManager) {
            this.playTestSound();
        }
    }

    // 切换Ciallo特效
    toggleCialloEffect() {
        this.cialloEnabled = !this.cialloEnabled;
        ConfigManager.setBoolConfig('cialloEffectEnabled', this.cialloEnabled);
        
        // 更新特效管理器的状态
        if (window.cialloEffectManager) {
            window.cialloEffectManager.setEnabled(this.cialloEnabled);
        }

        this.updateUI();

        // 如果开启，显示测试特效
        if (this.cialloEnabled && window.cialloEffectManager) {
            this.showTestCiallo();
        }
    }

    // 更新音量
    updateVolume(value) {
        this.volume = value / 100;
        ConfigManager.setConfig('soundEffectsVolume', this.volume);

        // 更新音效管理器
        if (window.soundEffectManager) {
            window.soundEffectManager.setVolume(this.volume);
        }

        // 更新UI
        const volumeValue = document.getElementById('volumeValue');
        if (volumeValue) {
            volumeValue.textContent = `${value}%`;
        }
    }

    // 测试音量
    testVolume() {
        if (this.soundEnabled && window.soundEffectManager) {
            this.playTestSound();
        }
    }

    // 播放测试音效
    playTestSound() {
        if (window.soundEffectManager) {
            // 使用当前选中的音效进行测试
            const selectedSound = window.soundEffectManager.getSelectedSound();
            window.soundEffectManager.playSound('click', './sounds/click.mp3', this.volume);
            
            // 直接播放选中的音效
            if (selectedSound) {
                const soundUrl = getSoundUrl(selectedSound);
                const audio = new Audio(soundUrl);
                audio.volume = this.volume;
                audio.play().catch(error => {
                    console.warn('测试音效播放失败:', error);
                });
            }
        }
    }

    // 显示测试Ciallo特效
    showTestCiallo() {
        if (window.cialloEffectManager) {
            // 在屏幕中央显示测试特效
            const x = window.innerWidth / 2;
            const y = window.innerHeight / 2;
            console.log('显示测试特效，当前选中文本:', window.cialloEffectManager.getSelectedText());
            window.cialloEffectManager.createEffect(x, y);
        }
    }
}
