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"
	rpcCenter "github.com/mhaya/game/game_cluster/internal/rpc/center"
	sessionKey "github.com/mhaya/game/game_cluster/internal/session_key"
)

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")

	account := rpcCenter.RegisterAccount(p.app, openid, ip, plt, channel, sign)
	if account == nil {
		callback(code.LoginError, nil)
		return
	}

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

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