package kuaishou

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

const (
	AuthTypeKuaiShouMini = "kuaishoumini" // 快手小程序
)

type KuaiShouMiniAuther struct {
	authprovider.DefAuther
}

var (
	AutherObjMini *KuaiShouMiniAuther
)

func init() {
	AutherObjMini = new(KuaiShouMiniAuther)
	auth2.RegisterAuther(AuthTypeKuaiShouMini, AutherObjMini)
}

func (a *KuaiShouMiniAuther) VerifySecret(dummy, code string) (authBindEx *auth2.AuthBindEx, err error) {
	//appID, realCode := splitCode(code)
	//sessionInfo, err := getTikTokApp(appID).GetTiktokOauth(realCode)
	sessionInfo, openId, err := api.KuaiShouApi.AuthLoginKuaiShou(code)
	if err != nil {
		return nil, err
	}
	if authBindEx, err = a.UnionFindAuthBind(AuthTypeKuaiShouMini, api.KuaiShouApi.GetAppID(), []string{AuthTypeKuaiShouMini}, openId, "", sessionInfo); err == nil {
		authBindEx.UserData = sessionInfo
	}
	return authBindEx, err
}

//func (a *KuaiShouMiniAuther) DecryptData(authInfo *auth2.AuthInfo, jsCode, encryptedData, iv string) (decryptedDataBase64 string, err error) {
//	var sessionKey string
//	appID, jsCode := weixin.SplitJsCode(jsCode)
//	if jsCode != "" {
//		sessionInfo, err := getWxApp(appID).SNSCode2Session(jsCode)
//		if err == nil {
//			// if authBindEx, err := a.UnionFindAuthBind(AuthTypeMini, getWxApp(appID).GetAppID(), []string{AuthTypeMini}, sessionInfo.OpenID, "", nil); err == nil {
//			// if authBindEx.UserID != authInfo.GetID() {
//			// 	return "", fmt.Errorf("jsCode与token不匹配")
//			// }
//			// } else {
//			// 	return "", err
//			// }
//			sessionKey = sessionInfo.SessionKey
//		} else {
//			return "", err
//		}
//	} else {
//		if authInfo.AuthBindInfo.Type != AuthTypeKuaiShouMini {
//			// return "", ErrAuthTypeShouldBeMini
//		}
//		sessionKey = authInfo.AuthBindInfo.UserData.(string)
//	}
//	decryptedData, err := weixinapi.SNSDecodeMiniProgramData(encryptedData, sessionKey, iv)
//	if err != nil {
//		return "", err
//	}
//	return string(decryptedData), nil
//}
//
//func (a *KuaiShouMiniAuther) GetUserType() (userType int8) {
//	return model.UserTypeStoreBoss
//}
//
//func getWxApp(appID string) (miniApi *weixinapi.API) {
//	miniApi = api.WeixinMiniAPI
//	if len(appID) > 0 && appID == api.WeixinMiniAppID2 {
//		miniApi = api.WeixinMiniAPI2
//	}
//	if len(appID) > 0 && appID == api.WeixinMiniAppIDsc {
//		miniApi = api.WeixinMiniAPIsc
//	}
//	return miniApi
//}
//
//func getTikTokApp(appID string) (TikTokMini *tiktok.API) {
//	TikTokMini = api.TiktokApi
//	if len(appID) > 0 && appID == api.TiktokJXDJApiID {
//		TikTokMini = api.TiktokJXDJApi
//	}
//	return TikTokMini
//}
//func splitCode(code string) (appID, realCode string) {
//	str := strings.Split(code, "&")
//	if len(str) == 2 {
//		appID = str[1]
//		realCode = str[0]
//	} else {
//		globals.SugarLogger.Warnf("splitCode abnormal code:%s", code)
//	}
//	return appID, realCode
//}
