package guesscolorCfg

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

const (
	CFG_GUESS_COLOR_TEMPLATE = "GuessColorTemplate"
	CFG_GUESS_COLOR_REWARD   = "GuessColorReward"
)

func init() {
	gamedata.InitCfg(CFG_GUESS_COLOR_TEMPLATE)
	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 {
		log.Debug("GetFirstPass err:%v, Id=%d", err, Id)
		return nil
	}
	str := gamedata.GetStringValue(data, "FirstPass")
	arr := strings.Split(str, "|")
	ret := make(map[int]int, len(arr))
	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 {
		log.Debug("GetLoseItem err:%v, Id=%d", err, Id)
		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 {
		log.Debug("GetFirstItem err:%v, Id=%d", err, Id)
		return nil
	}
	return gamedata.GetItemList(data, "FirstItem")
}

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

func GetRewardItem(Id int, Type int) []*item.Item {
	data, err := gamedata.GetData(CFG_GUESS_COLOR_REWARD)
	if err != nil {
		log.Debug("GetRewardItem err:%v, Id=%d, Type=%d", err, Id, Type)
		return nil
	}
	for k, v := range data {
		min := GoUtil.Int(k)
		max := gamedata.GetIntValue(v, "Max")
		if Id >= min && Id <= max {
			if Type == 1 {
				return gamedata.GetItemList(v, "Items1")
			}
			return gamedata.GetItemList(v, "Items2")
		}
	}
	return nil
}

func GetPassNum(Id int) int {
	data, err := gamedata.GetData(CFG_GUESS_COLOR_REWARD)
	if err != nil {
		log.Debug("GetPassNum err:%v, Id=%d", err, Id)
		return 0
	}
	for k, v := range data {
		min := GoUtil.Int(k)
		max := gamedata.GetIntValue(v, "Max")
		if Id >= min && Id <= max {
			return gamedata.GetIntValue(v, "Num")
		}
	}
	return 0
}

func GetWinTime(Id int) int {
	data, err := gamedata.GetData(CFG_GUESS_COLOR_REWARD)
	if err != nil {
		log.Debug("GetWinTime err:%v, Id=%d", err, Id)
		return 0
	}
	for k, v := range data {
		min := GoUtil.Int(k)
		max := gamedata.GetIntValue(v, "Max")
		if Id >= min && Id <= max {
			Type := gamedata.GetIntValue(v, "Type")
			if Type == 1 {
				return gamedata.GetIntValue(v, "Win")
			}
			str := gamedata.GetStringValue(v, "Win")
			arr := strings.Split(str, "|")
			return GoUtil.RandNum(GoUtil.Int(arr[0]), GoUtil.Int(arr[1]))
		}
	}
	return 0
}
