package dingding

import (
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/auth2"
	"git.rosy.net.cn/jx-callback/business/auth2/authprovider"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

type QRCodeAuther struct {
	authprovider.DefAuther
}

var (
	AutherObjQRCode *QRCodeAuther
)

func init() {
	AutherObjQRCode = new(QRCodeAuther)
	auth2.RegisterAuther(AuthTypeQRCode, AutherObjQRCode)
}

func (a *QRCodeAuther) VerifySecret(dummy, code string) (authBindEx *auth2.AuthBindEx, err error) {
	globals.SugarLogger.Debugf("dingding qrcode VerifySecret code:%s", code)

	userQRInfo, err := api.DingDingQRCodeAPI.GetUserInfoByCode(code)
	if err == nil {
		globals.SugarLogger.Debugf("dingding qrcode VerifySecret code:%s, userQRInfo:%s", code, utils.Format4Output(userQRInfo, false))
		if authBindEx, err = a.UnionFindAuthBind(AuthTypeQRCode, api.DingDingQRCodeAPI.GetAppID(), []string{AuthTypeStaff, AuthTypeQRCode}, userQRInfo.OpenID, userQRInfo.UnionID, userQRInfo); err == nil {
			authBindEx.UserHint = &auth2.UserBasic{
				Name: userQRInfo.Nickname,
			}
		}
	}
	return authBindEx, err
}
