package gen

import (
	"math/rand/v2"

	"tinyadventure/internal/rng"
)

var allPOIKinds = []string{"mine", "beach", "ruins", "crypt", "village", "tower", "camp"}

type Systems struct {
	Factions  bool
	SideQuest bool
	Hazards   bool
	POIKinds  []string
}

func rollSystems(r *rand.Rand) Systems {
	kinds := rng.Shuffled(r, allPOIKinds)[:rng.Between(r, 4, len(allPOIKinds))]
	return Systems{
		Factions:  r.IntN(4) != 0,
		SideQuest: r.IntN(4) != 0,
		Hazards:   r.IntN(2) == 0,
		POIKinds:  kinds,
	}
}

func (s Systems) List() []string {
	var out []string
	for name, on := range map[string]bool{"factions": s.Factions, "sidequests": s.SideQuest, "hazards": s.Hazards} {
		if on {
			out = append(out, name)
		}
	}
	sortStrings(out)
	for _, k := range s.POIKinds {
		out = append(out, "poi:"+k)
	}
	return out
}

func sortStrings(s []string) {
	for i := 1; i < len(s); i++ {
		for j := i; j > 0 && s[j] < s[j-1]; j-- {
			s[j], s[j-1] = s[j-1], s[j]
		}
	}
}
