package catnipCfg

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

const (
	CATNIP_TEMPLATE_CFG_NAME   = "CatnipTemplate"
	CATNIP_JACKPOT_CFG_NAME    = "CatnipJackpot"
	CATNIP_MULTIPLIER_CFG_NAME = "CatnipMultiplier"
	CATNIP_GAME_CFG_NAME       = "CatnipGame"
)

const (
	CATNIP_REWARD_TYPE_LOW  = 1
	CATNIP_REWARD_TYPE_MID  = 2
	CATNIP_REWARD_TYPE_HIGH = 3
)

func init() {
	gamedata.InitCfg(CATNIP_TEMPLATE_CFG_NAME)
	gamedata.InitCfg(CATNIP_JACKPOT_CFG_NAME)
	gamedata.InitCfg(CATNIP_MULTIPLIER_CFG_NAME)
	gamedata.InitCfg(CATNIP_GAME_CFG_NAME)
}

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

func GetGameMaxProgress(Id int) int {
	TemplateId := GetTemplateId(Id)
	data, err := gamedata.GetData(CATNIP_GAME_CFG_NAME)
	if err != nil {
		log.Debug("GetGameMaxProgress err:%v, Id=%d", err, Id)
		return 0
	}
	MaxProgress := 0
	for _, v := range data {
		if gamedata.GetIntValue(v, "Template") != TemplateId {
			continue
		}
		MaxProgress = max(MaxProgress, gamedata.GetIntValue(v, "Need"))
	}
	return MaxProgress
}

func GetJackpotItem(Mul int) (int, []*item.Item, int, int) {
	data, err := gamedata.GetData(CATNIP_JACKPOT_CFG_NAME)
	if err != nil {
		log.Debug("GetJackpotItem err:%v, Mul=%d", err, Mul)
		return 0, nil, 0, 0
	}
	JackpotType := GetJackpotType(Mul)
	r := make(map[int]int, len(data))
	for k, v := range data {
		if gamedata.GetIntValue(v, "Type") == JackpotType {
			r[GoUtil.Int(k)] = 1
		}
	}
	Id := GoUtil.RandMap(r)
	if Id == 0 {
		return 0, nil, 0, 0
	}
	itemData, err := gamedata.GetDataByIntKey(CATNIP_JACKPOT_CFG_NAME, Id)
	if err != nil {
		log.Debug("GetJackpotItem get itemData err:%v, Id=%d", err, Id)
		return 0, nil, 0, 0
	}
	return Id, gamedata.GetItemList(itemData, "Items"), gamedata.GetIntValue(itemData, "Growth"), gamedata.GetIntValue(itemData, "FriendItems")
}

func GetJackpotType(Mul int) int {
	data, err := gamedata.GetDataByIntKey(CATNIP_MULTIPLIER_CFG_NAME, Mul)
	if err != nil {
		log.Debug("GetJackpotType err:%v, Mul=%d", err, Mul)
		return 0
	}
	R := map[int]int{
		CATNIP_REWARD_TYPE_LOW:  gamedata.GetIntValue(data, "Low"),
		CATNIP_REWARD_TYPE_MID:  gamedata.GetIntValue(data, "Mid"),
		CATNIP_REWARD_TYPE_HIGH: gamedata.GetIntValue(data, "High"),
	}
	return GoUtil.RandMap(R)
}

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

func GetProgressReward(Id int, Rewards []int, Progress int) ([]*item.Item, []int) {
	TemplateId := GetTemplateId(Id)
	data, err := gamedata.GetData(CATNIP_GAME_CFG_NAME)
	if err != nil {
		log.Debug("GetProgressReward err:%v, Id=%d", err, Id)
		return nil, nil
	}
	Items := make([]*item.Item, 0, len(data))
	Ids := make([]int, 0, len(data))
	for k, v := range data {
		rewardId := GoUtil.Int(k)
		if !GoUtil.InArray(rewardId, Rewards) && gamedata.GetIntValue(v, "Need") < Progress && gamedata.GetIntValue(v, "Template") == TemplateId {
			Items = append(Items, gamedata.GetItemList(v, "Items")...)
			Ids = append(Ids, rewardId)
		}
	}
	return Items, Ids
}

func GetProgressNum(Id int) int {
	TemplateId := GetTemplateId(Id)
	data, err := gamedata.GetData(CATNIP_GAME_CFG_NAME)
	if err != nil {
		log.Debug("GetProgressNum err:%v, Id=%d", err, Id)
		return 0
	}
	Num := 0
	for _, v := range data {
		if gamedata.GetIntValue(v, "Template") == TemplateId {
			Num++
		}
	}
	return Num
}

func GetItemCost(Id, Mul int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CATNIP_TEMPLATE_CFG_NAME, Id)
	if err != nil {
		log.Debug("GetItemCost err:%v, Id=%d", err, Id)
		return nil
	}
	return item.MutilItem(gamedata.GetItemList(data, "ItemCost"), Mul)
}

func GetGrandReward(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CATNIP_TEMPLATE_CFG_NAME, Id)
	if err != nil {
		log.Debug("GetGrandReward err:%v, Id=%d", err, Id)
		return nil
	}
	return gamedata.GetItemList(data, "Reward")
}
