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/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) (*auth2.AuthBindEx, error) {
	userQRInfo, err := api.DingDingQRCodeAPI.GetUserInfoByCode(code)
	if err != nil {
		return nil, err
	}
	tmp_userID, err := api.DingDingAPI.GetByUnionID(userQRInfo.UnionID)
	if err != nil {
		return nil, err
	}
	userInfo, err := api.DingDingAPI.GetUser(tmp_userID.UserID)
	if err != nil {
		return nil, err
	}
	authBindEx, err := a.UnionFindAuthBind(AuthTypeQRCode, api.DingDingQRCodeAPI.GetAppID(), []string{AuthTypeStaff, AuthTypeQRCode}, userQRInfo.OpenID, userQRInfo.UnionID, userQRInfo)
	if err != nil {
		return nil, err
	}
	authBindEx.UserHint = &auth2.UserBasic{
		UserID2: tmp_userID.UserID,
		Mobile:  utils.Interface2String(userInfo["mobile"]),
		Email:   utils.Interface2String(userInfo["email"]),
		Name:    utils.Interface2String(userInfo["name"]),
	}

	return authBindEx, err
}
