package conv

import (
	"encoding/json"
	"gadmin/package/gmdata"
	"gadmin/utility"
	"github.com/sirupsen/logrus"
)

var Legend = new(cLegend)

type cLegend struct {
	version string
	lst     []*gmdata.Rune
}

func (j *cLegend) Extract(version string) {
	logrus.Info("cItem Extract...")
	j.setVersion(version).convert().save()
}

func (j *cLegend) setVersion(version string) *cLegend {
	j.version = version
	return j
}

func (j *cLegend) convert() *cLegend {
	b, err := GetUrlBody(j.version, "Legend.json")
	if err != nil {
		logrus.Fatalf("GetUrlBody err:%+v", err)
	}

	var list []*gmdata.Rune
	if err = json.Unmarshal(b, &list); err != nil {
		logrus.Fatalf("err=%v", err)
	}

	j.lst = list
	return j
}

func (j *cLegend) save() {
	filePath := utility.LocalJsonPath(j.version, "legend.json")
	SaveJsonPath(filePath, j.lst)
}
