package collect

import (
	"fmt"
	"server/GoUtil"
	collectCfg "server/conf/collect"
	"server/game/mod/item"
	"server/msg"
)

type Collect struct {
	Reward  []int // 已领取记录
	Jackpot []int // 奖池
}

type CollectInfo struct {
	List map[int]int
}

const (
	COLLECT_TYPE_EMOJI  = 1
	COLLECT_TYPE_FACE   = 2
	COLLECT_TYPE_AVATAR = 3
)

func (c *Collect) InitData() {
	if c.Reward == nil {
		c.Reward = make([]int, 0)
	}
	if len(c.Jackpot) == 0 {
		c.Jackpot = initJackpot()
	}
	if len(c.Jackpot) <= 18 {
		c.Jackpot = initJackpot2(c.Jackpot)
	}
}

func (c *Collect) GetReward(Id, Num int) ([]*item.Item, error) {
	// 判断是否已领取
	for _, v := range c.Reward {
		if v == Id {
			return nil, fmt.Errorf("已领取")
		}
	}
	Need := collectCfg.GetRewardNeed(Id)
	if Num < Need {
		return nil, fmt.Errorf("收集数量不足")
	}
	// 领取奖励
	c.Reward = append(c.Reward, Id)
	JackpotId := 0
	JackpotId, c.Jackpot = GoUtil.PopSlice(c.Jackpot)
	if len(c.Jackpot) <= 18 {
		c.Jackpot = initJackpot2(c.Jackpot)
	}
	Items := collectCfg.GetJackpotItems(JackpotId)
	// 删除收集记录
	return Items, nil
}

func (c *Collect) BackData() *msg.ResCollectInfo {
	var ItemsMsg []*msg.CollectItem
	for i := 0; i < 18; i++ {
		Items := collectCfg.GetJackpotItems(c.Jackpot[i])
		ItemsMsg = append(ItemsMsg, &msg.CollectItem{
			Id:    int32(i + 1),
			Items: item.ItemToMsg(Items),
		})
	}
	return &msg.ResCollectInfo{
		Id:    GoUtil.IntToInt32(c.Reward),
		Items: ItemsMsg,
	}
}

func initJackpot2(r1 []int) []int {
	Ids := collectCfg.GetJackpotId()
	r2 := GoUtil.ShuffleArray(Ids)
	return append(r1, r2...)
}

func initJackpot() []int {
	Ids := collectCfg.GetJackpotId()
	r1 := GoUtil.ShuffleArray(Ids)
	r2 := GoUtil.ShuffleArray(Ids)
	r3 := GoUtil.ShuffleArray(Ids)
	return append(append(r1, r2...), r3...)
}
