package cardCfg

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

const (
	CARD_CONST_CFG_NAME   = "CardConst"
	CARD_DETAIL_CFG_NAME  = "CardDetail"
	CARD_PACK_CFG_NAME    = "CardPack"
	CARD_COLLECT_CFG_NAME = "CardCollect"
	CARD_EXCHANGE_CFG     = "CardExchange"
)

func init() {
	gamedata.InitCfg(CARD_CONST_CFG_NAME)
	gamedata.InitCfg(CARD_DETAIL_CFG_NAME)
	gamedata.InitCfg(CARD_PACK_CFG_NAME)
	gamedata.InitCfg(CARD_COLLECT_CFG_NAME)
	gamedata.InitCfg(CARD_EXCHANGE_CFG)
}

func GetAllCollectReward() []*item.Item {
	data, err := gamedata.GetDataByKey(CARD_CONST_CFG_NAME, "all_collect_reward")
	if err != nil {
		log.Debug("GetAllCollectReward err:%v", err)
		return nil
	}
	if value, ok := data["Value"]; ok {
		return item.ParseItem(value)
	}
	return nil
}
func GetStarById(Id int) int {
	data, err := gamedata.GetDataByIntKey(CARD_DETAIL_CFG_NAME, Id)
	if err != nil {
		log.Debug("GetStarById err:%v, Id=%d", err, Id)
		return 0
	}
	return gamedata.GetIntValue(data, "Star")
}

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

// 获取开启卡包获得的卡牌数量
func GetPackRewardCnt(Star int) int {
	data, err := gamedata.GetDataByIntKey(CARD_PACK_CFG_NAME, Star)
	if err != nil {
		log.Debug("GetPackRewardCnt err:%v, Star=%d", err, Star)
		return 0
	}
	return gamedata.GetIntValue(data, "RewardCnt")
}

// 获取开启开包必得x星卡
func GetPackMustHave(Star int) int {
	data, err := gamedata.GetDataByIntKey(CARD_PACK_CFG_NAME, Star)
	if err != nil {
		log.Debug("GetPackMustHave err:%v, Star=%d", err, Star)
		return 0
	}
	return gamedata.GetIntValue(data, "MustHave")
}

// 根据星级获取卡牌列表
func GetCardListByStar(Round, Star, IsGold int) []int {
	data, err := gamedata.GetData(CARD_DETAIL_CFG_NAME)
	if err != nil {
		log.Debug("GetCardListByStar err:%v", err)
		return nil
	}
	CardList := make([]int, 0, len(data)/10)
	for k, v := range data {
		Extra := gamedata.GetIntValue(v, "Round")
		if Round < Extra {
			continue
		}
		vStar := gamedata.GetIntValue(v, "Star")
		isGold := gamedata.GetIntValue(v, "IsGold")
		if vStar == Star && IsGold == isGold {
			id, err := strconv.Atoi(k)
			if err != nil {
				log.Debug("GetCardListByStar parse id err:%v, key=%s", err, k)
				continue
			}
			CardList = append(CardList, id)
		}
	}
	return CardList
}

// 获取随机卡牌
func GetRandListByStar(Star int) map[int]int {
	data, err := gamedata.GetDataByIntKey(CARD_PACK_CFG_NAME, Star)
	if err != nil {
		log.Debug("GetRandListByStar err:%v, Star=%d", err, Star)
		return nil
	}
	r1 := gamedata.GetStringValue(data, "RandRate")
	r2 := strings.Split(r1, ",")
	randList := GoUtil.SliceStringToInt(r2)
	r := make(map[int]int, len(randList))
	for k, v := range randList {
		r[k+1] = v
	}
	return r
}

// 获取卡牌收集奖励
func GetCollectReward(Color int) ([]*item.Item, int) {
	data, err := gamedata.GetDataByIntKey(CARD_COLLECT_CFG_NAME, Color)
	if err != nil {
		log.Debug("GetCollectReward data not found")
		return nil, 0
	}
	itemCfg := gamedata.GetValue(data, "Item")
	Chess := gamedata.GetIntValue(data, "Chess")
	itemList := item.ParseItem(itemCfg)
	return itemList, Chess
}

// 获取兑换奖励
func GetExchangeCfg(Id int) (int, []*item.Item) {
	data, err := gamedata.GetDataByIntKey(CARD_EXCHANGE_CFG, Id)
	if err != nil {
		log.Debug("GetExchangeCfg err:%v, Id=%d", err, Id)
		return 0, nil
	}
	CostStar := gamedata.GetIntValue(data, "CostStar")
	itemList := gamedata.GetItemList(data, "Item")
	return CostStar, itemList
}

// 根据系列获取卡牌列表
func GetCardListByColor(Id int) []int {
	data, err := gamedata.GetData(CARD_DETAIL_CFG_NAME)
	if err != nil {
		log.Debug("GetCardListByColor err:%v, Id=%d", err, Id)
		return nil
	}
	r := make([]int, 0, len(data)/5)
	for k, v := range data {
		color := gamedata.GetIntValue(v, "PictureAlbum")
		if color == Id {
			k1, err := strconv.Atoi(k)
			if err != nil {
				log.Debug("GetCardListByColor parse id err:%v, key=%s", err, k)
				continue
			}
			r = append(r, k1)
		}
	}
	return r
}

// 获取所有卡牌id
func GetAllCardId(Round int) []int {
	data, err := gamedata.GetData(CARD_DETAIL_CFG_NAME)
	if err != nil {
		log.Debug("GetAllCardId err:%v, Round=%d", err, Round)
		return nil
	}
	r := make([]int, 0, len(data))
	for k, v := range data {
		Extra := gamedata.GetIntValue(v, "Round")
		if Round < Extra {
			continue
		}
		k1, err := strconv.Atoi(k)
		if err != nil {
			log.Debug("GetAllCardId parse id err:%v, key=%s", err, k)
			continue
		}
		r = append(r, k1)
	}
	return r
}

func CheckCardIsGold(Id int) bool {
	data, err := gamedata.GetDataByIntKey(CARD_DETAIL_CFG_NAME, Id)
	if err != nil {
		log.Debug("CheckCardIsGold err:%v, Id=%d", err, Id)
		return false
	}
	return gamedata.GetIntValue(data, "IsGold") == 1
}

func GetCardDuration() int64 {
	data, err := gamedata.GetDataByKey(CARD_CONST_CFG_NAME, "duration")
	if err != nil {
		log.Debug("GetCardDuration err:%v", err)
		return 0
	}
	return int64(gamedata.GetIntValue(data, "Value"))
}

func GetExTimes() int {
	data, err := gamedata.GetDataByKey(CARD_CONST_CFG_NAME, "exchange_times")
	if err != nil {
		log.Debug("GetExTimes err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetReqTimes() int {
	data, err := gamedata.GetDataByKey(CARD_CONST_CFG_NAME, "request_times")
	if err != nil {
		log.Debug("GetReqTimes err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

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

func GetSeasonFirstReward() []*item.Item {
	data, err := gamedata.GetDataByKey(CARD_CONST_CFG_NAME, "season_first")
	if err != nil {
		log.Debug("GetSeasonFirstReward err:%v", err)
		return nil
	}
	if value, ok := data["Value"]; ok {
		return item.ParseItem(value)
	}
	return nil
}
