package main

import (
	"encoding/json"
	"fmt"
	"math/rand/v2"
	"os"
)

type Lane int

const (
	Any Lane = iota
	Red
	Green
	Blue
)

type Type int

const (
	Unit Type = iota
	Spell
	Passive
	Token
)

func (l Lane) String() string {
	switch l {
	case Any:
		return "⬜ Passive"
	case Red:
		return "🟥 Red"
	case Green:
		return "🟩 Green"
	case Blue:
		return "🟦 Blue"
	default:
		panic("unknwon")
	}
}

func (t Type) String() string {
	switch t {
	case Unit:
		return "Unit"
	case Passive:
		return "Passive"
	case Token:
		return "Token"
	default:
		panic("unknown")
	}
}

func main() {
	type (
		jsonfront struct {
			Name       string `json:"name"`
			Type       string `json:"type"`
			Cost       int    `json:"cost"`
			Image      string `json:"image"`
			Horizontal bool   `json:"isHorizontal"`
		}
		jsoncard struct {
			// required
			Id   string `json:"id"`
			Face struct {
				Front jsonfront `json:"front"`
			} `json:"face"`
			Name  string `json:"name"`
			Type  string `json:"type"`
			Cost  int    `json:"cost"`
			Token bool   `json:"isToken"`

			// custom
			Lane    string          `json:"Lane"`
			Entropy int             `json:"Entropy"`
			Force   int             `json:"Force"`
			Legal   map[string]bool `json:"_legal"`
		}
	)

	thejson := make(map[string]jsoncard)
	for i := range len(CARDS) {
		card := &CARDS[i]
		if len(card.name) == 0 {
			card.name = generateTemporaryName()
		}

		baseurl := "https://docs.brut.systems/judah/entropy/"

		var imgurl string
		switch card.lane {
		case Red:
			imgurl = baseurl + "red-base.png"
		case Blue:
			imgurl = baseurl + "blue-base.png"
		case Green:
			imgurl = baseurl + "green-base.png"
		default:
			imgurl = baseurl + "passive-base.png"
		}

		id := fmt.Sprintf("%03d", i)
		thejson[id] = jsoncard{
			Id: id,
			Face: struct {
				Front jsonfront `json:"front"`
			}{
				jsonfront{
					Name:       card.name,
					Type:       card.typ.String(),
					Image:      imgurl,
					Cost:       card.entropy,
					Horizontal: true,
				},
			},
			Token: card.typ == Token,
			Name:  card.name,
			Type:  card.typ.String(),
			Cost:  card.entropy,

			Entropy: card.entropy,
			Force:   card.force,
			Lane:    card.lane.String(),
			Legal: map[string]bool{
				"playtest": true,
			},
		}
	}

	enc, err := json.MarshalIndent(thejson, "", "  ")
	if err != nil {
		panic(err)
	}

	if err := os.WriteFile("CardList.json", enc, 0644); err != nil {
		panic(err)
	}
}

func generateTemporaryName() string {
	var (
		adjectives = []string{
			"Absent",
			"Ambient",
			"Bare",
			"Blunt",
			"Civic",
			"Composite",
			"Concave",
			"Dull",
			"Errant",
			"Excess",
			"Flat",
			"Former",
			"Gross",
			"Inland",
			"Lateral",
			"Live",
			"Lucid",
			"Moot",
			"Nominal",
			"Oblique",
			"Partial",
			"Passive",
			"Petit",
			"Polar",
			"Residual",
			"Rote",
			"Soft",
			"Spent",
			"Terminal",
			"Warm",
		}
		nouns = []string{
			"Aperture",
			"Axis",
			"Bulk",
			"Cartilage",
			"Compact",
			"Contour",
			"Conviction",
			"Cortex",
			"Deficit",
			"Dividend",
			"Dosage",
			"Envelope",
			"Filament",
			"Gesture",
			"Gradient",
			"Inlet",
			"Lapse",
			"Mandate",
			"Membrane",
			"Morale",
			"Offset",
			"Posture",
			"Precinct",
			"Prospect",
			"Rapport",
			"Reflex",
			"Substrate",
			"Surplus",
			"Tendon",
			"Volume",
		}
	)

	return adjectives[rand.IntN(len(adjectives))] + nouns[rand.IntN(len(nouns))]
}
