package model

import (
	"errors"
	"time"

	"github.com/globalsign/mgo/bson"

	"mongo.games.com/goserver/core/logger"
)

type InviteCode struct {
	Id   bson.ObjectId `bson:"_id"`
	SnId int32
	Code string
}

type InviteSnIdReq struct {
	Platform string
	Code     string
}

type InviteSnIdRet struct {
	SnId int32
}

func GetSnIdByCode(platform string, code string) (int32, error) {
	if rpcCli == nil {
		logger.Logger.Warnf("rpcCli is nil")
		return 0, errors.New("rpcCli is nil")
	}

	req := &InviteSnIdReq{
		Platform: platform,
		Code:     code,
	}
	ret := &InviteSnIdRet{}
	err := rpcCli.CallWithTimeout("InviteCodeSvc.GetSnIdByCode", req, ret, time.Second*30)
	if err != nil {
		logger.Logger.Warnf("GetSnIdByCode err:%v", err)
		return 0, err
	}

	return ret.SnId, err
}

// InviteScore 积分记录
type InviteScore struct {
	Id         bson.ObjectId `bson:"_id"`
	Platform   string        // 平台id
	SnId       int32         // 被邀请人id
	InviteSnId int32         // 邀请人id
	Tp         int32         // 积分类型 common.InviteScoreType~
	Score      int64         // 积分
	Ts         int64         // 时间戳
}

type InviteScoreReq struct {
	Platform string
	SnId     int32
}

type InviteScoreRet struct {
	Score  int64
	ZScore int64
}

// GetInviteScore 查询总积分
// 下级玩家所有积分
func GetInviteScore(plt string, snid int32) (int64, int64, error) {
	if rpcCli == nil {
		logger.Logger.Warnf("rpcCli is nil")
		return 0, 0, errors.New("rpcCli is nil")
	}

	req := &InviteScoreReq{
		Platform: plt,
		SnId:     snid,
	}
	ret := &InviteScoreRet{}
	err := rpcCli.CallWithTimeout("BindScoreSvc.GetInviteScore", req, ret, time.Second*30)
	if err != nil {
		logger.Logger.Warnf("GetInviteScore err:%v", err)
		return 0, 0, err
	}

	return ret.Score, ret.ZScore, err
}

// SaveInviteScore 保存积分变更记录
func SaveInviteScore(b *InviteScore) error {
	if rpcCli == nil {
		logger.Logger.Warnf("rpcCli is nil")
		return errors.New("rpcCli is nil")
	}

	ret := false
	err := rpcCli.CallWithTimeout("BindScoreSvc.SaveInviteScore", b, &ret, time.Second*30)
	if err != nil {
		logger.Logger.Warnf("SaveInviteScore err:%v", err)
		return err
	}
	return nil
}

const EvtBindInvite = "evt_bind_invite"
const AckBindNum = "ack_bind_num"
const EvtInviteScore = "evt_invitescore"

type BindInvite struct {
	Platform   string
	SnId       int32
	InviteSnId int32
	Ts         int64
}

type BindNum struct {
	SnId int32
	Num  int32
}

type InviteLisArgs struct {
	Platform string
	SnId     int32
}

type InviteInfo struct {
	Name     string
	SnId     int32
	CreateTs int64
	Score    int64
	ModId    int32
}

type InviteListRet struct {
	List []*InviteInfo
}

func GetInviteList(platform string, snid int32) ([]*InviteInfo, error) {
	if rpcCli == nil {
		logger.Logger.Error("model.GetInviteList rpcCli == nil")
		return nil, nil
	}

	req := &InviteLisArgs{
		Platform: platform,
		SnId:     snid,
	}
	ret := new(InviteListRet)
	err := rpcCli.CallWithTimeout("BindScoreSvc.GetInviteList", req, ret, time.Second*30)
	if err != nil {
		logger.Logger.Error("GetInviteList error:", err)
		return nil, err
	}
	return ret.List, nil
}
