package facility

import (
	"encoding/json"
	"log/slog"
	"os"
)

type conditions struct {
	Type  int `json:"type"`
	Level int `json:"level"`
}

type facility struct {
	Title      string       `json:"title"`
	Des        string       `json:"des"`
	Name       string       `json:"name"`
	Type       int8         `json:"type"`
	Additions  []int8       `json:"additions"`
	Conditions []conditions `json:"conditions"`
	Levels     []fLevel     `json:"levels"`
}

type NeedRes struct {
	Decree int `json:"decree"`
	Grain  int `json:"grain"`
	Wood   int `json:"wood"`
	Iron   int `json:"iron"`
	Stone  int `json:"stone"`
	Gold   int `json:"gold"`
}

type fLevel struct {
	Level  int     `json:"level"`
	Values []int   `json:"values"`
	Need   NeedRes `json:"need"`
	Time   int     `json:"time"` //升级需要的时间
}

func NewFacility(jsonName string) *facility {
	f := &facility{}
	f.load(jsonName)
	return f
}

func (this *facility) load(jsonName string) {

	jdata, err := os.ReadFile(jsonName)
	if err != nil {
		slog.Error("facility load file error",
			"error", err, "file", jsonName)
		os.Exit(0)
	}

	json.Unmarshal(jdata, this)

	slog.Debug("facility file loaded", "name", this.Name)
}
