package sevenLoginCfg

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

const (
	CFG_SEVEN_LOGIN         = "SevenLogin"
	CFG_SEVEN_LOGIN_JACKPOT = "SevenLoginJackpot"
	CFG_SEVEN_LOGIN_MONTH   = "SevenLoginMonth"
)

func init() {
	gamedata.InitCfg(CFG_SEVEN_LOGIN)
	gamedata.InitCfg(CFG_SEVEN_LOGIN_JACKPOT)
	gamedata.InitCfg(CFG_SEVEN_LOGIN_MONTH)
}

func GetSevenLoginReward() []*gamedata.SevenLoginRewardData {
	data, err := gamedata.GetData(CFG_SEVEN_LOGIN)
	if err != nil {
		log.Debug("GetSevenLoginReward err:%v", err)
		return nil
	}
	var result []*gamedata.SevenLoginRewardData
	for k, v := range data {
		Id, _ := strconv.Atoi(k)
		Diamond := gamedata.GetIntValue(v, "Diamond")
		RewardNum := gamedata.GetIntValue(v, "RewardNum")
		result = append(result, &gamedata.SevenLoginRewardData{
			Id:        Id,
			Diamond:   Diamond,
			RewardNum: RewardNum,
		})
	}
	return result
}

func GetSevenLoginMonthReward() []*gamedata.SevenLoginRewardData {
	data, err := gamedata.GetData(CFG_SEVEN_LOGIN_MONTH)
	if err != nil {
		log.Debug("GetSevenLoginReward err:%v", err)
		return nil
	}
	var result []*gamedata.SevenLoginRewardData
	for k, v := range data {
		Id, _ := strconv.Atoi(k)
		Diamond := gamedata.GetIntValue(v, "Diamond")
		RewardNum := gamedata.GetIntValue(v, "RewardNum")
		result = append(result, &gamedata.SevenLoginRewardData{
			Id:        Id,
			Diamond:   Diamond,
			RewardNum: RewardNum,
		})
	}
	return result
}

func GetSevenLoginJackpot(IsMonth int) []*gamedata.SevenLoginJackpotData {
	data, err := gamedata.GetData(CFG_SEVEN_LOGIN_JACKPOT)
	if err != nil {
		log.Debug("GetSevenLoginJackpot err:%v", err)
		return nil
	}
	var result []*gamedata.SevenLoginJackpotData
	for k, v := range data {
		Id, _ := strconv.Atoi(k)
		Diamond := gamedata.GetIntValue(v, "Diamond")
		Type := gamedata.GetIntValue(v, "Type")
		Month := gamedata.GetIntValue(v, "Month")
		if IsMonth != Month && IsMonth != 0 {
			continue
		}
		ItemMap := gamedata.GetValue(v, "Item")
		Items := item.ParseItem(ItemMap)
		result = append(result, &gamedata.SevenLoginJackpotData{
			Id:      Id,
			Diamond: Diamond,
			Items:   Items,
			Type:    Type,
			Month:   Month,
		})
	}
	return result
}

func GetMonthActive(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_SEVEN_LOGIN_MONTH, Id)
	if err != nil {
		log.Debug("GetSevenLoginReward err:%v", err)
		return int(math.Inf(1))
	}
	return gamedata.GetIntValue(data, "Active")
}
