package ebaiapi

import (
	"net/url"
	"strings"
	"testing"

	"git.rosy.net.cn/baseapi"

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

const (
	testShopBaiduID = 2267254343
	testShopID      = "2"
)

var (
	api         *API
	sugarLogger *zap.SugaredLogger
)

func init() {
	logger, _ := zap.NewDevelopment()
	sugarLogger = logger.Sugar()
	baseapi.Init(sugarLogger)
	// sandbox，果园测试门店
	// api = New("62289", "d3ec2358d6a819ea")

	// 京西菜市
	api = New("34665", "c3db75b754ea2d89")

	//菜市测试
	// api = New("62923", "aa4cdc6c1108486b")

	// 京西果园
	//api = New("35957", "10013fbb7c2ddad7")
	// api.SetCookie("PASSPORT_DELIMONT_TOKEN", "PBE_2.0_5cd1c6141c127d4188f026ac01fc93656266683e8dfb3127c2fdf894259e9034125ff3bdd2a997a385802ee3ef1802ba93a04acea34fde2d2b6e802c5dcd4ec6e3f4ad909a1d806e3ceeb349ed726b03d60ed1fe7010d4140aa338d9c5f05e3fec172c78d3d7f0ca579d61b7015af1bf99aa46b04d2b8a64aa50646dc09afe94b6b60e0ba9a933635db5e8b2a035e9b6d693b289acf1b256d5b9a3f8478c87b0b009115bfd1394f20bb5a0dc2c07b8d013a25f286ec6bf7f2d86010d65507e31358834b7a6b58fbd88cb3f1a12cf71c997b91c1527f6f3c10693f7c2bd6073da8633a98cd2dc1114dfa5be5ee0e60b02cf7e4a94d0fb563a8c01717e7c050f02249117219c07a2eb211577c208ba77f4d536fa25139bc249be93b38d6fc495ef67a32aa206835d177db402bc534de1d29caf4f6b4fbcd912c13f167d00d1732222744c336a5189728f72fb5e153c4b1164171cfb0c811f34f4c2fedd43f721b8706b43f8d631251c")
	//api.SetCookie("WMUSS", "NTE2NDMTAwMDAyNzYxNzAwOTEyTmUzNWJNcDlQ ")
	//api.SetCookie("WMSTOKEN", "NTE2NDMTAwMDAyNzYxNzAwOTEyTmUzNWJNcDlQ ")
	api.SetCookieWithStr(`
UTUSER=0; cna=A9cHGZw1jjgCAd3tly+uuYIG; ubt_ssid=kpqd18ueqc12rljyipjmobcr5vlc3xry_2021-04-23; t=bc6b1c1fc4e7ef841fff494286415f8b; OPENUSS=N2NKOTMTAwMDAyNzYxNzAwOTEyTmpod0RtaTlQ; SWITCH_SHOP=%2F6bac%2BAb1bPK6koBkfv6k08FDLdMxARhpTN8yXhXvoflklVfUmxnSim7ZNZjEGNB8JBNv5vn946%2B7HtPrNmkcz5R4iSP9JCINIhfBd0fwz5lR%2B7DY72DtAidguPGmM778RF0rUhnjJhVMiuIe2Kt8OA%2FUTnG59pQNWfCtVjpfmQvNdtU%2FVcr1%2FWwDrbtAY2v; _m_h5_tk=a77351c2911f8484ff7c2a4c9c8def83_1629200451933; _m_h5_tk_enc=c113d3c67a3d35821adc914d799a2d6d; xlly_s=1; WMUSS=MJLLN2MTAwMDAyNzYxNzAwOTEyTmxrMm9PYzBQ; WMSTOKEN=MJLLN2MTAwMDAyNzYxNzAwOTEyTmxrMm9PYzBQ; OUTER_AUTH_LOGIN=MJLLN2MTAwMDAyNzYxNzAwOTEyTmxrMm9PYzBQ%3BMJLLN2MTAwMDAyNzYxNzAwOTEyTmxrMm9PYzBQ; EGG_SESS=m6drJjNW1sXiQ8e5om0V3oO39ZNOOo-XsaxKm6yWxBrYOA5xU8GuwFtZME4VD8yx; tfstk=c54GBNifxlo_sxn3VNg__oi3ULgNZnLqErzQ8rUOInR02YUFiXKe4_WdtA2gkn1..; l=eBgWAKP7jpZewuqWBO5Z-urza77TFIdf5sPzaNbMiInca1uR0UXMSNCKItmXUdtjgtCf1eKPrwlgwRButnzdgnbuzDKV9ydtnxJO.; isg=BLGxXdb4dFu3btnHkQqSlG7TwD1LniUQBRshF5PHB3jvutgM2u5d4m2c3E7ccr1I
`)
}

func TestTest(t *testing.T) {
	sugarLogger.Debug(utils.GetCurTimeStr())
}

func TestSign(t *testing.T) {
	reqBody := "cmd=order.create&timestamp=1538098573&version=3&ticket=F6338922-44FB-2D30-9CDA-5E9AE2604C54&source=63032&body={\"order_id\":\"15380983922052\"}&sign=E9042A52120BFD3D0B448C5F50A1BA55&encrypt="
	kvs := strings.Split(reqBody, "&")
	values := make(url.Values, len(kvs))
	for _, v := range kvs {
		kv := strings.Split(v, "=")
		values[kv[0]] = []string{kv[1]}
	}
	values["secret"] = []string{api.secret}
	sign := api.signParams(values)
	t.Log(sign)
}

func TestAccessAPI(t *testing.T) {
	//
	result, err := api.AccessAPI("shop.get", utils.Params2Map("baidu_shop_id", testShopBaiduID))
	if err != nil {
		t.Fatalf("Error when accessing AccessAPI result:%v, error:%v", result, err)
	} else {
		t.Log(utils.Format4Output(result, false))
		// shopInfo := result.Data.(map[string]interface{})
		// if len(shopInfo) > 0 {
		// 	t.Fatalf("data is not correct:%v", shopInfo)
		// }
	}
}
