package conv

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

var PlayLevel = new(cPlayLevel)

type cPlayLevel struct {
	version string
	lst     []*gmdata.Level
}

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

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

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

	if err := json.Unmarshal(b, &j.lst); err != nil {
		logrus.Fatalf("err=%v", err)
	}

	return j
}

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