package mtpsapi

import (
	"fmt"
	"strings"
	"testing"
	"time"

	"git.rosy.net.cn/baseapi"

	"git.rosy.net.cn/baseapi/utils"
	"go.uber.org/zap"
)

var (
	api         *API
	sugarLogger *zap.SugaredLogger
)

func init() {
	logger, _ := zap.NewDevelopment()
	sugarLogger = logger.Sugar()
	baseapi.Init(sugarLogger)

	// sandbox
	// api = New("25e816550bc9484480642f19a95f13fd", "r4$HqrKx9~=7?2Jfo,$Z~a7%~k!Au&pEdI2)oPJvSbH2ao@2N0[8wSIvtuumh_J^")
	// prod
	api = New("3c0a05d464c247c19d7ec13accc78605", "b1M}9?:sTbsB[OF2gNORnN(|(iy9rB8(`7]|[wGLnbmt`evfM>E:A90DjHAW:UPE")

	api.SetCookie("token", "DviwXn2qfstXdi64jbTES0MyEkyUQiCKHhWYLC2yb41BOl-NvIg0ffKJfUR58pGwEA-yB15Sxo02F-5iAbD2vw")
}

func handleError(t *testing.T, err error) {
	if err != nil {
		sugarLogger.Debug(err)
		t.Fatal(err.Error())
	}
}
func TestTest(t *testing.T) {
	sugarLogger.Debug(utils.GetCurTimeStr())
	tmpStr := "this is a\u00A0"
	sugarLogger.Debug(strings.Trim(tmpStr, " \u00A0"))
}

func TestAccessAPI(t *testing.T) {
	mtPeiSongId := "1529387562097059"
	params := map[string]interface{}{
		"delivery_id":   123456789,
		"mt_peisong_id": mtPeiSongId,
	}
	result, err := api.AccessAPI("order/status/query", params)
	if err != nil {
		t.Fatalf("Error when accessing AccessAPI result:%v, error:%v", result, err)
	} else {
		getMtPsId := result.Data["mt_peisong_id"].(string)
		if getMtPsId != mtPeiSongId {
			t.Fatalf("mt_peisong_id is not same, %v vs %v", mtPeiSongId, getMtPsId)
		}
	}
}

func TestCreateOrderByShop(t *testing.T) {
	basicParams := &CreateOrderByShopParam{
		DeliveryID: 123456789,
		OrderID:    "order_123456789",
		// 设置测试门店 id，测试门店的坐标地址为 97235456,31065079（高德坐标），配送范围3km
		ShopID: "test_0001",
		//DeliveryServiceCode: DeliveryServiceCodeIntime,
		ReceiverName:    "xjh",
		ReceiverAddress: "九里堤",
		ReceiverPhone:   "18112345678",
		ReceiverLng:     97235456,
		ReceiverLat:     31065079,
		CoordinateType:  CoordinateTypeMars,
		GoodsValue:      12.34,
		GoodsWeight:     3.4,
		OrderType:       OrderTypeASAP,
	}

	order, err := api.CreateOrderByShop2(basicParams)
	handleError(t, err)
	if order != nil {
		sugarLogger.Debugf("order:%v", order)
	}
}

func TestCancelOrder(t *testing.T) {
	result, err := api.CancelOrder(123456789, "1577258173664001659", CancelReasonMerchantOther, "just a test")
	handleError(t, err)
	sugarLogger.Debug(result)
}

func TestEvaluateRider(t *testing.T) {
	err := api.EvaluateRider(4614631, "1697594622186088963", 1, "提前点击送达")
	if err != nil {
		t.Fatal(err)
	}
}

func TestRiderLocation(t *testing.T) {
	lng, lat, err := api.RiderLocation(2997655, "1577100357518191619")
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("lng:%d,lat:%d", lng, lat)
}

func TestPreCreateByShop(t *testing.T) {
	_, _, err := api.PreCreateByShop(&PreCreateByShopParam{
		DeliveryID: 123456789,
		OrderID:    "order_123456789",
		// 设置测试门店 id，测试门店的坐标地址为 97235456,31065079（高德坐标），配送范围3km
		ShopID:               "667235",
		DeliveryServiceCode:  DeliveryServiceCodeRapid,
		ReceiverName:         "xjh",
		ReceiverAddress:      "九里堤",
		ReceiverPhone:        "18112345678",
		ReceiverLng:          113860710,
		ReceiverLat:          27628259,
		CoordinateType:       CoordinateTypeMars,
		GoodsValue:           12.34,
		GoodsWeight:          3.7,
		PayTypeCode:          0,
		OuterOrderSourceDesc: "103",
	})
	if err != nil {
		t.Fatal(err)
	}
	//sugarLogger.Debug(result)
}

func TestCheckOrder(t *testing.T) {
	_, err := api.CheckOrder("66820", "绍兴市柯桥区安昌镇西扆村濮家农贸市场", 120512730, 30129535, time.Now().Unix())
	//if err != nil {
	//	t.Fatal(err)
	//}
	fmt.Println(IsOpen(err))
}

func TestNumber(t *testing.T) {
	fmt.Println("1", -2&4 != 0)
	fmt.Println("2", 0&4 != 0)
	fmt.Println("3", 1&4 != 0)
	fmt.Println("4", 2&4 != 0)
	fmt.Println("5", 4&4 != 0)
	fmt.Println("6", 5&4 != 0)
}

func TestQueryOrderStatus(t *testing.T) {
	api.QueryOrderStatus(1100536572153699974, "1683539405488098818")
}
