package server

import (
	"testing"

	"tinyadventure/internal/model"
	"tinyadventure/internal/sim"
	"tinyadventure/internal/world"
)

func party(classes ...string) model.Party {
	var p model.Party
	for _, c := range classes {
		p.Units = append(p.Units, model.Unit{ID: c, Name: c, Class: c, Level: 1, Stats: model.Stats{HP: 100, MaxHP: 100}})
	}
	return p
}

func TestRoamingBandInterceptsTheParty(t *testing.T) {
	g := openGame(30, 3)
	g.state.Party = party("fighter")
	g.state.Party.Y = 1
	g.state.Map.Bands = []model.Band{{ID: "b1", Kind: "roam", X: 10, Y: 1, HomeX: 10, HomeY: 1}}
	g.encs = map[string]model.Encounter{"b1": {ID: "enc-b1", POI: "b1"}}
	if err := g.move(29, 1, 0); err != nil {
		t.Fatal(err)
	}
	b := g.band("b1")
	if g.threat != "b1" || world.Distance(b.X, b.Y, g.state.Party.X, g.state.Party.Y) > 1 || g.state.Party.X >= 29 {
		t.Fatalf("threat %q band %d,%d party %d,%d", g.threat, b.X, b.Y, g.state.Party.X, g.state.Party.Y)
	}
	if st := g.snapshot(); st.Threat == nil || st.Threat.Band != "b1" {
		t.Fatalf("snapshot threat %+v", st.Threat)
	}
}

func TestGuardBlocksItsChest(t *testing.T) {
	g := openGame(8, 1)
	g.state.Party = party("fighter")
	g.state.Map.Loot = []model.LootNode{{ID: "c1", X: 4, Y: 0, Item: model.Item{ID: "i1"}}}
	g.state.Map.Bands = []model.Band{{ID: "b1", Kind: "guard", X: 4, Y: 0}}
	g.encs = map[string]model.Encounter{"b1": {ID: "enc-b1", POI: "b1"}}
	g.move(7, 0, 0)
	if g.threat != "b1" || g.state.Party.X != 3 || g.state.Map.Loot[0].Collected {
		t.Fatalf("threat %q party %d collected %v", g.threat, g.state.Party.X, g.state.Map.Loot[0].Collected)
	}
}

func TestHazardsSapButNeverKill(t *testing.T) {
	g := openGame(40, 1)
	for x := range 40 {
		g.state.Map.Hex(x, 0)[0] = model.ShapeShallow
	}
	g.state.Party = party("fighter", "mage")
	g.state.Party.Units[1].Gear = model.Gear{model.SlotArmor: &model.Item{ID: "w", Trait: "guard-frost"}}
	g.move(39, 0, 0)
	f, m := g.state.Party.Units[0], g.state.Party.Units[1]
	if f.Stats.HP >= 100 || f.Stats.HP < 1 || m.Stats.HP != 100 || len(g.state.Notes) == 0 {
		t.Fatalf("fighter %d mage %d notes %v", f.Stats.HP, m.Stats.HP, g.state.Notes)
	}
}

func TestLandmarksAndTraps(t *testing.T) {
	g := openGame(10, 1)
	g.state.Party = party("fighter")
	g.state.Party.Units[0].Stats.HP = 10
	g.state.Map.Landmarks = []model.Landmark{{ID: "s", Kind: "shrine", X: 2}, {ID: "c", Kind: "cache", X: 4}}
	g.state.Map.Loot = []model.LootNode{{ID: "t", X: 6, Trap: true, Item: model.Item{ID: "i"}}}
	g.move(5, 0, 0)
	u := g.state.Party.Units[0]
	if u.Stats.HP != sim.Effective(u).MaxHP || u.XP == 0 && u.Level == 1 || !g.state.Map.Landmarks[0].Used {
		t.Fatalf("hp %d xp %d level %d", u.Stats.HP, u.XP, u.Level)
	}
	before := u.Stats.HP
	g.move(6, 0, 0)
	if g.state.Party.Units[0].Stats.HP >= before {
		t.Fatal("trap did not wound a party without a rogue")
	}
	g = openGame(10, 1)
	g.state.Party = party("rogue")
	g.state.Map.Loot = []model.LootNode{{ID: "t", X: 3, Trap: true, Item: model.Item{ID: "i"}}}
	g.move(3, 0, 0)
	if g.state.Party.Units[0].Stats.HP != 100 {
		t.Fatal("rogue failed to disarm")
	}
}

func TestBackingOffShakesABandOff(t *testing.T) {
	g := openGame(30, 3)
	g.state.Party = party("fighter")
	g.state.Party.X, g.state.Party.Y = 10, 1
	g.state.Map.Bands = []model.Band{{ID: "b1", Kind: "roam", X: 11, Y: 1, HomeX: 11, HomeY: 1}}
	g.encs = map[string]model.Encounter{"b1": {ID: "enc-b1", POI: "b1"}}
	g.threat = "b1"
	if err := g.move(2, 1, 0); err != nil {
		t.Fatal(err)
	}
	if g.threat != "" || g.state.Party.X > 5 {
		t.Fatalf("party stuck at %d with threat %q", g.state.Party.X, g.threat)
	}
}

func TestRoutesGoAroundHazardsWhenTheDetourIsShort(t *testing.T) {
	g := openGame(7, 3)
	g.state.Party.Y = 1
	for x := 2; x <= 4; x++ {
		g.state.Map.Hex(x, 1)[1] = 6
	}
	steps, err := g.path(6, 1)
	if err != nil {
		t.Fatal(err)
	}
	for _, s := range steps {
		if name, _ := hazardOf(&g.state.Map, s[0], s[1]); name != "" {
			t.Fatalf("route crosses %s at %v: %v", name, s, steps)
		}
	}
	for x := range 7 {
		g.state.Map.Hex(x, 0)[1], g.state.Map.Hex(x, 2)[1] = 6, 6
	}
	if steps, err = g.path(6, 1); err != nil || len(steps) != 6 {
		t.Fatalf("with hazards everywhere the route must go straight: %v %v", steps, err)
	}
}

func TestEquippingNeverLeavesHpAboveTheNewMax(t *testing.T) {
	g := openGame(2, 1)
	g.state.Party = party("fighter")
	g.state.Party.Inventory = []model.Item{{ID: "axe", Slot: model.SlotWeapon, Bonus: model.Stats{MaxHP: -6}}}
	if err := g.equip(model.Action{Unit: "fighter", Item: "axe"}); err != nil {
		t.Fatal(err)
	}
	if hp := g.state.Party.Units[0].Stats.HP; hp != 94 {
		t.Fatalf("hp %d of 94", hp)
	}
}
