package player

import (
	"context"
	"fmt"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/data"
	"github.com/mhaya/game/game_cluster/internal/mdb"
	"github.com/mhaya/game/game_cluster/internal/mdb/models"
	"github.com/mhaya/game/game_cluster/internal/param"
	clog "github.com/mhaya/logger"
)

func (p *actorPlayer) invite() {

	pid := mdb.RDB.Get(context.Background(), fmt.Sprintf("%v:1:%v", constant.InviteKey, p.Player.OpenId)).Val()

	p.Player.Pid = pid
	p.Player.SetInvite(pid)
	//添加日志
	clog.Infof("player invite，playerName：%v, inviteName：%v", p.Player.UserName, pid)
}

func (p *actorPlayer) inviteRecord() (*param.WeeklyRankConfigResp, int32) {
	if !p.isOnline {
		return nil, code.PlayerNotLogin
	}
	var resp param.WeeklyRankConfigResp
	if d, ok := p.Player.Invite[models.InvitePlayer]; ok {
		resp.InviteNum = d.Num
		for _, v := range d.Reward {
			resp.InviteTotalReward = append(resp.InviteTotalReward, data.ItemReward{ItemID: v.ItemID, ItemBaseType: v.ItemBaseType, Amount: v.Amount})
		}
	}
	if d, ok := p.Player.Invite[models.InviteVipPlayer]; ok {
		resp.VipInviteNum = d.Num
		for _, v := range d.Reward {
			resp.VipInviteTotalReward = append(resp.VipInviteTotalReward, data.ItemReward{ItemID: v.ItemID, ItemBaseType: v.ItemBaseType, Amount: v.Amount})
		}
	}

	return &resp, code.OK
}

/*func (p *actorPlayer) inviteReward(req *param.InviteReq) (*int, int32) {
	if !p.isOnline {
		p.InitBase(req.ParentName)
	}
	p.dirty = true
	p.isOnline = true
	addItem := models.NewAddItem()
	switch req.Type {
	case models.InvitePlayer:
		ret, ok := data.DiscreteRuleConfig.Get(models.InvitePlayer)
		if !ok {
			return &p.Player.IsLeader, code.Error
		}
		addItem.Add(ret.Reward)
		p.Player.Item.AddItem(ret.Reward)
		addItem.AddById(models.ItemInvite, 1)
		p.Player.SetPlayerRewardLog(models.SourceInvite, models.InvitePlayer, ret.Reward, []data.ItemReward{}, 2)
	case models.InviteVipPlayer:
		ret, ok := data.DiscreteRuleConfig.Get(models.InviteVipPlayer)
		if !ok {
			return &p.Player.IsLeader, code.Error
		}
		addItem.Add(ret.Reward)
		p.Player.Item.AddItem(ret.Reward)
		addItem.AddById(models.ItemInvite, 1)
		p.Player.SetPlayerRewardLog(models.SourceInvite, models.InviteVipPlayer, ret.Reward, []data.ItemReward{}, 2)
	}
	//社区长邀请
	if p.Player.IsLeader == 1 {
		ret, ok := data.DiscreteRuleConfig.Get(models.InvitePlayerByLeader)
		if !ok {
			return &p.Player.IsLeader, code.Error
		}
		addItem.Add(ret.Reward)
		p.Player.Item.AddItem(ret.Reward)
		p.Player.SetPlayerRewardLog(models.SourceInvite, models.InvitePlayerByLeader, ret.Reward, []data.ItemReward{}, 2)
	}
	//设置邀请榜单
	models.SetRank(constant.RankSourceInvite, p.Player.IsRobot, p.Player.UserName, 1)

	p.Player.LevelUp()

	p.Player.FinishAchieveTaskByType2()

	return &p.Player.IsLeader, code.OK
}
*/
