package uuptapi

import (
	"fmt"
	"git.rosy.net.cn/baseapi"
	"git.rosy.net.cn/baseapi/utils"
	"go.uber.org/zap"
	"math"
	"strings"
	"testing"
)

var (
	//沙箱环境
	//api = New("ccba8bd4a2d54a2fb6df97e87979f303", "2815a7a1f8e3405d81fd6263683ec4e7", "910a0dfd12bb4bc0acec147bcb1ae246")
	//正式环境
	api         = New("55c4542ae60e4d348edcfc93b06dd302", "76b362c06b1b4baa9e47bab6387a5356", "8d8464e7c9354c1e88a3f5afa2a7922e")
	sugarLogger *zap.SugaredLogger
)

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

	//api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "cabrXQf9eFMVWVYg4hNlwu")
	//token, _ := api.GetAccessToken()
	//api.accessToken = token.BusinessDataObj.AccessToken
}

//
//func TestSignParam(t *testing.T) {
//	testStr := map[string]interface{}{
//		"origin_id":     "T00001",
//		"from_address":  "郑州国际会展中心",
//		"from_usernote": "1层",
//		"to_address":    "商务外环路17号",
//		"to_usernote":   "1楼101",
//	}
//	ans := api.signParam(testStr)
//	fmt.Print(ans)
//}
//func TestRandStr(t *testing.T) {
//	fmt.Print(randStr())
//}
//
//计算运单价格
func TestGetWaybillPrice(t *testing.T) {
	param := &GetOrderPriceReq{
		OriginID:    "88442688797068",
		FromAddress: "成都市都江堰市中新巷四合院金杨调味",
		ToAddress:   "景中路宝石花石油花园天能电池(宝石花石油花园店)111",
		CityName:    "成都市",
		SendType:    "0",
		ToLat:       "30.985248",
		ToLng:       "103.638852",
		FromLat:     "103.639058",
		FromLng:     "103.728641",
		//ToLat:   "29.598951",
		//ToLng:   "103.736961",
		//FromLat: "29.602826",
		//FromLng: "103.722107",
	}
	price := utils.Struct2Map(param, "", false)
	resp, err := api.GetOrderPrice(price)
	fmt.Println(utils.Format4Output(resp, false))
	fmt.Println(err)
}

//发布订单
func TestAddOrder(t *testing.T) {
	param := &AddOrderReq{
		PriceToken:      "0215e844d582477191dfc5f6cd7dcb31",
		OrderPrice:      "9.50",
		BalancePayMoney: "8.07",
		Receiver:        "测试订单",
		ReceiverPhone:   "15680424816,9776",
		CallbackUrl:     uuCallbackURL,
		PushType:        "2", //测试订单
		SpecialType:     "0",
		CallMeWithTake:  "0",
	}
	order := utils.Struct2Map(param, "", false)
	orderCode, originID, err := api.AddOrder(order)
	fmt.Println("orderCode=====", orderCode)
	fmt.Println("originID=====", originID)
	fmt.Println("err=======", err)
}

//获取订单详情
func TestGetOrderDetail(t *testing.T) {
	detail, err := api.GetOrderDetail("23071316101004700004743106")
	fmt.Println(utils.Format4Output(detail, false))
	orderPrice := utils.Str2Float64(detail.OrderPrice)
	priceOff := utils.Str2Float64(detail.PriceOff)
	price := orderPrice - priceOff
	temp := int64(price * 100)
	fmt.Println(temp)
	fmt.Println(err)
}

//取消订单
func TestCancelOrder(t *testing.T) {
	err := api.CancelOrder("24011415121024100001986825", "测试取消")
	fmt.Println(err)
}

func Test(t *testing.T) {
	//a := "成都市"
	a := "都江堰市"
	b := "都江堰市"
	c := "都江堰市中新巷四合院金杨调味"
	d := ""
	if !strings.Contains(c, a) {
		d += a
		//fmt.Println(d)
	}
	if !strings.Contains(c, b) {
		d += b
	}
	d += c
	fmt.Println(d)

}

func TestGetOrderLiquidatedDamages(t *testing.T) {
	deductFee, err := api.GetOrderLiquidatedDamages("23022310041019000003804258", "2304613965000093")
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(deductFee)
	}
}
func TestChangeCoord(t *testing.T) {
	lng, lat := GaodeCoord2BaiDu(104.057697, 30.69524)
	fmt.Println(lng, lat)
}

func GaodeCoord2BaiDu(lng, lat float64) (float64, float64) {
	x := lng
	y := lat
	z := math.Sqrt(x*x+y*y) + 0.00002*math.Sin(y*math.Pi)
	theta := math.Atan2(y, x) + 0.000003*math.Cos(x*math.Pi)

	bdLng := z*math.Cos(theta) + 0.0065
	bdLat := z*math.Sin(theta) + 0.006

	return bdLng, bdLat
}
