package miningCfg

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

const (
	CFG_MINING_TEMPLATE = "MiningTemplate"
	CFG_MINING_JACKPOT  = "MiningJackpot"
	CFG_MINING_GEM      = "MiningGem"
	CFG_MINING_PASS     = "MiningPass"
)

func init() {
	gamedata.InitCfg(CFG_MINING_TEMPLATE)
	gamedata.InitCfg(CFG_MINING_JACKPOT)
	gamedata.InitCfg(CFG_MINING_GEM)
	gamedata.InitCfg(CFG_MINING_PASS)
}

func GetTemplate(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_MINING_TEMPLATE, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Template")
}

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

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

func GetPassArea(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_MINING_PASS, Id)
	if err != nil {
		return 0
	}
	Area := gamedata.GetStringValue(data, "Area")
	strArr := strings.Split(Area, "*")
	a, _ := strconv.Atoi(strArr[0])
	b, _ := strconv.Atoi(strArr[1])
	return a * b
}

func GetPassGem(Id int) []int {
	data, err := gamedata.GetDataByIntKey(CFG_MINING_PASS, Id)
	if err != nil {
		return nil
	}
	Gem := gamedata.GetStringValue(data, "Gem")
	strArr := strings.Split(Gem, "|")
	result := make([]int, 0, len(strArr))
	for _, v := range strArr {
		a, _ := strconv.Atoi(v)
		result = append(result, a)
	}
	return result
}

func GetRandItem() []*item.Item {
	data, err := gamedata.GetData(CFG_MINING_JACKPOT)
	if err != nil {
		return nil
	}
	ProbMap := make(map[int]int)
	for k, v := range data {
		Id := GoUtil.Int(k)
		ProbMap[Id] = gamedata.GetIntValue(v, "Prob")
	}
	RandId := GoUtil.RandMap(ProbMap)
	Info, _ := gamedata.GetDataByIntKey(CFG_MINING_JACKPOT, RandId)
	return gamedata.GetItemList(Info, "Items")
}

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