package kuaishou_mini

import (
	"encoding/base64"
	"fmt"
	"git.rosy.net.cn/baseapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/globals"
	"go.uber.org/zap"
	"testing"
)

var (
	api         *API
	sugarLogger *zap.SugaredLogger
)

func init() {
	logger, _ := zap.NewDevelopment()
	sugarLogger = logger.Sugar()
	baseapi.Init(sugarLogger)
	api = New("1wShCPqUzhg8W1vcb8OdvA", "ks680887971696897880")
}

func TestAuthLogin(t *testing.T) {
	jscode := "0F937BADB84C739B0C5DAFCACE1DB3BDFC9D83777CAF4A094AD513DD42BE2238"
	sessionCode, openId, err := api.AuthLoginKuaiShou(jscode)
	globals.SugarLogger.Debugf("session_code := %s", sessionCode)
	globals.SugarLogger.Debugf("open_id := %s", openId)
	globals.SugarLogger.Debugf("err := %v", err)
}

func TestDecryptUserMsg(t *testing.T) {
	encryptedData := "ipj6OPhMqb35MZbKafvdWbCqJ3a7kb4BPGGClijFMB1VEy4x8p91ERyfjC1Yl15jfuL+3Viy2r5E+bls9b3Qxg=="
	iv := "J4UVoNNj5pHD/7MLdw9I8w=="
	sessionKey := "6gUP5PZCqw+NUM/hW0zXMg=="
	data, err := DecryptUserMsg(sessionKey, iv, encryptedData)
	globals.SugarLogger.Debugf("data := %s", data)
	globals.SugarLogger.Debugf("err := %v", err)
}

func DecryptUserMsg(sessionKey, iv, msg string) (string, error) {
	decodeMsg, err := base64.StdEncoding.DecodeString(msg)
	if err != nil {
		return "", err
	}
	decodeIv, err := base64.StdEncoding.DecodeString(iv)
	if err != nil {
		return "", err
	}
	decodeSessionKey, err := base64.StdEncoding.DecodeString(sessionKey)
	if err != nil {
		return "", err
	}

	userInfo, err := utils.AESCBC16Decrypt(decodeSessionKey, decodeIv, decodeMsg)
	if err != nil {
		return "", err
	}
	//result := &TikTokDecryptInfo{}
	//if err := json.Unmarshal(userInfo, result); err != nil {
	//	return "", err
	//}
	//return result.PhoneNumber, nil
	return string(userInfo), nil
}

func TestCreateToken(t *testing.T) {
	api.GetToken()
}

// 快手预下单获取支付参数
func TestPreCreateOrder(t *testing.T) {
	param := &PreCreateOrderReq{
		OutOrderNo:           "1024028220",
		OpenId:               "f198f59711c9785314bf5724f7ab9f47",
		TotalAmount:          1,
		Subject:              "这是一个测试商品",
		Detail:               "大白菜,小白菜,中白菜",
		TypeDetail:           1832, // 素菜
		ExpireTime:           10 * 60,
		Sign:                 "",
		Attach:               "1111",
		NotifyUrl:            "https://callback.jxc4.com/tictoc/tiktokMsg",
		GoodsId:              "1122",
		GoodsDetailUrl:       "/page/index/anima",
		MultiCopiesGoodsInfo: "[{\"copies\":2}]",
		CancelOrder:          0,
	}
	data, err := api.PreCreateOrder(param)
	globals.SugarLogger.Debugf("data := %s", utils.Format4Output(data, false))
	globals.SugarLogger.Debugf("err := %s", utils.Format4Output(err, false))
}

func TestAaww(t *testing.T) {
	aa := make(map[string]interface{}, 1)
	aa["a"] = ""
	if value, ok := aa["a"].(int64); ok {
		fmt.Println(value)
	}
	if value, ok := aa["a"].(string); ok {

		fmt.Println(utils.Str2Int64(value))
	}
}
