package endlessCfg

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

const (
	CFG_ENDLESS         = "Endless"
	CFG_ENDLESS_JACKPOT = "EndlessJackpot"
)

func init() {
	gamedata.InitCfg(CFG_ENDLESS)
	gamedata.InitCfg(CFG_ENDLESS_JACKPOT)
}

func GetEnergy(Money float64) float64 {
	data, err := gamedata.GetData(CFG_ENDLESS)
	if err != nil {
		log.Debug("GetEnergy err:%v, Money=%.2f", err, Money)
		return 0
	}
	for _, v := range data {
		if Money == gamedata.GetFloatValue(v, "Money") {
			return gamedata.GetFloatValue(v, "Energy")
		}
	}
	return 0
}

func NextMoney(Money float64) float64 {
	data, err := gamedata.GetData(CFG_ENDLESS)
	if err != nil {
		log.Debug("NextMoney err:%v, Money=%.2f", err, Money)
		return 0
	}
	for _, v := range data {
		if Money == gamedata.GetFloatValue(v, "Money") {
			return gamedata.GetFloatValue(v, "NextMoney")
		}
	}
	return 0
}

func GetChargeId(Money float64) int {
	data, err := gamedata.GetData(CFG_ENDLESS)
	if err != nil {
		log.Debug("GetChargeId err:%v, Money=%.2f", err, Money)
		return 0
	}
	for _, v := range data {
		if Money == gamedata.GetFloatValue(v, "Money") {
			return gamedata.GetIntValue(v, "ChargeId")
		}
	}
	return 0
}

func GetPerEnergy(Id int) float64 {
	data, err := gamedata.GetData(CFG_ENDLESS_JACKPOT)
	if err != nil {
		log.Debug("GetPerEnergy err:%v, Id=%d", err, Id)
		return 0
	}
	for _, v := range data {
		if Id == gamedata.GetIntValue(v, "ItemId") {
			return gamedata.GetFloatValue(v, "Energy")
		}
	}
	return 0
}

func GetItemId(Energy float64, Type, Lv int) []int {
	data, err := gamedata.GetData(CFG_ENDLESS_JACKPOT)
	if err != nil {
		log.Debug("GetItemId err:%v, Energy=%.2f, Type=%d, Lv=%d", err, Energy, Type, Lv)
		return nil
	}
	ItemId := make([]int, 0, len(data))
	for _, v := range data {
		if Lv >= gamedata.GetIntValue(v, "Lv") && Energy >= gamedata.GetFloatValue(v, "Energy") && Type != gamedata.GetIntValue(v, "Type") && gamedata.GetIntValue(v, "ItemId") != item.ITEM_ENERGY_ID {
			ItemId = append(ItemId, gamedata.GetIntValue(v, "ItemId"))
		}
	}
	return ItemId
}

func GetType(ItemId int) int {
	data, err := gamedata.GetData(CFG_ENDLESS_JACKPOT)
	if err != nil {
		log.Debug("GetType err:%v, ItemId=%d", err, ItemId)
		return 0
	}
	for _, v := range data {
		if ItemId == gamedata.GetIntValue(v, "ItemId") {
			return gamedata.GetIntValue(v, "Type")
		}
	}
	return 0
}

func GetEnergyByItemId(ItemId int) float64 {
	data, err := gamedata.GetData(CFG_ENDLESS_JACKPOT)
	if err != nil {
		log.Debug("GetEnergyByItemId err:%v, ItemId=%d", err, ItemId)
		return 0
	}
	for _, v := range data {
		if ItemId == gamedata.GetIntValue(v, "ItemId") {
			return gamedata.GetFloatValue(v, "Energy")
		}
	}
	return 0
}
