package assets

import (
	"bytes"
	_ "embed"
	"log"

	"github.com/hajimehoshi/ebiten/v2/audio/wav"
)

type SndAssetName string

const (
	MainLoop SndAssetName = "MainLoop"
	Explode  SndAssetName = "Explode"

	sampleRate = 44100
)

var (
	SoundBank map[SndAssetName]*wav.Stream

	//go:embed loop.wav
	mainloop_snd []byte
	//go:embed explode.wav
	explode_snd []byte

	//go:embed explode.wav
	Splode []byte
	//go:embed hit.wav
	TargetHit []byte
	//go:embed herodeath.wav
	HeroDeath []byte
	//go:embed shot.wav
	Shot []byte
	//go:embed magic.wav
	Magic []byte
	//go:embed survive.wav
	Survive []byte
	//go:embed pausein.wav
	PauseIn []byte
	//go:embed pauseout.wav
	PauseOut []byte
	//go:embed flare.wav
	Flare []byte
)

func LoadSounds() {
	SoundBank = make(map[SndAssetName]*wav.Stream)

	SoundBank[MainLoop] = LoadSoundFatal(sampleRate, mainloop_snd)
	SoundBank[Explode] = LoadSoundFatal(sampleRate, explode_snd)
}

func LoadSoundFatal(rate int, obj []byte) *wav.Stream {

	stream, err := wav.DecodeWithSampleRate(rate, bytes.NewReader(obj))
	if err != nil {
		log.Fatal("dead, jim")
	}

	return stream
}
