package userCfg

import (
	"server/game/mod/item"
	"server/gamedata"
	"server/pkg/github.com/name5566/leaf/log"
)

const (
	CFG_NAME       = "UserData"
	CFG_NANE_CONST = "UserDataConst"
)

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

// 获取用户能量倍数
func GetEnergyMulByLv(lv int) int {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, lv)
	if err != nil {
		log.Debug("UserDataCfg GetEnergyMulByLv lv:%v not found", lv)
		return 0
	}
	return gamedata.GetIntValue(data, "EnergyMul")
}

func GetEnergyMax(Lv int) int {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, Lv)
	if err != nil {
		log.Debug("UserDataCfg GetEnergyMax lv:%v not found", Lv)
		return 0
	}
	return gamedata.GetIntValue(data, "MaxEnergy")
}

// 获取七天登录加成
func GetSevenloginAdd(Lv int) int {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, Lv)
	if err != nil {
		log.Debug("UserDataCfg GetSevenloginAdd lv:%v not found", Lv)
		return 0
	}
	return gamedata.GetIntValue(data, "SevenLogin")
}

// 获取订单系数
func GetOrderNByLv(lv int) (int, error) {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, lv)
	if err != nil {
		log.Debug("UserDataCfg GetOrderNByLv lv:%v not found", lv)
		return 0, err
	}
	return gamedata.GetIntValue(data, "OrderN"), nil
}

// 获取升级经验
func GetLevUpExp(lv int) (int, int) {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, lv)
	if err != nil {
		log.Debug("UserDataCfg GetLevUpExp lv:%v not found", lv)
		return 0, 0
	}
	return gamedata.GetIntValue(data, "Exp"), gamedata.GetIntValue(data, "PExp")
}

func GetNewLevUpExp(lv int, Exp int, PetExt int) (int, int) {
	ExpLv := lv
	PetLv := lv
	for {
		data1, err := gamedata.GetDataByIntKey(CFG_NAME, ExpLv)
		if err != nil {
			break
		}
		ExpNeed := gamedata.GetIntValue(data1, "Exp")
		if Exp >= ExpNeed {
			Exp -= ExpNeed
			ExpLv++
		} else {
			break
		}
	}
	for {
		data2, err := gamedata.GetDataByIntKey(CFG_NAME, PetLv)
		if err != nil {
			break
		}
		PetNeed := gamedata.GetIntValue(data2, "PExp")
		if PetExt >= PetNeed {
			PetExt -= PetNeed
			PetLv++
		} else {
			break
		}
	}
	return ExpLv, PetLv
}

// 获取能量回复时间
func GetRecover(lv int) int {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, lv)
	if err != nil {
		log.Debug("UserDataCfg GetRecover lv:%v not found", lv)
		return 0
	}
	return gamedata.GetIntValue(data, "Recover")
}

// 获取解锁背包数量
func GetUnlockPack(lv int) int {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, lv)
	if err != nil {
		log.Debug("UserDataCfg GetUnlockPack lv:%v not found", lv)
		return 0
	}
	return gamedata.GetIntValue(data, "UnlockPack")
}

// 获取升级奖励
func GetLevUpReward(lv int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, lv)
	if err != nil {
		log.Debug("UserDataCfg GetLevUpReward lv:%v not found", lv)
		return nil
	}
	itemMap := gamedata.GetValue(data, "Item")
	itemList := item.ParseItem(itemMap)
	return itemList
}

func GetUnlock(Lv int) string {
	data, err := gamedata.GetDataByIntKey(CFG_NAME, Lv)
	if err != nil {
		log.Debug("UserDataCfg GetUnlock lv:%v not found", Lv)
		return ""
	}
	s1 := gamedata.GetStringValue(data, "Unlock_1")
	s2 := gamedata.GetStringValue(data, "Unlock_2")
	return s1 + "," + s2
}

func GetInitEnergy() int {
	data, err := gamedata.GetDataByKey(CFG_NANE_CONST, "Energy")
	if err != nil {
		log.Debug("UserDataCfg GetInitEnergy not found")
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetInitDiamond() int {
	data, err := gamedata.GetDataByKey(CFG_NANE_CONST, "Diamond")
	if err != nil {
		log.Debug("UserDataCfg GetInitDiamond not found")
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetInitStar() int {
	data, err := gamedata.GetDataByKey(CFG_NANE_CONST, "Star")
	if err != nil {
		log.Debug("UserDataCfg GetInitStar not found")
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}
