package kuaishou_mini

import (
	"encoding/base64"
	"encoding/json"
	"errors"
	"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:           "88422209072664",
		OpenId:               "f198f59711c9785314bf5724f7ab9f47",
		TotalAmount:          501,
		Subject:              "蔬菜水果日用品",
		Detail:               "好吃的素材水果",
		TypeDetail:           1832, // 素菜
		ExpireTime:           10 * 60,
		Sign:                 "",
		Attach:               "22",
		NotifyUrl:            "https://callback-jxgy.jxc4.com/kuaishou/kuaiShouCallback",
		GoodsId:              "1122",
		GoodsDetailUrl:       "/page/index/anima",
		MultiCopiesGoodsInfo: `[{"copies":2}]`,
		CancelOrder:          1,
	}
	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))
	}
}

func TestLen(t *testing.T) {
	data := `{"data":{"ks_order_no":"123060711428646148797","refund_type":null,"out_refund_no":null,"ks_settle_no":null,"channel":"WECHAT","out_order_no":"88423854448004","ks_refund_fail_reason":null,"apply_refund_reason":null,"promotion_amount":0,"developer_promotion_amount":0,"extra_info":"{\"url\":\"\",\"provider\":\"\",\"item_type\":\"UNKNOWN\",\"item_id\":\"\",\"author_id\":\"\",\"refer_id\":\"\",\"channel_mark\":\"\",\"provider_type\":\"\",\"trade_no\":\"4307400963202306073218531739\"}","settle_amount":null,"order_amount":2,"enable_promotion":false,"out_settle_no":null,"refund_amount":null,"trade_no":"4307400963202306073218531739","attach":"","ks_refund_no":null,"status":"SUCCESS"},"biz_type":"PAYMENT","message_id":"65299d6d-072e-4804-8ef4-23451024a0d9","app_id":"ks680887971696897880","timestamp":1686129660709}`
	ACCC(data)
}

func ACCC(data string) (*CallBackDetail, *RefundCallBack, string, string, error) {
	globals.SugarLogger.Debugf("data================: %s", string(data))
	var callback *KauiShouCallbackRes
	if err := json.Unmarshal([]byte(data), &callback); err != nil {
		return nil, nil, "", "-1", err
	}

	switch callback.BizType {
	case CallbackTypePay:
		var payCallback *CallBackDetail
		globals.SugarLogger.Debugf("payCallback : %s", utils.Format4Output(callback.Data, false))
		if err := utils.Map2StructByJson(callback.Data, &payCallback, false); err != nil {
			globals.SugarLogger.Debugf("err3 =========== :%s", utils.Format4Output(err, false))
			return nil, nil, "", callback.MessageId, err
		}
		return payCallback, nil, CallbackTypePay, callback.MessageId, nil
	case CallbackTypeRefund:
		var refundCallback *RefundCallBack
		if err := utils.Map2StructByJson(callback.Data, &refundCallback, false); err != nil {
			globals.SugarLogger.Debugf("err5 =========== :%s", utils.Format4Output(err, false))
			return nil, nil, "", callback.MessageId, err
		}
		return nil, refundCallback, CallbackTypeRefund, callback.MessageId, nil
	case CallbackTypeSettle:
		return nil, nil, "", callback.MessageId, errors.New("无效回调类型")
	default:
		return nil, nil, "", callback.MessageId, errors.New("无效回调类型")
	}
}
