package baseCfg

import (
	"server/gamedata"
	"server/pkg/github.com/name5566/leaf/log"
	"strconv"
)

const (
	CFG_BASE = "Base"
)

const (
	ENERGY_SHOP_MUL = "energy_shop_mul"
	ENERGY_CFG      = "BaseEnergy"
)

func init() {
	gamedata.InitCfg(CFG_BASE)
	gamedata.InitCfg(ENERGY_CFG)
}

func GetEnergyByMul(T int) float64 {
	data, err := gamedata.GetDataByKey(CFG_BASE, ENERGY_SHOP_MUL)
	if err != nil {
		log.Debug("GetEnergyByMul err:%v", err)
		return 0
	}
	d := data["Value"].(map[string]interface{})
	if len(d) == 0 {
		return 0
	}
	T = min(T, len(d))
	T1 := strconv.Itoa(T)
	if v, ok := d[T1]; ok {
		return v.(float64)
	} else {
		return 0
	}
}

func GetMaxEnergyMul(Lv int, Energy int) int {
	Mul := 0
	for i := 1; i < 999; i++ {
		data, err := gamedata.GetDataByIntKey(ENERGY_CFG, i)
		if err != nil {
			break
		}
		UnlockLv := gamedata.GetIntValue(data, "UnlockLv")
		if Lv < UnlockLv || Energy < gamedata.GetIntValue(data, "NeedEnergy") {
			break
		}
		Mul = i - 1
	}
	return Mul
}

func GetLimitEnergyMul(Lv int) int {
	data, err := gamedata.GetDataByIntKey(ENERGY_CFG, Lv+1)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Limit")
}
