package assets

import (
	"gitea.boner.be/bdnugget/goonscape/types"
	rl "github.com/gen2brain/raylib-go/raylib"
)

// Helper function to load animations for a model
func loadModelAnimations(animPaths map[string]string) (types.AnimationSet, error) {
	var animSet types.AnimationSet

	// Load idle animations if specified
	if idlePath, ok := animPaths["idle"]; ok {
		idleAnims := rl.LoadModelAnimations(idlePath)
		if len(idleAnims) > 0 {
			animSet.Idle = idleAnims
			rl.TraceLog(rl.LogInfo, "Loaded idle animation: %s (%d frames, %f seconds)",
				idlePath, idleAnims[0].FrameCount, float32(idleAnims[0].FrameCount)/60.0)
		}
	}

	// Load walk animations if specified
	if walkPath, ok := animPaths["walk"]; ok {
		walkAnims := rl.LoadModelAnimations(walkPath)
		if len(walkAnims) > 0 {
			animSet.Walk = walkAnims
			rl.TraceLog(rl.LogInfo, "Loaded walk animation: %s (%d frames, %f seconds)",
				walkPath, walkAnims[0].FrameCount, float32(walkAnims[0].FrameCount)/60.0)
		}
	}

	return animSet, nil
}

func LoadModels() ([]types.ModelAsset, error) {
	// Goonion model and animations
	goonerModel := rl.LoadModel("resources/models/gooner/walk_no_y_transform.glb")
	goonerAnims, _ := loadModelAnimations(map[string]string{"idle": "resources/models/gooner/idle_no_y_transform.glb", "walk": "resources/models/gooner/walk_no_y_transform.glb"})

	// Apply transformations
	transform := rl.MatrixIdentity()
	transform = rl.MatrixMultiply(transform, rl.MatrixRotateY(180*rl.Deg2rad))
	transform = rl.MatrixMultiply(transform, rl.MatrixRotateX(-90*rl.Deg2rad))
	transform = rl.MatrixMultiply(transform, rl.MatrixScale(1.0, 1.0, 1.0))
	goonerModel.Transform = transform

	// Coomer model (ready for animations)
	coomerModel := rl.LoadModel("resources/models/coomer/idle_notransy.glb")
	// coomerTexture := rl.LoadTexture("resources/models/coomer.png")
	// rl.SetMaterialTexture(coomerModel.Materials, rl.MapDiffuse, coomerTexture)
	// When you have animations, add them like:
	coomerAnims, _ := loadModelAnimations(map[string]string{"idle": "resources/models/coomer/idle_notransy.glb", "walk": "resources/models/coomer/unsteadywalk_notransy.glb"})
	coomerModel.Transform = transform

	// Shreke model (ready for animations)
	shrekeModel := rl.LoadModel("resources/models/shreke.obj")
	shrekeTexture := rl.LoadTexture("resources/models/shreke.png")
	rl.SetMaterialTexture(shrekeModel.Materials, rl.MapDiffuse, shrekeTexture)
	// When you have animations, add them like:
	// shrekeAnims, _ := loadModelAnimations("resources/models/shreke.glb",
	//     map[string]string{
	//         "idle": "resources/models/shreke_idle.glb",
	//         "walk": "resources/models/shreke_walk.glb",
	//     })

	return []types.ModelAsset{
		{
			Model:      goonerModel,
			Animation:  append(goonerAnims.Idle, goonerAnims.Walk...),
			AnimFrames: int32(len(goonerAnims.Idle) + len(goonerAnims.Walk)),
			Animations: goonerAnims,
			YOffset:    0.0,
		},
		{
			Model:      coomerModel,
			Animation:  append(coomerAnims.Idle, coomerAnims.Walk...),
			AnimFrames: int32(len(coomerAnims.Idle) + len(coomerAnims.Walk)),
			Animations: coomerAnims,
			YOffset:    -4.0,
		},
		{Model: shrekeModel, Texture: shrekeTexture},
	}, nil
}

func LoadMusic(filename string) (rl.Music, error) {
	return rl.LoadMusicStream(filename), nil
}

func UnloadModels(models []types.ModelAsset) {
	for _, model := range models {
		if model.Animation != nil {
			for i := int32(0); i < model.AnimFrames; i++ {
				rl.UnloadModelAnimation(model.Animation[i])
			}
		}
		rl.UnloadModel(model.Model)
		rl.UnloadTexture(model.Texture)
	}
}

func UnloadMusic(music rl.Music) {
	rl.UnloadMusicStream(music)
}
