package invite

import (
	"fmt"
	inviteCfg "server/conf/invite"
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/msg"
)

type InviteMod struct {
	InviteList map[int]struct{} // 邀请列表
	GetIndex   int
}

func (i *InviteMod) InitData() {
	if i.InviteList == nil {
		i.InviteList = make(map[int]struct{})
	}
}

func (i *InviteMod) AddInvite(id int) {
	i.InviteList[id] = struct{}{}
}

func (i *InviteMod) GetReward(Index int) ([]*item.Item, error) {
	if Index <= i.GetIndex {
		return nil, fmt.Errorf("index error")
	}
	var Items []*item.Item
	cur := i.GetIndex
	for cur < Index {
		cur++
		Need, Reward := inviteCfg.GetInviteReward(cur)
		if Need == 0 {
			return nil, fmt.Errorf("invite reward not found")
		}
		if len(i.InviteList) < Need {
			return nil, fmt.Errorf("invite not enough")
		}
		Items = item.Merge(Items, Reward)
	}
	i.GetIndex = cur
	return Items, nil
}

func (i *InviteMod) BackData() *msg.ResInviteFriendData {
	return &msg.ResInviteFriendData{
		IdLists:  GoUtil.MapIntToSlice(i.InviteList),
		GetIndex: int32(i.GetIndex),
	}
}

func (i *InviteMod) NotifySuccess() *msg.NotifyInvitedSuccess {
	return &msg.NotifyInvitedSuccess{
		ResultCode: 1,
		IdLists:    GoUtil.MapIntToSlice(i.InviteList),
	}
}
