package conv

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

var Item = new(cItem)

type cItem struct {
	version string
	lst     []*gmdata.Material
}

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

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

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

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

	j.lst = list
	return j
}

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