package weixin

import (
	"errors"

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

const (
	AuthTypeWeixin      = "wxqrcode"    // 微信扫码
	AuthTypeMP          = "weixinsns"   // 公众号
	AuthTypeWXNative    = "wxnative"    // 微信APP
	AuthTypeWxApp       = "weixinapp"   //app微信登录(商家版)
	AuthTypeWxAppCaishi = "weixinappcs" //app微信登录(用户版)
)

type Auther struct {
	authprovider.DefAuther
	authType string
}

var (
	AutherObjWX        *Auther
	AutherObjMP        *Auther
	AutherObjNative    *Auther
	AutherObjApp       *Auther
	AutherObjCaiShiApp *Auther
)

var (
	ErrStateIsWrong = errors.New("登录state非法")
)

func init() {
	AutherObjWX = &Auther{
		authType: AuthTypeWeixin,
	}
	auth2.RegisterAuther(AuthTypeWeixin, AutherObjWX)

	AutherObjMP = &Auther{
		authType: AuthTypeMP,
	}
	auth2.RegisterAuther(AuthTypeMP, AutherObjMP)

	AutherObjNative = &Auther{
		authType: AuthTypeWXNative,
	}
	auth2.RegisterAuther(AuthTypeWXNative, AutherObjNative)

	AutherObjApp = &Auther{
		authType: AuthTypeWxApp,
	}
	auth2.RegisterAuther(AuthTypeWxApp, AutherObjApp)

	AutherObjCaiShiApp = &Auther{
		authType: AuthTypeWxAppCaishi,
	}
	auth2.RegisterAuther(AuthTypeWxAppCaishi, AutherObjCaiShiApp)
}

func (a *Auther) VerifySecret(id, secret string) (authBindEx *auth2.AuthBindEx, err error) {
	var openID, accessToken string
	_, jsCode := SplitJsCode(secret)
	if a.authType != AuthTypeWXNative {
		state := id
		code := jsCode
		if state == "" {
			//temp := a.getAPI()
			//fmt.Printf("secret=%s,appid=%s", temp.GetSecret(), temp.GetAppID())
			token, err2 := a.getAPI().SNSRetrieveToken(code)
			if err = err2; err == nil {
				openID = token.OpenID
				accessToken = token.AccessToken
			}
		} else {
			err = ErrStateIsWrong
		}
	} else {
		openID = id
		accessToken = secret
	}
	if err == nil {
		wxUserinfo, err2 := a.getAPI().SNSGetUserInfo(accessToken, openID)
		if err = err2; err == nil {
			if authBindEx, err = a.UnionFindAuthBind(a.authType, a.getAPI().GetAppID(), []string{AuthTypeWeixin, AuthTypeMP, AuthTypeMini, AuthTypeWXNative, AuthTypeWxApp, AuthTypeWxAppCaishi}, wxUserinfo.OpenID, wxUserinfo.UnionID, wxUserinfo); err == nil {
				authBindEx.UserHint = &auth2.UserBasic{
					Name:   wxUserinfo.NickName,
					Avatar: wxUserinfo.HeadImgURL,
				}
			}
		}
	}
	return authBindEx, err
}

func (a *Auther) getAPI() *weixinapi.API {
	if a.authType == AuthTypeWeixin {
		return api.WeixinPageAPI
	}
	if a.authType == AuthTypeWxApp {
		return api.WeixinApp
	}
	if a.authType == AuthTypeWxAppCaishi {
		return api.WeixinApp2
	}
	return api.WeixinAPI
}

func (a *Auther) GetUserType() (userType int8) {
	return model.UserTypeStoreBoss
}

func GetAPI(appID string) *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
}
