package friendTreasureCfg

import (
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/gamedata"
)

const (
	CFG_FRIEND_TREASURE_PROB  = "FriendTreasureProb"
	CFG_FRIEND_TREASURE_CHEST = "FriendTreasureChest"
	CFG_FRIEND_TREASURE_CONST = "FriendTreasureConst"
)

func init() {
	gamedata.InitCfg(CFG_FRIEND_TREASURE_PROB)
	gamedata.InitCfg(CFG_FRIEND_TREASURE_CHEST)
	gamedata.InitCfg(CFG_FRIEND_TREASURE_CONST)
}

func GetBigReward() []*item.Item {
	data, err := gamedata.GetDataByKey(CFG_FRIEND_TREASURE_CONST, "big_reward")
	if err != nil {
		return nil
	}
	return gamedata.GetItemList(data, "Value")
}

func GetFriendTreasureProb(Num int) map[int]int {
	data, err := gamedata.GetData(CFG_FRIEND_TREASURE_PROB)
	if err != nil {
		return nil
	}
	ProbMap := make(map[int]int)
	for k, v := range data {
		Id := GoUtil.Int(k)
		Prob := gamedata.GetIntValue(v, "Prob")
		ProbMap[Id] = Prob
	}
	AddProb := 0
	n := 0
	for k, v := range ProbMap {
		if k > Num {
			AddProb += v
			n++
			ProbMap[k] = 0
		}
	}
	PerProb := AddProb / n
	for k, v := range ProbMap {
		if k <= Num && k != 1 {
			ProbMap[k] = v + PerProb
		}
	}
	return ProbMap
}

func GetProbAdd(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_FRIEND_TREASURE_PROB, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Add")
}

func GetChestProb() map[int]int {
	data, err := gamedata.GetData(CFG_FRIEND_TREASURE_CHEST)
	if err != nil {
		return nil
	}
	ProbMap := make(map[int]int)
	for k, v := range data {
		Id := GoUtil.Int(k)
		Prob := gamedata.GetIntValue(v, "Prob")
		ProbMap[Id] = Prob
	}
	return ProbMap
}

func GetChestItems(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_FRIEND_TREASURE_CHEST, Id)
	if err != nil {
		return nil
	}

	return gamedata.GetItemList(data, "Items")
}
