package assets

import (
	"bytes"
	"log"

	_ "embed"

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

type SndAssetName string

const (
	MainLoop    SndAssetName = "MainLoop"
	MainLoopMp3 SndAssetName = "MainLoopMp3"
	MainLoopOgg SndAssetName = "MainLoopOgg"
	SampleRate               = 44100
)

var (
	//SoundBank    map[SndAssetName]*wav.Stream
	//SoundBankMp3 map[SndAssetName]*mp3.Stream
	SoundBankOgg map[SndAssetName]*vorbis.Stream
	//go:embed mainloop.ogg
	mainloop_snd []byte
)

func init() {
	LoadSounds()
}

func LoadSounds() {
	//SoundBank = make(map[SndAssetName]*wav.Stream)
	//SoundBankMp3 = make(map[SndAssetName]*mp3.Stream)
	SoundBankOgg = make(map[SndAssetName]*vorbis.Stream)

	//SoundBank[MainLoop] = LoadSoundFatal(SampleRate, hyper_snd)
	//SoundBankMp3[MainLoopMp3] = LoadSoundFatalMp3(SampleRate, mainloop_snd)
	SoundBankOgg[MainLoopOgg] = LoadSoundFatalOgg(SampleRate, mainloop_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
}

func LoadSoundFatalMp3(rate int, obj []byte) *mp3.Stream {

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

	return stream
}

func LoadSoundFatalOgg(rate int, obj []byte) *vorbis.Stream {

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

	return stream
}
