package server

import (
	"fmt"
	"slices"

	"tinyadventure/internal/sim"
)

const (
	trapPC     = 15
	cacheBase  = 20
	cachePerLv = 12
)

var disarmers = []string{"rogue", "ranger"}

func (g *game) visit(x, y int) {
	for i := range g.state.Map.Landmarks {
		l := &g.state.Map.Landmarks[i]
		if l.Used || l.X != x || l.Y != y {
			continue
		}
		l.Used = true
		switch l.Kind {
		case "lookout":
			g.note("From the lookout you see far across the land")
		case "shrine":
			g.mend(100, false)
			g.note("The shrine's light mends the wounds of the living")
		case "cache":
			for i := range g.state.Party.Units {
				if u := &g.state.Party.Units[i]; !u.Fallen() {
					sim.Grant(u, cacheBase+cachePerLv*u.Level)
				}
			}
			g.note("An old cache of maps and journals: the party gains experience")
		}
	}
}

func (g *game) spring() {
	for _, u := range g.state.Party.Units {
		if !u.Fallen() && slices.Contains(disarmers, u.Class) {
			g.note(fmt.Sprintf("%s disarms a trap on the chest", u.Name))
			return
		}
	}
	for i := range g.state.Party.Units {
		if u := &g.state.Party.Units[i]; !u.Fallen() {
			u.Stats.HP = max(1, u.Stats.HP-max(1, sim.Effective(*u).MaxHP*trapPC/100))
		}
	}
	g.note("A trap springs as the chest opens, wounding the party")
}
