package assets

import (
	"bytes"
	"image"
	"log"

	_ "embed"

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

type ImgAssetName string

const (
	Title         ImgAssetName = "Title"
	FlyEyeNormal  ImgAssetName = "FlyEyeNormal"
	FlyEyeDamaged ImgAssetName = "FlyEyeDamaged"
	FlyEyeDying   ImgAssetName = "FlyEyeDying"
	FlyEyeShadow  ImgAssetName = "FlyEyeShadow"
	HeroNormal    ImgAssetName = "HeroNormal"
	HeroDying     ImgAssetName = "HeroDying"
	TileSet       ImgAssetName = "TileSet"
	Altar         ImgAssetName = "Altar"
	Weapon        ImgAssetName = "Weapon"
	WormDamaged   ImgAssetName = "WormDamaged"
	Worm          ImgAssetName = "WormDefault"
	Cloud         ImgAssetName = "Cloud"
	Fireball      ImgAssetName = "Fireball"
	Splash        ImgAssetName = "Splash"
	LaserBeam     ImgAssetName = "LaserBeam"
	ItemLaser     ImgAssetName = "ItemLaser"
	RainSplash    ImgAssetName = "RainSplash"
)

var (
	ImageBank map[ImgAssetName]*ebiten.Image

	//go:embed title.png
	title_img []byte
	//go:embed fly-eye.png
	flyeye_img []byte
	//go:embed fly-eye2.png
	flyeye_img2 []byte
	//go:embed fly-eye3.png
	flyeye_img3 []byte
	//go:embed shadow.png
	shadow_img []byte
	//go:embed hero.png
	hero_img []byte
	//go:embed herodeath.png
	herodeath_img []byte
	//go:embed grasstile.png
	tileset_img []byte
	//go:embed altar.png
	altar_img []byte
	//go:embed weapon.png
	weapon_img []byte
	//go:embed worm.png
	worm_img []byte
	//go:embed wormdefault.png
	wormdefault_img []byte
	//go:embed cloud.png
	cloud_img []byte
	//go:embed hot.png
	fireball_img []byte
	//go:embed splash.png
	splash_img []byte
	//go:embed laserbeam.png
	laserbeam_img []byte
	//go:embed item-laser.png
	itemlaser_img []byte
	//go:embed rain-splash.png
	rainsplash_img []byte
)

func LoadImages() {
	ImageBank = make(map[ImgAssetName]*ebiten.Image)

	ImageBank[Title] = LoadImagesFatal(title_img)
	ImageBank[FlyEyeNormal] = LoadImagesFatal(flyeye_img)
	ImageBank[FlyEyeDamaged] = LoadImagesFatal(flyeye_img2)
	ImageBank[FlyEyeDying] = LoadImagesFatal(flyeye_img3)
	ImageBank[FlyEyeShadow] = LoadImagesFatal(shadow_img)
	ImageBank[HeroNormal] = LoadImagesFatal(hero_img)
	ImageBank[HeroDying] = LoadImagesFatal(herodeath_img)
	ImageBank[TileSet] = LoadImagesFatal(tileset_img)
	ImageBank[Altar] = LoadImagesFatal(altar_img)
	ImageBank[Weapon] = LoadImagesFatal(weapon_img)
	ImageBank[WormDamaged] = LoadImagesFatal(worm_img)
	ImageBank[Worm] = LoadImagesFatal(wormdefault_img)
	ImageBank[Cloud] = LoadImagesFatal(cloud_img)
	ImageBank[Fireball] = LoadImagesFatal(fireball_img)
	ImageBank[Splash] = LoadImagesFatal(splash_img)
	ImageBank[LaserBeam] = LoadImagesFatal(laserbeam_img)
	ImageBank[ItemLaser] = LoadImagesFatal(itemlaser_img)
	ImageBank[RainSplash] = LoadImagesFatal(rainsplash_img)

}

func LoadImagesFatal(b []byte) *ebiten.Image {
	img, _, err := image.Decode(bytes.NewReader(b))
	if err != nil {
		log.Fatal(err)
	}
	return ebiten.NewImageFromImage(img)
}
