package models

import (
	"context"
	mhayaTime "github.com/mhaya/extend/time"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/guid"
	"github.com/mhaya/game/game_cluster/internal/mdb"
	"github.com/mhaya/game/game_cluster/internal/param"
	initdata "github.com/mhaya/game/game_cluster/internal/third/tg_sign"
	clog "github.com/mhaya/logger"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type Account struct {
	UserName  string `json:"username" bson:"userName"`
	Platform  string `json:"platform" bson:"platform"`
	Account   string `json:"account" bson:"account"`
	Channel   string `json:"channel" bson:"channel"`
	IsPremium bool   `json:"isPremium" bson:"isPremium"`
	LoginIp   string `json:"loginIp" bson:"loginIp"`
	OpenId    string `json:"openId" bson:"openId"`
	JoinIp    string `json:"joinip" bson:"JoinIp"`
	JoinTime  int64  `json:"jointime" bson:"JoinTime"`
}

func (ac *Account) AccountRegisterOrLogin(req *param.LoginReq) (*Account, int32) {
	var account Account
	findFilter := bson.M{"openId": req.OpenID}
	err := mdb.MDB.Collection(constant.CNameAccount).FindOne(context.Background(), findFilter).Decode(&account)
	if err != nil && err != mongo.ErrNoDocuments {
		clog.Errorf("Failed to AccountRegisterOrLogin select err request: %v  err = %v", req, err.Error())
		return nil, code.LoginError
	}

	user := initdata.GetUserInfo(req.Sign)

	//设置IP 规则
	num, ok := SetPlayerBlacklistIpRecord(req.IP, req.OpenID)
	if ok {
		clog.Errorf("Failed to SetPlayerBlacklistIpRecord err request: %v,num:%v", req, num)
		return nil, code.LoginError
	}

	if account.OpenId != "" {
		if account.Platform == "on" {
			//统计新注册
			SetAppointDailyRecordNewUserRegisterHash(req.Platform, req.Channel, account.UserName, account.JoinIp, mhayaTime.CreateFromTimestamp(account.JoinTime).Unix(), DailyRecordNewRegistered)
		}
		if (len(user.Username) > 0 && account.Account != user.Username) || user.IsPremium != account.IsPremium || req.IP != account.LoginIp || account.Platform != req.Platform {
			account.Account = user.Username
			account.IsPremium = user.IsPremium
			account.LoginIp = req.IP
			account.Platform = req.Platform
			mdb.MDB.Collection(constant.CNameAccount).UpdateOne(context.Background(), findFilter, bson.M{"$set": bson.M{"platform": req.Platform, "account": user.Username, "isPremium": user.IsPremium, "loginIp": req.IP}})
		}

		return &account, code.OK
	}

	devAccountTable := &Account{
		UserName:  guid.Next(),
		OpenId:    req.OpenID,
		Platform:  req.Platform,
		Channel:   req.Channel,
		Account:   user.Username,
		IsPremium: user.IsPremium,
		LoginIp:   req.IP,
		JoinIp:    req.IP,
		JoinTime:  mhayaTime.Now().Unix(),
	}
	_, err = mdb.MDB.Collection(constant.CNameAccount).InsertOne(context.Background(), devAccountTable)
	if err != nil {
		clog.Errorf("Failed to AccountRegisterOrLogin request: %v  err = %v", devAccountTable, err.Error())
		return nil, code.LoginError
	}
	//统计新注册
	SetDailyRecordNewUserRegisterHash(req.Platform, req.Channel, devAccountTable.UserName, req.IP, DailyRecordNewRegistered)

	return devAccountTable, code.OK
}
