package model

import (
	"time"

	"github.com/globalsign/mgo/bson"
)

var (
	LoginLogDBName   = "log"
	LoginLogCollName = "log_login"
)

type ClientLoginInfo struct {
	LoginType    int32
	ApkVer       int32
	ResVer       int32
	InviterId    int32
	PromoterTree int32
	Sid          int64
	UserName     string
	PlatformTag  string
	Promoter     string
	HeadUrl      string
	DeviceOS     string
}

// LoginLog 登录登出记录
type LoginLog struct {
	LogId        bson.ObjectId `bson:"_id"`
	Platform     string        //平台id
	Channel      string        //渠道
	Promoter     string        //推广员
	Package      string        //包名
	PackageTag   string        //推广包标识
	InviterId    int32         //邀请人id
	PromoterTree int32
	SnId         int32
	LogType      int32
	ApkVer       int32
	ResVer       int32
	Tel          string
	IP           string
	City         string
	IsBind       bool
	TotalCoin    int64 // 总余额
	GameId       int   // 玩家掉线时所在游戏
	Time         time.Time
	Ts           int64
	LastGameID   int // 玩家最后所在游戏id

	DeviceName   string
	AppVersion   string
	BuildVersion string
	AppChannel   string
	DeviceOS     string
}

func NewLoginLog(snId, logType int32, tel, ip, platform, channel, promoter, packageId, city string,
	clog *ClientLoginInfo, totalCoin int64, gameId, lastGameId int,
	DeviceName, PackageName, AppVersion, BuildVersion, AppChannel string) *LoginLog {
	now := time.Now()
	cl := &LoginLog{LogId: bson.NewObjectId()}
	cl.SnId = snId
	cl.LogType = logType
	cl.IP = ip
	cl.Tel = tel
	cl.City = city
	cl.Time = now
	cl.Ts = now.Unix()
	cl.Platform = platform
	cl.Channel = channel
	cl.Promoter = promoter
	cl.PackageTag = packageId
	if clog != nil {
		cl.InviterId = clog.InviterId
		cl.ApkVer = clog.ApkVer
		cl.ResVer = clog.ResVer
		cl.PackageTag = clog.PlatformTag
		cl.DeviceOS = clog.DeviceOS
	}
	cl.TotalCoin = totalCoin
	cl.GameId = gameId
	cl.LastGameID = lastGameId
	if tel != "" {
		cl.IsBind = true
	} else {
		cl.IsBind = false
	}

	cl.Package = PackageName
	cl.DeviceName = DeviceName
	cl.AppVersion = AppVersion
	cl.BuildVersion = BuildVersion
	cl.AppChannel = AppChannel

	return cl
}

//func InsertLoginLogs(logs ...*LoginLog) (err error) {
//	if rpcCli == nil {
//		logger.Logger.Error("model.InsertLoginLogs rpcCli == nil")
//		return
//	}
//	var ret bool
//	err = rpcCli.CallWithTimeout("LoginsLogSvc.InsertLoginLogs", logs, &ret, time.Second*30)
//	if err != nil {
//		logger.Logger.Warn("InsertLoginLogs error:", err)
//	}
//	return
//}
//
//func InsertSignleLoginLog(log *LoginLog) (err error) {
//	if rpcCli == nil {
//		logger.Logger.Error("model.InsertLoginLogs rpcCli == nil")
//		return
//	}
//	var ret bool
//	err = rpcCli.CallWithTimeout("LoginsLogSvc.InsertLoginLogs", log, &ret, time.Second*30)
//	if err != nil {
//		logger.Logger.Warn("InsertLoginLogs error:", err)
//	}
//	return
//}
