package model

import (
	"time"

	"github.com/globalsign/mgo/bson"
	"mongo.games.com/goserver/core/logger"
)

// 玩家排位赛信息

type RankAward struct {
	Id int32 // 奖励ID
	Ts int64 // 奖励领取时间
}

type PlayerRankInfo struct {
	Score  int64        // 积分
	Awards []*RankAward // 赛季奖励
}

type PlayerRankSeason struct {
	Id           bson.ObjectId `bson:"_id"`
	Platform     string
	SnId         int32
	SeasonId     int32 // 赛季id
	RankType     map[int32]*PlayerRankInfo
	LastRankType map[int32]*PlayerRankInfo
	UpdateTs     int64
}

type NewPlayerRankSeasonArgs struct {
	Platform string
	SnId     int32
	SeasonId int32
}

func NewPlayerRankSeason(args *NewPlayerRankSeasonArgs) *PlayerRankSeason {
	ret := &PlayerRankSeason{
		Platform:     args.Platform,
		SnId:         args.SnId,
		SeasonId:     args.SeasonId,
		RankType:     make(map[int32]*PlayerRankInfo),
		LastRankType: make(map[int32]*PlayerRankInfo),
	}
	ret.UpdateTs = time.Now().Unix()
	return ret
}

func UpsertPlayerRankSeason(args *PlayerRankSeason) bool {
	if rpcCli == nil {
		logger.Logger.Error("model.UpsertPlayerRankSeason rpcCli == nil")
		return false
	}

	var ret bool
	args.UpdateTs = time.Now().Unix()
	err := rpcCli.CallWithTimeout("PlayerRankSeasonSvc.Upsert", args, &ret, time.Second*30)
	if err != nil {
		logger.Logger.Error("PlayerRankSeasonSvc.Upsert error:", err)
		return false
	}
	return ret
}

type FindPlayerRankSeasonArgs struct {
	Platform string
	Id       []int32
}

type FindPlayerRankSeasonReply struct {
	List []*PlayerRankSeason
}

func FindPlayerRankSeason(args *FindPlayerRankSeasonArgs) (*FindPlayerRankSeasonReply, error) {
	if rpcCli == nil {
		logger.Logger.Error("model.FindPlayerRankSeason rpcCli == nil")
		return nil, nil
	}

	ret := new(FindPlayerRankSeasonReply)
	err := rpcCli.CallWithTimeout("PlayerRankSeasonSvc.Find", args, ret, time.Second*30)
	if err != nil {
		logger.Logger.Error("FindPlayerRankSeason.Find error:", err)
		return ret, err
	}
	return ret, nil
}
