package model

const (
	ShapeDeep byte = iota
	ShapeShallow
	ShapePlain
	ShapeHill
	ShapeMountain
	ShapePeak
)

const (
	FlagForest byte = 1 << iota
	FlagRoad
)

const HexBytes = 4

var Biomes = []string{"Frost steppe", "Frostwood", "Rime fen", "Fey meadows", "Enchanted forest", "Mistbog", "Ember badlands", "Ashwood", "Jungle of vines"}

type POI struct {
	ID      string `json:"id"`
	Kind    string `json:"kind"`
	Name    string `json:"name"`
	X       int    `json:"x"`
	Y       int    `json:"y"`
	Faction string `json:"faction,omitempty"`
	Level   int    `json:"level"`
	Cleared bool   `json:"cleared"`
}

type SpawnPoint struct {
	ID   string `json:"id"`
	Kind string `json:"kind"`
	X    int    `json:"x"`
	Y    int    `json:"y"`
	Hint string `json:"hint"`
}

type LootNode struct {
	ID        string `json:"id"`
	X         int    `json:"x"`
	Y         int    `json:"y"`
	Item      Item   `json:"item"`
	Collected bool   `json:"collected"`
	Trap      bool   `json:"trap,omitempty"`
}

type Band struct {
	ID      string `json:"id"`
	Kind    string `json:"kind"`
	Name    string `json:"name"`
	Faction string `json:"faction"`
	Level   int    `json:"level"`
	X       int    `json:"x"`
	Y       int    `json:"y"`
	HomeX   int    `json:"homeX"`
	HomeY   int    `json:"homeY"`
	Cleared bool   `json:"cleared,omitempty"`
	Calm    int    `json:"calm,omitempty"`
}

type Landmark struct {
	ID   string `json:"id"`
	Kind string `json:"kind"`
	X    int    `json:"x"`
	Y    int    `json:"y"`
	Used bool   `json:"used,omitempty"`
}

type Map struct {
	Width     int          `json:"width"`
	Height    int          `json:"height"`
	Hexes     []byte       `json:"hexes"`
	POIs      []POI        `json:"pois"`
	Spawns    []SpawnPoint `json:"spawns"`
	Loot      []LootNode   `json:"loot"`
	Bands     []Band       `json:"bands"`
	Landmarks []Landmark   `json:"landmarks"`
}

func (m *Map) In(x, y int) bool { return x >= 0 && y >= 0 && x < m.Width && y < m.Height }

func (m *Map) Hex(x, y int) []byte {
	i := (y*m.Width + x) * HexBytes
	return m.Hexes[i : i+HexBytes]
}

func (m *Map) Walkable(x, y int) bool {
	if !m.In(x, y) {
		return false
	}
	s := m.Hex(x, y)[0]
	return s != ShapeDeep && s != ShapePeak
}

func (m *Map) Cost(x, y int) int {
	h := m.Hex(x, y)
	if h[3]&FlagRoad != 0 {
		return 1
	}
	c := [...]int{0, 3, 2, 3, 4, 0}[h[0]]
	if h[3]&FlagForest != 0 {
		c++
	}
	return c
}
