package components

import (
	"dk/internal/helpers"
	"dk/internal/models/spells"
	"dk/internal/models/towns"
	"dk/internal/models/users"
	"fmt"

	sushi "git.sharkk.net/Sharkk/Sushi"
)

// LeftAside generates the data map for the left sidebar.
// Returns an empty map when not auth'd.
func LeftAside(ctx sushi.Ctx) map[string]any {
	data := map[string]any{}

	if !ctx.IsAuthenticated() {
		return data
	}

	user := ctx.UserValue("user").(*users.User)

	// Build owned town maps list
	if user.Towns != "" {
		townMap := helpers.NewOrderedMap[int, *towns.Town]()
		for _, id := range user.GetTownIDs() {
			if town, err := towns.Find(id); err == nil {
				townMap.Set(id, town)
			}
		}
		data["_towns"] = townMap.ToSlice()
	}

	return data
}

// RightAside generates the data map for the right sidebar.
// Returns an empty map when not auth'd.
func RightAside(ctx sushi.Ctx) map[string]any {
	data := map[string]any{}

	if !ctx.IsAuthenticated() {
		return data
	}

	user := ctx.UserValue("user").(*users.User)

	data["_expprog"] = fmt.Sprintf("%.1f", user.ExpProgress())

	hpPct := helpers.ClampPct(float64(user.HP), float64(user.MaxHP), 0, 100)
	data["hppct"] = hpPct
	data["mppct"] = helpers.ClampPct(float64(user.MP), float64(user.MaxMP), 0, 100)
	data["tppct"] = helpers.ClampPct(float64(user.TP), float64(user.MaxTP), 0, 100)

	data["hpcolor"] = ""
	if hpPct < 35 {
		data["hpcolor"] = "danger"
	} else if hpPct < 75 {
		data["hpcolor"] = "warning"
	}

	// Build known healing spells list
	if user.Spells != "" {
		spellMap := helpers.NewOrderedMap[int, string]()
		for _, id := range user.GetSpellIDs() {
			if spell, err := spells.Find(id); err == nil {
				spellMap.Set(id, spell.Name)
			}
		}
		data["_spells"] = spellMap.ToSlice()
	}

	return data
}
