export class SoundContainer {

  constructor(soundPath) {
    this.lastInstance = 0
    this.soundInstances = [
      new Audio(soundPath),
      new Audio(soundPath),
      new Audio(soundPath),
      new Audio(soundPath),
      new Audio(soundPath),
      new Audio(soundPath),
    ]
  }

  async play() {
    return new Promise((resolve) => {
      this.lastInstance = this.lastInstance + 1 >= this.soundInstances.length ? 0 : this.lastInstance + 1
      this.soundInstances[this.lastInstance].play()
      resolve()
    })
  }

  stop() {
    this.soundInstances.forEach((sound) => sound.currentTime = 0)
  }

  setVolume(volume) {
    this.soundInstances.forEach((sound) => sound.volume = volume)
  }
}

export class SoundEffect {
  static explosion = new SoundContainer("assets/sounds/explosion3.ogg")
  static explosionBig = new SoundContainer("assets/sounds/explosion_big.ogg")

  static soundList = [
    SoundEffect.explosion,
    SoundEffect.explosionBig
  ]
  
  static setVolumeAll(volume) {
    SoundEffect.soundList.forEach((sound) => sound.setVolume(volume))
  }
  
  static stopAll() {
    SoundEffect.soundList.forEach((sound) => sound.stop())
  }
}