package wxpayapi

import (
	"io/ioutil"
	"strings"
	"testing"

	"git.rosy.net.cn/baseapi"
	"git.rosy.net.cn/baseapi/utils"
	"github.com/clbanning/mxj"

	"go.uber.org/zap"
)

var (
	api         *API
	sugarLogger *zap.SugaredLogger
)

func init() {
	logger, _ := zap.NewDevelopment()
	sugarLogger = logger.Sugar()
	baseapi.Init(sugarLogger)
	certPEMBlock, _ := ioutil.ReadFile("1390686702_20190115_cert/apiclient_cert.pem")
	keyPEMBlock, _ := ioutil.ReadFile("1390686702_20190115_cert/apiclient_key.pem")
	api = NewWithCertificate("wx70d0943e61e0d15c", "XKJPOIHJ233adf01KJIXlIeQDSDKFJAD", "1603491062", certPEMBlock, keyPEMBlock)
	// api = New("wx4b5930c13f8b1170", "XKJPOIHJ233adf01KJIXlIeQDSDKFJAD", "1390686702")
}

func TestOrderQuery(t *testing.T) {
	result, err := api.OrderQuery("4200000411201910186159598703", "")
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestCreateUnifiedOrder(t *testing.T) {
	orderNo := utils.GetUUID()
	t.Log(orderNo)
	result, err := api.CreateUnifiedOrder(&CreateOrderParam{
		Body:           "这里一个测试商品",
		NotifyURL:      "http://callback.test.jxc4.com/wxpay/msg/",
		OutTradeNo:     orderNo,
		SpbillCreateIP: "114.114.114.114",
		TradeType:      TradeTypeNative,
		TotalFee:       20,
		ProfitSharing:  OptYes,
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestCloseOrder(t *testing.T) {
	orderNo := "5CA4BC5E22C611EA9AB9186590E02977" // utils.GetUUID()
	// t.Log(orderNo)
	err := api.CloseOrder(orderNo)
	if err != nil {
		t.Fatal(err)
	}
}

func TestPayRefund(t *testing.T) {
	result, err := api.PayRefund(&PayRefundParam{
		TransactionID: "",
		OutTradeNo:    "88316516447981",
		NotifyURL:     "http://callback.test.jxc4.com/wxpay/msg/",
		OutRefundNo:   "88316516447981",
		TotalFee:      8987,
		RefundFee:     8987,
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestXml2Json(t *testing.T) {
	xmlStr := strings.Replace(`
	<root>
	<out_refund_no><![CDATA[131811191610442717309]]></out_refund_no>
	<out_trade_no><![CDATA[71106718111915575302817]]></out_trade_no>
	<refund_account><![CDATA[REFUND_SOURCE_RECHARGE_FUNDS]]></refund_account>
	<refund_fee><![CDATA[3960]]></refund_fee>
	<refund_id><![CDATA[50000408942018111907145868882]]></refund_id>
	<refund_recv_accout><![CDATA[支付用户零钱]]></refund_recv_accout>
	<refund_request_source><![CDATA[API]]></refund_request_source>
	<refund_status><![CDATA[SUCCESS]]></refund_status>
	<settlement_refund_fee><![CDATA[3960]]></settlement_refund_fee>
	<settlement_total_fee><![CDATA[3960]]></settlement_total_fee>
	<success_time><![CDATA[2018-11-19 16:24:13]]></success_time>
	<total_fee><![CDATA[3960]]></total_fee>
	<transaction_id><![CDATA[4200000215201811190261405420]]></transaction_id>
	</root>

	`, "\\n", "\n", -1)
	mv, err := mxj.NewMapXml([]byte(xmlStr))
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(mv, false))
}

func TestMultiProfitSharing(t *testing.T) {
	result, err := api.MultiProfitSharing(&MultiProfitSharingParam{
		TransactionID: "4200000506202002122465150534",
		OutOrderNo:    "A18546A84D6111EA8D8A7824AF852DFE",
		Receivers:     `[{"type":"PERSONAL_OPENID","account":"ojWb10ND2JBY_hOic5phWQNChWC0","amount":1,"description":"分到个人"}]`,
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestFinishProfitSharing(t *testing.T) {
	result, err := api.FinishProfitSharing(&FinishProfitSharingParam{
		TransactionID: "4200000506202002122465150534",
		OutOrderNo:    "A18546A84D6111EA8D8A7824AF852DFE",
		Description:   "测试",
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestTransfers(t *testing.T) {
	orderNo := utils.GetUUID()
	result, err := api.Transfers(&TransfersParam{
		PartnerTradeNo: orderNo,
		OpenID:         "o2dL05DjZggIr2nStjgHWa9oqQBc",
		CheckName:      "NO_CHECK",
		Amount:         1,
		Desc:           "测试",
		SpbillCreateIP: "171.217.98.243",
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestAddProfitSharingReceiver(t *testing.T) {
	result, err := api.AddProfitSharingReceiver(&ProfitSharingReceiverParam{
		Receiver: `{
			"type": "PERSONAL_OPENID",
			"account":"ojWb10ND2JBY_hOic5phWQNChWC0",
			"relation_type": "STAFF"
		}`,
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestDeleteProfitSharingReceiver(t *testing.T) {
	result, err := api.DeleteProfitSharingReceiver(&ProfitSharingReceiverParam{
		Receiver: `{
			"type": "PERSONAL_OPENID",
			"account":"ojWb10ND2JBY_hOic5phWQNChWC0"
		}`,
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}

func TestReturnProfitSharing(t *testing.T) {
	orderNo := utils.GetUUID()
	result, err := api.ReturnProfitSharing(&ReturnProfitSharingParam{
		OutOrderNo:        "A18546A84D6111EA8D8A7824AF852DFE",
		OutReturnNo:       orderNo,
		ReturnAccountType: "MERCHANT_ID",
		ReturnAccount:     "1390686702",
		ReturnAmount:      1000,
		Description:       "测试",
	})
	if err != nil {
		t.Fatal(err)
	}
	t.Log(utils.Format4Output(result, false))
}
