import snd_move_1 from '@/assets/sounds/move-1.mp3'
import snd_move_2 from '@/assets/sounds/move-2.mp3'
import snd_move_3 from '@/assets/sounds/move-3.mp3'
import snd_move_4 from '@/assets/sounds/move-4.mp3'
import snd_intro from '@/assets/sounds/intro.mp3'
import snd_click_btn from '@/assets/sounds/button_click.wav'
import snd_ding_1 from '@/assets/sounds/ding-1.wav'
import { Howl } from 'howler'

const soundAssets = [
  { name: 'snd-move-1', src: [snd_move_1], loop: false, volume: 1 },
  { name: 'snd-move-2', src: [snd_move_2], loop: false, volume: 1 },
  { name: 'snd-move-3', src: [snd_move_3], loop: false, volume: 1 },
  { name: 'snd-move-4', src: [snd_move_4], loop: false, volume: 1 },
  { name: 'snd-intro', src: [snd_intro], loop: true, volume: 1 },
  { name: 'snd-click-btn', src: [snd_click_btn], loop: false, volume: 1 },
  { name: 'snd-ding-1', src: [snd_ding_1], loop: false, volume: 1 },
]

export class SoundManager {
  private sounds: Record<string, Howl> = {}

  constructor() {
    soundAssets.forEach(({ name, src, loop, volume = 1 }) => {
      this.sounds[name] = new Howl({
        src,
        loop,
        volume: volume,
      })
    })
  }

  play(name: string) {
    const sound = this.sounds[name]
    if (sound === undefined) {
      console.error(`Sound ${name} not found`)
      return
    }
    sound.play()
  }

  pause(name: string) {
    const sound = this.sounds[name]
    if (sound === undefined) {
      console.error(`Sound ${name} not found`)
      return
    }
    sound.pause()
  }

  stop(name: string) {
    const sound = this.sounds[name]
    if (sound === undefined) {
      console.error(`Sound ${name} not found`)
      return
    }
    sound.pause()
    sound.seek(0)
  }
}
