// AudioManager.js
// 音频管理器

class AudioManager {
  constructor() {
    this.sounds = new Map();
    this.music = new Map();
    this.masterVolume = 1.0;
    this.soundVolume = 1.0;
    this.musicVolume = 1.0;
    this.enabled = true;
  }

  // 加载音效
  loadSound(name, src, options = {}) {
    return new Promise((resolve, reject) => {
      const audio = new Audio();
      audio.src = src;
      audio.preload = 'auto';
      audio.loop = options.loop || false;
      audio.volume = (options.volume || 1.0) * this.soundVolume * this.masterVolume;
      
      audio.addEventListener('canplaythrough', () => {
        this.sounds.set(name, {
          audio,
          volume: options.volume || 1.0,
          loop: options.loop || false
        });
        resolve(audio);
      });
      
      audio.addEventListener('error', (e) => {
        reject(new Error(`Failed to load sound: ${src}`));
      });
    });
  }

  // 加载音乐
  loadMusic(name, src, options = {}) {
    return new Promise((resolve, reject) => {
      const audio = new Audio();
      audio.src = src;
      audio.preload = 'auto';
      audio.loop = options.loop || false;
      audio.volume = (options.volume || 1.0) * this.musicVolume * this.masterVolume;
      
      audio.addEventListener('canplaythrough', () => {
        this.music.set(name, {
          audio,
          volume: options.volume || 1.0,
          loop: options.loop || false
        });
        resolve(audio);
      });
      
      audio.addEventListener('error', (e) => {
        reject(new Error(`Failed to load music: ${src}`));
      });
    });
  }

  // 播放音效
  playSound(name, options = {}) {
    if (!this.enabled) return;
    
    const sound = this.sounds.get(name);
    if (!sound) {
      console.warn(`Sound ${name} not found`);
      return;
    }
    
    // 克隆音频以便可以同时播放多次
    const audioClone = sound.audio.cloneNode();
    audioClone.volume = (options.volume || sound.volume) * this.soundVolume * this.masterVolume;
    audioClone.loop = options.loop || sound.loop;
    
    audioClone.play().catch(e => {
      console.warn(`Failed to play sound ${name}:`, e);
    });
    
    return audioClone;
  }

  // 播放音乐
  playMusic(name, options = {}) {
    if (!this.enabled) return;
    
    const music = this.music.get(name);
    if (!music) {
      console.warn(`Music ${name} not found`);
      return;
    }
    
    music.audio.volume = (options.volume || music.volume) * this.musicVolume * this.masterVolume;
    music.audio.loop = options.loop || music.loop;
    
    music.audio.play().catch(e => {
      console.warn(`Failed to play music ${name}:`, e);
    });
    
    return music.audio;
  }

  // 停止音乐
  stopMusic(name) {
    const music = this.music.get(name);
    if (music) {
      music.audio.pause();
      music.audio.currentTime = 0;
    }
  }

  // 暂停音乐
  pauseMusic(name) {
    const music = this.music.get(name);
    if (music) {
      music.audio.pause();
    }
  }

  // 恢复音乐
  resumeMusic(name) {
    const music = this.music.get(name);
    if (music) {
      music.audio.play().catch(e => {
        console.warn(`Failed to resume music ${name}:`, e);
      });
    }
  }

  // 设置主音量
  setMasterVolume(volume) {
    this.masterVolume = Math.max(0, Math.min(1, volume));
    // 更新所有音频的音量
    for (const [name, sound] of this.sounds) {
      sound.audio.volume = sound.volume * this.soundVolume * this.masterVolume;
    }
    for (const [name, music] of this.music) {
      music.audio.volume = music.volume * this.musicVolume * this.masterVolume;
    }
  }

  // 设置音效音量
  setSoundVolume(volume) {
    this.soundVolume = Math.max(0, Math.min(1, volume));
  }

  // 设置音乐音量
  setMusicVolume(volume) {
    this.musicVolume = Math.max(0, Math.min(1, volume));
  }

  // 启用/禁用音频
  setEnabled(enabled) {
    this.enabled = enabled;
    if (!enabled) {
      // 停止所有音频
      for (const [name, music] of this.music) {
        music.audio.pause();
      }
    }
  }

  // 预加载音频资源
  preload(resources) {
    const promises = [];
    
    for (const [name, resource] of Object.entries(resources)) {
      if (resource.type === 'sound') {
        promises.push(this.loadSound(name, resource.src, resource.options));
      } else if (resource.type === 'music') {
        promises.push(this.loadMusic(name, resource.src, resource.options));
      }
    }
    
    return Promise.all(promises);
  }
}

export default AudioManager;
export { AudioManager };