package itemCfg

import (
	languageCfg "server/conf/language"
	"server/gamedata"
	"server/msg"
	"server/pkg/github.com/name5566/leaf/log"
	"strconv"
	"strings"
)

var CFG_NAME = "Item"

func init() {
	gamedata.InitCfg(CFG_NAME)
}

func GetItemType(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, Id)
	if err != nil {
		log.Debug("GetItemType err:%v, Id=%d", err, Id)
		return 0
	}
	return gamedata.GetIntValue(data, "IType")
}

func GetItemEffect(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, Id)
	if err != nil {
		log.Debug("GetItemEffect err:%v, Id=%d", err, Id)
		return 0
	}
	return gamedata.GetIntValue(data, "Effect")
}

func GetItemEffectList(Id int) []int {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, Id)
	if err != nil {
		log.Debug("GetItemEffectList err:%v, Id=%d", err, Id)
		return nil
	}
	str := gamedata.GetStringValue(data, "Effect")
	a := strings.Split(str, ",")
	res := make([]int, 0, len(a))
	for _, v := range a {
		b, err := strconv.Atoi(v)
		if err != nil {
			log.Debug("GetItemEffectList parse err:%v, v=%s", err, v)
			continue
		}
		res = append(res, b)
	}
	return res
}

func GetItemName(Id int) (string, string) {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, Id)
	if err != nil {
		log.Debug("GetItemName err:%v, Id=%d", err, Id)
		return "", ""
	}
	name := gamedata.GetStringValue(data, "Name")
	return languageCfg.GetLanguage(msg.LANG_TYPE_LANG_CN, name), languageCfg.GetLanguage(msg.LANG_TYPE_LANG_EN, name)
}
