package sevenLogin

import (
	"math/rand/v2"
	sevenLoginCfg "server/conf/seven_login"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/gamedata"
	"sort"
)

const (
	JACKPOT_CARD_TYPE = 2

	DEFAULT_REWARD = 0
	BACK_REWARD    = 1
)

func randWeekReward(Add, Type int) map[int]Reward {
	RewardList := sevenLoginCfg.GetSevenLoginReward()
	sort.Slice(RewardList, func(i, j int) bool {
		return RewardList[i].Id < RewardList[j].Id
	})
	jackpot := sevenLoginCfg.GetSevenLoginJackpot(0)
	sort.Slice(jackpot, func(i, j int) bool {
		return jackpot[i].Diamond < jackpot[j].Diamond
	})
	RI := make([]int, 0, len(RewardList))
	for _, v := range RewardList {
		RI = append(RI, v.Id)
	}
	CardPackIds := GoUtil.RandSliceNumNonAdjacent(RI, 3)
	result := make(map[int]Reward)
	LastType := 0
	EnergyMul := 1.0
	if Type == BACK_REWARD {
		EnergyMul = 2.0
	}
	for _, v := range RewardList {
		v.Diamond = int(float64(v.Diamond) * (1 + float64(Add)/100))
		Reward := randReward(v, LastType, jackpot, CardPackIds, 0.5, EnergyMul)
		result[v.Id] = Reward
	}
	return result
}

func randReward(RewardData *gamedata.SevenLoginRewardData, LastType int, jackpot []*gamedata.SevenLoginJackpotData, CardPackIds []int, EnergyPer, EnergyMul float64) Reward {
	// 奖励1 体力 体力等价钻石占总价值的50%，且1钻≈2.5体力；体力值四舍五入，需以0或5结尾
	Num := float64(RewardData.Diamond) * EnergyPer * 2.5
	Num = float64(int(Num/5+0.5)) * 5
	RemainDiamond := RewardData.Diamond - int(Num/2.5)
	Num = Num * EnergyMul
	Item1 := []*item.Item{item.NewItem(item.ITEM_ENERGY_ID, int(Num))}
	// 奖励2

	NewJackpot := make([]*gamedata.SevenLoginJackpotData, 0, len(jackpot))
	for _, v := range jackpot {
		if v.Diamond <= RemainDiamond && v.Type != LastType && v.Type != JACKPOT_CARD_TYPE {
			NewJackpot = append(NewJackpot, v)
		}
	}
	if len(NewJackpot) == 0 {
		return Reward{Item1: Item1}
	}
	var Item2 []*item.Item
	Item2Type := 0
	if RewardData.RewardNum == 3 {
		RemainDiamond -= 5
	}
	if GoUtil.InArray(RewardData.Id, CardPackIds) {
		CardJackpot := make([]*gamedata.SevenLoginJackpotData, 0, len(jackpot))
		for _, v := range jackpot {
			if v.Diamond <= RemainDiamond && v.Type == JACKPOT_CARD_TYPE {
				CardJackpot = append(CardJackpot, v)
			}
		}
		if len(CardJackpot) == 0 {
			Item2 = []*item.Item{item.NewItem(item.ITEM_DIAMOND_ID, RemainDiamond)}
		} else {
			Index := rand.IntN(len(CardJackpot))
			Item2Type = CardJackpot[Index].Type
			Item2 = CardJackpot[Index].Items
			RemainDiamond -= CardJackpot[Index].Diamond
		}
	} else {
		Index := rand.IntN(len(NewJackpot))
		Item2Type = NewJackpot[Index].Type
		Item2 = NewJackpot[Index].Items
		RemainDiamond -= NewJackpot[Index].Diamond
	}
	var Item3 []*item.Item
	if RewardData.RewardNum == 3 {
		RemainDiamond += 5
		NewJackpot3 := make([]*gamedata.SevenLoginJackpotData, 0, len(jackpot))
		for _, v := range jackpot {
			if v.Diamond <= RemainDiamond && v.Type != LastType && v.Type != Item2Type {
				NewJackpot3 = append(NewJackpot3, v)
			}
		}
		Index := rand.IntN(len(NewJackpot3))
		Item3 = NewJackpot3[Index].Items
	}

	return Reward{
		Item1: Item1,
		Item2: Item2,
		Item3: Item3,
	}
}

func randMonthReward() map[int]Reward {
	RewardList := sevenLoginCfg.GetSevenLoginMonthReward()
	sort.Slice(RewardList, func(i, j int) bool {
		return RewardList[i].Id < RewardList[j].Id
	})
	jackpot := sevenLoginCfg.GetSevenLoginJackpot(1)
	sort.Slice(jackpot, func(i, j int) bool {
		return jackpot[i].Diamond < jackpot[j].Diamond
	})
	result := make(map[int]Reward)
	for _, v := range RewardList {
		Reward := randReward(v, 0, jackpot, []int{}, 0.65, 1)
		result[v.Id] = Reward
	}
	return result
}
