package champshipCfg

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

const (
	CFG_CHAMPSHIP_SCORE   = "ChampshipScore"
	CFG_CHAMPSHIP_RANK    = "ChampshipRank"
	CFG_CHAMPSHIP_JACKPOT = "ChampshipJackpot"
	CFG_CHAMPSHIP_GROUP   = "ChampshipGroup"
)

func init() {
	gamedata.InitCfg(CFG_CHAMPSHIP_SCORE)
	gamedata.InitCfg(CFG_CHAMPSHIP_RANK)
	gamedata.InitCfg(CFG_CHAMPSHIP_JACKPOT)
	gamedata.InitCfg(CFG_CHAMPSHIP_GROUP)
}

func GetGroupId(N int) int {
	data, err := gamedata.GetData(CFG_CHAMPSHIP_GROUP)
	if err != nil {
		return 0
	}
	for k, v := range data {
		if N >= gamedata.GetIntValue(v, "Min") && N <= gamedata.GetIntValue(v, "Max") {
			return GoUtil.Int(k)
		}
	}
	return 0
}

func GetChessScore(ChessLv int) int {
	data, err := gamedata.GetDataByIntKey(CFG_CHAMPSHIP_SCORE, ChessLv)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Score")
}

func GetReward(Reward, Score int) (int, []*item.Item) {
	data, err := gamedata.GetData(CFG_CHAMPSHIP_JACKPOT)
	if err != nil {
		return Reward, nil
	}
	r := make([]*item.Item, 0)
	NewReward := Reward
	for k, v := range data {
		Id := GoUtil.Int(k)
		if Reward >= Id {
			continue
		}
		if Score >= gamedata.GetIntValue(v, "Total") {
			Items := item.ParseItem(gamedata.GetValue(v, "Items"))
			r = item.Merge(r, Items)
			NewReward = max(NewReward, Id)
		}
	}
	return NewReward, r
}

func GetRankReward(Rank int) []*item.Item {
	data, err := gamedata.GetData(CFG_CHAMPSHIP_RANK)
	if err != nil {
		return nil
	}
	for _, v := range data {
		if Rank >= gamedata.GetIntValue(v, "Min") && Rank <= gamedata.GetIntValue(v, "Max") {
			return gamedata.GetItemList(v, "Items")
		}
	}
	return nil
}
