package limitedTimeEventCfg

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

const (
	CFG_LIMITED_TIME_EVENT               = "LimitedTimeEvent"
	CFG_LIMITED_TIME_EVENT_METEOR        = "LimitedTimeEventMeteor"
	CFG_LIMITED_TIME_EVENT_CHEST         = "LimitedTimeEventChest"
	CFG_LIMITED_TIME_EVENT_ORDER         = "LimitedTimeEventOrder"
	CFG_LIMITED_TIME_EVENT_SENCE         = "LimitedTimeEventSence"
	CFG_LIMITED_TIME_EVENT_SENCE_JACKPOT = "LimitedTimeEventSenceJackpot"
	CFG_LIMITED_TIME_EVENT_FAST          = "LimitedTimeEventFast"
	CFG_LIMITED_TIME_EVENT_JACKPOT       = "LimitedTimeEventJackpot"
	CFG_LIMITED_TIME_EVENT_CHEST_JACKPOT = "LimitedTimeEventChestJackpot"
	CFG_LIMITED_TIME_EVENT_CONST         = "LimitedTimeEventConst"
	CFG_LIMITED_TIME_EVENT_BONUS         = "LimitedTimeEventBonus"
)

func init() {
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_METEOR)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_CHEST)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_ORDER)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_SENCE)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_SENCE_JACKPOT)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_FAST)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_JACKPOT)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_CONST)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_BONUS)
	gamedata.InitCfg(CFG_LIMITED_TIME_EVENT_CHEST_JACKPOT)
}

// 获取限时事件触发列表
func GetLimitedTimeEventCfg() []*gamedata.LimitedTimeEventData {
	return nil
}

// 获取流星雨加成
func GetMeteorAdd(ChessLv int) int {
	data, err := gamedata.GetDataByIntKey(CFG_LIMITED_TIME_EVENT_METEOR, ChessLv)
	if err != nil {
		log.Debug("GetMeteorAdd err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Add")
}

// 获取宝箱雨奖励
func GetChestReward(Star int) []*item.Item {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_CHEST)
	if err != nil {
		log.Debug("GetChestReward err:%v", err)
		return nil
	}

	for _, v := range data {
		Min := gamedata.GetIntValue(v, "Min")
		Max := gamedata.GetIntValue(v, "Max")
		if Star >= Min && Star <= Max {
			return gamedata.GetItemList(v, "Items")
		}
	}
	return nil

}

// 获取超级订单奖励
func GetSuperOrderReward(Star int) []*item.Item {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_ORDER)
	if err != nil {
		log.Debug("GetSuperOrderReward err:%v", err)
		return nil
	}
	for _, v := range data {
		Min := gamedata.GetIntValue(v, "Min")
		Max := gamedata.GetIntValue(v, "Max")
		if Star >= Min && Star <= Max {
			return gamedata.GetItemList(v, "Items")
		}
	}
	return nil
}

// 获取场景冲刺奖励
func GetSceneDashReward(Sence, Progress int) (int, []*item.Item) {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_SENCE)
	if err != nil {
		log.Debug("GetSceneDashReward err:%v", err)
		return 0, nil
	}
	for k, v := range data {
		if Sence == gamedata.GetIntValue(v, "Sence") && Progress == gamedata.GetIntValue(v, "Progress") {
			return GoUtil.Int(k), gamedata.GetItemList(v, "Items")
		}
	}
	return 0, nil
}

// 获取场景冲刺奖池
func GetSenceJackpotProb() map[int]int {
	r := make(map[int]int)
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_SENCE_JACKPOT)
	if err != nil {
		log.Debug("GetSenceJackpotReward err:%v", err)
		return nil
	}
	for k, v := range data {
		Id := GoUtil.Int(k)
		r[Id] = gamedata.GetIntValue(v, "Prob")
	}
	return r
}

// 获取场景冲刺奖励
func GetSenceJackpotReward(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_LIMITED_TIME_EVENT_SENCE_JACKPOT, Id)
	if err != nil {
		log.Debug("GetSenceJackpotReward err:%v", err)
		return nil
	}
	return gamedata.GetItemList(data, "Items")
}

// 获取连击快手奖励
func GetFastProduceReward(Times, Energy int) []*item.Item {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_FAST)
	if err != nil {
		log.Debug("GetSceneDashReward err:%v", err)
		return nil
	}
	for _, v := range data {
		if Times == gamedata.GetIntValue(v, "Times") && Energy <= gamedata.GetIntValue(v, "Max") && Energy >= gamedata.GetIntValue(v, "Min") {
			return gamedata.GetItemList(v, "Items")
		}
	}
	return nil
}

// 获取连击快手最大次数
func GetFastProduceMaxTimes() int {
	Max := 0
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_FAST)
	if err != nil {
		log.Debug("GetSceneDashReward err:%v", err)
		return 0
	}
	for _, v := range data {
		Max = max(gamedata.GetIntValue(v, "Times"), Max)
	}
	return Max
}

// 获取进度最大值
func GetProgressMax(Lv, Num int) int {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_BONUS)
	if err != nil {
		log.Debug("GetProgressMax err:%v", err)
		return 0
	}
	for _, v := range data {
		Min := gamedata.GetIntValue(v, "Min")
		Max := gamedata.GetIntValue(v, "Max")
		if Lv >= Min && Lv <= Max {
			OrderNumStr := gamedata.GetStringValue(v, "OrderNum")
			OrderNum := GoUtil.SplitInt(OrderNumStr, ",")
			if Num >= len(OrderNum) {
				return OrderNum[len(OrderNum)-1]
			} else {
				return OrderNum[Num]
			}
		}
	}
	return 0
}

func GetBonusLv(Lv int) int {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_BONUS)
	if err != nil {
		log.Debug("GetProgressMax err:%v", err)
		return 0
	}
	for k, v := range data {
		Min := gamedata.GetIntValue(v, "Min")
		Max := gamedata.GetIntValue(v, "Max")
		if Lv >= Min && Lv <= Max {
			return GoUtil.Int(k)
		}
	}
	return 0
}

// 获取进度奖励随机
func GetProgressRewardRand(Lv int) map[int]int {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_JACKPOT)
	if err != nil {
		log.Debug("GetProgressRewardRand err:%v", err)
		return nil
	}
	r := make(map[int]int)
	for k, v := range data {
		if Lv >= gamedata.GetIntValue(v, "BonusLv") {
			Id := GoUtil.Int(k)
			r[Id] = gamedata.GetIntValue(v, "Prob")
		}
	}
	return r
}

// 获取进度选择数量
func GetProgressSelectNum(Lv int) int {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_BONUS)
	if err != nil {
		log.Debug("GetProgressMax err:%v", err)
		return 0
	}
	for _, v := range data {
		Min := gamedata.GetIntValue(v, "Min")
		Max := gamedata.GetIntValue(v, "Max")
		if Lv >= Min && Lv <= Max {
			return gamedata.GetIntValue(v, "Option")
		}
	}
	return 0
}

// 获取进度奖励
func GetProgressReward(RewardId int) []*item.Item {
	data, err := gamedata.GetData(CFG_LIMITED_TIME_EVENT_JACKPOT)
	if err != nil {
		log.Debug("GetProgressReward err:%v", err)
		return nil
	}
	for k, v := range data {
		Id := GoUtil.Int(k)
		if Id == RewardId {
			return gamedata.GetItemList(v, "Items")
		}
	}
	return nil
}

// 获取进度奖励类型
func GetProgressRewardType(RewardId int) int {
	data, err := gamedata.GetDataByIntKey(CFG_LIMITED_TIME_EVENT_JACKPOT, RewardId)
	if err != nil {
		log.Debug("GetProgressReward err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Type")
}

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

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

func GetFastCD() int {
	data, err := gamedata.GetDataByKey(CFG_LIMITED_TIME_EVENT_CONST, "Fast_Cd")
	if err != nil {
		log.Debug("GetFirstEvent err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}
