package guesscolorCfg

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

const (
	CFG_GUESS_COLOR_TEMPLATE = "GuessColorTemplate"
	CFG_GUESS_COLOR_PASS     = "GuessColorPass"
	CFE_GUESS_COLOR_JACKPOT  = "GuessColorJackpot"
	CFG_GUESS_COLOR_REWARD   = "GuessColorReward"
)

func init() {
	gamedata.InitCfg(CFG_GUESS_COLOR_TEMPLATE)
	gamedata.InitCfg(CFG_GUESS_COLOR_PASS)
	gamedata.InitCfg(CFE_GUESS_COLOR_JACKPOT)
	gamedata.InitCfg(CFG_GUESS_COLOR_REWARD)
}

func GetFirstPass(Id int) map[int]int {
	data, err := gamedata.GetDataByIntKey(CFG_GUESS_COLOR_TEMPLATE, Id)
	if err != nil {
		return nil
	}
	str := gamedata.GetStringValue(data, "FirstPass")
	arr := strings.Split(str, "|")
	ret := make(map[int]int)
	for k, v := range arr {
		ret[k+1] = GoUtil.Int(v)
	}
	return ret
}

func GetLoseItem(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_GUESS_COLOR_TEMPLATE, Id)
	if err != nil {
		return nil
	}
	return gamedata.GetItemList(data, "ItemCost")
}

func GetFirstItem(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_GUESS_COLOR_TEMPLATE, Id)
	if err != nil {
		return nil
	}
	return gamedata.GetItemList(data, "FirstItem")
}

func GetActivityItemId(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_GUESS_COLOR_TEMPLATE, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "ItemId")
}

func GetRewardItem(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_GUESS_COLOR_REWARD, Id)
	if err != nil {
		return nil
	}
	return gamedata.GetItemList(data, "Items")
}

func GetJackpot() []int {
	data, err := gamedata.GetData(CFE_GUESS_COLOR_JACKPOT)
	if err != nil {
		return []int{}
	}
	result := make([]int, 0)
	for k := range data {
		result = append(result, GoUtil.Int(k))
	}
	return result
}

func GetPassNum(Pass int) int {
	data, err := gamedata.GetData(CFG_GUESS_COLOR_PASS)
	if err != nil {
		return 0
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "Min") <= Pass && gamedata.GetIntValue(v, "Max") >= Pass {
			return gamedata.GetIntValue(v, "Num")
		}
	}
	return 0
}
