package douyin

import (
	"git.rosy.net.cn/baseapi/platformapi/tiktok"
	"git.rosy.net.cn/baseapi/platformapi/weixinapi"
	"git.rosy.net.cn/jx-callback/business/auth2"
	"git.rosy.net.cn/jx-callback/business/auth2/authprovider"
	"git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
	"strings"
)

const (
	AuthTypeTiktokMini = "tiktokmini" // 抖音小程序
)

type TiktokMiniAuther struct {
	authprovider.DefAuther
}

var (
	AutherObjMini *TiktokMiniAuther
)

func init() {
	AutherObjMini = new(TiktokMiniAuther)
	auth2.RegisterAuther(AuthTypeTiktokMini, AutherObjMini)
}

func (a *TiktokMiniAuther) VerifySecret(dummy, code string) (authBindEx *auth2.AuthBindEx, err error) {
	//appID, realCode := splitCode(code)
	//sessionInfo, err := getTikTokApp(appID).GetTiktokOauth(realCode)
	sessionInfo, err := api.TiktokApi.GetTiktokOauth(code)
	if err != nil {
		return nil, err
	}
	sessionKey := sessionInfo.Data.SessionKey
	if authBindEx, err = a.UnionFindAuthBind(AuthTypeTiktokMini, api.TiktokApi.GetAppID(), []string{AuthTypeTiktokMini}, sessionInfo.Data.OpenId, sessionInfo.Data.Unionid, sessionInfo); err == nil {
		authBindEx.UserData = sessionKey
	}
	return authBindEx, err
}

// 特殊接口
func (a *TiktokMiniAuther) 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 != AuthTypeTiktokMini {
			// 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 *TiktokMiniAuther) 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
}
