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/business/model"
	"git.rosy.net.cn/jx-callback/globals/api"
)

type StaffAuther struct {
	authprovider.DefAuther
}

var (
	AutherObjStaff *StaffAuther
)

func init() {
	AutherObjStaff = new(StaffAuther)
	auth2.RegisterAuther(AuthTypeStaff, AutherObjStaff)
}

func (a *StaffAuther) VerifySecret(dummy, code string) (authBindEx *auth2.AuthBindEx, err error) {
	userID, err := api.DingDingAPI.GetUserID(code)
	if err == nil {
		userDetail, err2 := api.DingDingAPI.GetUserDetail(userID.UserID)
		if err = err2; err == nil {
			if authBindEx, err = a.UnionFindAuthBind(AuthTypeStaff, api.DingDingQRCodeAPI.GetAppID(), []string{AuthTypeStaff, AuthTypeQRCode}, userID.UserID, utils.Interface2String(userDetail["unionid"]), userDetail); err == nil {
				authBindEx.UserHint = &auth2.UserBasic{
					UserID2: userID.UserID,
					Mobile:  utils.Interface2String(userDetail["mobile"]),
					Email:   utils.Interface2String(userDetail["email"]),
					Name:    utils.Interface2String(userDetail["name"]),
				}
			}
		}
	}
	return authBindEx, err
}

func (a *StaffAuther) GetUserType() (userType int8) {
	return model.UserTypeOperator
}
