package models

import (
	"github.com/mhaya/game/game_cluster/internal/data"
	mhayaLogger "github.com/mhaya/logger"
)

func NewInviteNodeReward() InviteNodeReward {
	return make(map[int]*InviteNodeRewardItem)
}

type InviteNodeReward map[int]*InviteNodeRewardItem

type InviteNodeRewardItem struct {
	ID         int               `json:"ID" bson:"ID"`
	IsClaim    int               `json:"isClaim" bson:"isClaim"`       // 是否领取
	IsUnlock   int               `json:"isUnlock" bson:"isUnlock"`     // 是否解锁
	Reward     []data.ItemReward `json:"reward" bson:"reward"`         // 奖励
	RewardTime int64             `json:"rewardTime" bson:"rewardTime"` //领取奖励时间
	Condition  int               `json:"condition" bson:"condition"`   // 节点条件
}

type InviteReward struct {
	IsClaim    int               `json:"isClaim" bson:"isClaim"`
	Reward     []data.ItemReward `json:"reward" bson:"reward"`
	RewardTime int64             `json:"rewardTime" bson:"rewardTime"`
}

type Invite map[int]*InviteModel

type InviteModel struct {
	Type   int                      `json:"type" bson:"type"` //2普通3会员
	Num    int                      `json:"num" bson:"num"`
	Reward map[int]*data.ItemReward `json:"reward" bson:"reward"`
}

func NewFirstReward() FirstReward {
	return make(map[int]*FirstRewardItem)
}

type FirstReward map[int]*FirstRewardItem

type FirstRewardItem struct {
	IsClaim    int               `json:"isClaim" bson:"isClaim"`
	Reward     []data.ItemReward `json:"reward" bson:"reward"`
	RewardTime int64             `json:"rewardTime" bson:"rewardTime"`
}

func NewInvite() map[int]*InviteModel {

	var invite = make(map[int]*InviteModel)

	invite[InvitePlayer] = &InviteModel{
		Type: InvitePlayer,
		Num:  0,
	}
	invite[InvitePlayer].Reward = make(map[int]*data.ItemReward)

	invite[InviteVipPlayer] = &InviteModel{
		Type: InviteVipPlayer,
		Num:  0,
	}
	invite[InviteVipPlayer].Reward = make(map[int]*data.ItemReward)
	return invite
}

func (i Invite) Set(tp int, reward []data.ItemReward) {
	if _, ok := i[tp]; ok {
		i[tp].Num += 1
		mhayaLogger.Infof("player set total invite, tp=%v,num=%v", tp, i[tp].Num)
		for _, v := range reward {
			if _, ok = i[tp].Reward[v.ItemID]; ok {
				i[tp].Reward[v.ItemID].Amount += v.Amount
			} else {
				i[tp].Reward[v.ItemID] = &v
			}
		}
	}
}

func (ir *InviteReward) Merge() {

	m := make(map[int]int)
	for _, kv := range ir.Reward {
		m[kv.ItemID] += kv.Amount
	}

	result := make([]data.ItemReward, 0, len(m))
	for k, v := range m {
		result = append(result, data.ItemReward{ItemID: k, Amount: v, ItemBaseType: 1})
	}

	ir.Reward = result
}
