package sdk

import (
	mhayaGin "github.com/mhaya/components/gin"
	cerror "github.com/mhaya/error"
	cstring "github.com/mhaya/extend/string"
	cfacade "github.com/mhaya/facade"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/data"
	"github.com/mhaya/game/game_cluster/internal/mdb/models"
	"github.com/mhaya/game/game_cluster/internal/param"
	sessionKey "github.com/mhaya/game/game_cluster/internal/session_key"
	"github.com/mhaya/game/game_cluster/internal/token"
	clog "github.com/mhaya/logger"
)

type quickSdk struct {
	app cfacade.IApplication
}

func (quickSdk) SdkId() int {
	return QuickSDK
}

func (p quickSdk) Login(config *data.SdkConfigRow, params Params, callback Callback) {
	openid, found := params.GetString("openid")
	if found == false || cstring.IsBlank(openid) {
		err := cerror.Error("openid is nil")
		callback(code.LoginError, nil, err)
		return
	}

	ip, found := params.GetString("ip")
	if found == false || cstring.IsBlank(ip) {
		err := cerror.Error("ip is nil")
		callback(code.LoginError, nil, err)
		return
	}

	plt, found := params.GetString("platform")
	if found == false || cstring.IsBlank(plt) {
		err := cerror.Error("platform is nil")
		callback(code.LoginError, nil, err)
		return
	}

	channel, found := params.GetString("channel")
	if found == false || cstring.IsBlank(channel) {
		err := cerror.Error("channel is nil")
		callback(code.LoginError, nil, err)
		return
	}

	sign, _ := params.GetString("sign")

	req := &param.LoginReq{
		OpenID:   openid,
		IP:       ip,
		Platform: plt,
		Channel:  channel,
		Sign:     sign,
	}
	acc := &models.Account{}
	account, err := acc.AccountRegisterOrLogin(req)
	if err > 0 {
		callback(code.LoginError, nil)
		return
	}
	nodeId, ok := SetNode(p.app)
	if code.IsFail(ok) {
		clog.Warnf("[RegisterAccount] openID = %s,nodeID=%v, errCode = %v", openid, nodeId, err)
		callback(code.LoginError, nil)
		return
	}

	targetPath := cfacade.NewChildPath(nodeId, "player"+nodeId, account.UserName)

	callback(code.OK, map[string]string{
		sessionKey.PlayerID:   account.UserName, //返回 quick的uid做为 open id
		sessionKey.OpenID:     openid,
		sessionKey.TargetPath: targetPath,
	})
}

func (p quickSdk) Reconnect(token *token.Token) (int32, *token.Token) {
	list := p.app.Discovery().ListByType("game")
	targetPath, err := cfacade.ToActorPath(token.TargetPath)
	if err != nil {
		clog.Warnf("[Reconnect] Target path error. targetPath = %v, error = %v", token.TargetPath, err)
		return code.AccountAuthFail, nil
	}
	isNew := false
	for _, v := range list {
		if v.GetNodeId() == targetPath.NodeID {
			isNew = true
			break
		}
	}
	if !isNew {
		nodeId, ok := SetNode(p.app)
		if code.IsFail(ok) {
			clog.Warnf("[RegisterAccount] openID = %s,nodeID=%v, errCode = %v", token.OpenID, nodeId, err)
			return code.AccountAuthFail, nil
		}
		path := cfacade.NewChildPath(nodeId, "player"+nodeId, token.PlayerID)
		token.TargetPath = path
	}
	return code.OK, token
}

func (p quickSdk) PayCallback(config *data.SdkConfigRow, c *mhayaGin.Context) {
	// TODO 这里实现quick sdk 支付回调的逻辑
	c.RenderHTML("FAIL")
}
