package wechat

import (
	"testing"

	log "github.com/sirupsen/logrus"
	. "github.com/smartystreets/goconvey/convey"
)

const (
	WechatAppID     = "wx45745a8c51091ae0"
	WechatAppSecret = "2ab33bc79d9b47efa4abef19d66e1977"
	WechatToken     = "W8Xhw5TivYBgY"
	WechatAesKey    = "F6AqCxAV4W1eCrY6llJ2zapphKK49CQN3RgtPDrjhnI"
)

func init() {
	log.SetLevel(log.DebugLevel)
}

func getClient() *Client {
	return New(
		WithAppID(WechatAppID),
		WithAppSecret(WechatAppSecret),
		WithAESKey(WechatAesKey),
		WithToken(WechatToken),
		WithClient(DefaultClient.DevMode()),
	)
}

func TestWechatClient_GetAccessToken(t *testing.T) {
	Convey("Test GetAccessToken", t, func() {
		token, err := getClient().GetAccessToken()
		So(err, ShouldBeNil)
		So(token.AccessToken, ShouldNotBeEmpty)
		So(token.ExpiresIn, ShouldBeGreaterThan, 0)

		t.Log("Access Token:", token.AccessToken)
	})
}

func TestClient_ScopeAuthorizeURL(t *testing.T) {
	Convey("Test ScopeAuthorizeURL", t, func() {
		url, err := getClient().ScopeAuthorizeURL(
			ScopeAuthorizeURLWithScope(ScopeBase),
			ScopeAuthorizeURLWithRedirectURI("https://qvyun.mp.jdwan.com/"),
		)
		So(err, ShouldBeNil)
		So(url, ShouldNotBeEmpty)
		t.Log("URL:", url)
	})
}

func TestClient_AuthorizeCode2Token(t *testing.T) {
	code := "011W1sll2Xv4Ae4OjUnl2I7jvd2W1slX"

	Convey("Test AuthorizeCode2Token", t, func() {
		token, err := getClient().AuthorizeCode2Token(code)
		So(err, ShouldBeNil)

		t.Logf("token: %+v", token)
	})
}

func TestClient_AuthorizeRefreshAccessToken(t *testing.T) {
	token := "86_m_EAHq0RKlo6RzzGAsY8gVmiCqHqIiAJufxhm8mK8imyIW6yoE4NTcIr2vaukp7dexPWId0JWP1iZWYaLpXT_MJv1N7YQW8Qt3zOZDpJY90"

	Convey("Test AuthorizeCode2Token", t, func() {
		token, err := getClient().AuthorizeRefreshAccessToken(token)
		So(err, ShouldBeNil)

		t.Logf("token: %+v", token)
	})
}

func TestClient_AuthorizeUserInfo(t *testing.T) {
	token := "86_ZxJa8mIwbml5mDlHHbIUle_UKW8LA75nOuB0wqiome8AX5LlMWU8JwRKMZykdLEjDnKX8EJavz5GeQn3T1ot7TwpULp8imQvNIgFIjC4er8"
	openID := "oMLa5tyJ2vRHa-HI4CMEkHztq3eU"

	Convey("Test AuthorizeUserInfo", t, func() {
		user, err := getClient().AuthorizeUserInfo(token, openID)
		So(err, ShouldBeNil)

		t.Logf("user: %+v", user)
	})
}

func Test_GetJsTicket(t *testing.T) {
	Convey("Test GetJsTicket", t, func() {
		token := "91_0pKuAiBFquPdLakDyhYqOyNJkGLr7-Egx-IF4bRzw-2Lpm7wxgz6zVBNJ36FvMXmiu8bz9BTtspVICf1zDZ3XWuVLwTq6T3a6WG1k6NHv6E0PadT-G5x2Y85-xUECBcADATRQ"
		ticket, err := getClient().GetJSTicket(token)
		So(err, ShouldBeNil)
		So(ticket, ShouldNotBeEmpty)

		t.Log("Js Ticket:", ticket)
	})
}

func Test_GetStableToken(t *testing.T) {
	Convey("Test_GetStableToken GetJsTicket", t, func() {
		token, err := getClient().GetStableAccessToken()
		So(err, ShouldBeNil)
		So(token, ShouldNotBeNil)

		t.Logf("Stable Token: %+v", token)
	})
}
