package conv

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

var Rune = new(cRune)

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

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

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

func (j *cRune) convert() *cRune {
	b, err := GetUrlBody(j.version, "Rune.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 *cRune) save() {
	filePath := utility.LocalJsonPath(j.version, "rune.json")
	SaveJsonPath(filePath, j.lst)
}
