package ebaiapi

import (
	"testing"

	"git.rosy.net.cn/baseapi/utils"
)

func TestShopCategoryCreate(t *testing.T) {
	result, err := api.ShopCategoryCreate(testShopID, 0, "绿色蔬菜", 16)
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestShopCategoryGet(t *testing.T) {
	result, err := api.ShopCategoryGet("300034")
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestShopCategoryUpdate(t *testing.T) {
	err := api.ShopCategoryUpdate(testShopID, 153760204017121, "水果2", 2)
	if err != nil {
		t.Fatal(err)
	}
}

func TestShopCategoryDelete(t *testing.T) {
	err := api.ShopCategoryDelete(testShopID, 1538315352106929)
	if err != nil {
		t.Fatal(err)
	}
}

func TestSkuGetItemsByCategoryId(t *testing.T) {
	result, err := api.SkuGetItemsByCategoryId(testShopID, 153838813210900)
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestSkuList(t *testing.T) {
	result, err := api.SkuList("667544", &SkuListParams{})
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestSkuUploadRTF(t *testing.T) {
	rtfDetail := BuildRFTFromImgs("https://image.jxc4.com/sijidou.jpg")
	t.Log(rtfDetail)
	result, err := api.SkuUploadRTF("2", rtfDetail)
	if err != nil {
		t.Fatal(err)
	}
	t.Log(result)
}

func TestSkuCreate(t *testing.T) {
	result, err := api.SkuCreate("", testShopID, 17, map[string]interface{}{
		"name":         "测试商品",
		"status":       SkuStatusOnline,
		"left_num":     MaxLeftNum,
		"sale_price":   100,
		"market_price": 100,
		"cat1_id":      151301831158987,
		"cat2_id":      15347484581335,
		"cat3_id":      15347484581339,
		"encrypt":      "",
		"sign":         "A97439C8B502BB7197EC7E5CED53C158",
		"source":       "34665",
		"ticket":       "E700820C-8FDC-4A8B-A920-38ACAA01C332",
		"timestamp":    1639390432,
		"traceid":      "0bc5f41d16393904320001232e5843",
		"version":      "3",
		"photos": []map[string]interface{}{
			map[string]interface{}{
				"is_master": 1,
				"url":       "http://image.jxc4.com/8536c76779333f53517006f88ea7174c.jpg?imageView2/1/w/80/h/80",
			},
		},
	})
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestSkuUpdate(t *testing.T) {
	// 15579787500720732 高级

	result, err := api.SkuUpdate(utils.GetUUID(), "2", 1557043939079105, map[string]interface{}{
		// "name": "高级商品2015a333约1100g/份",
		// "rtf": "http://www.rosy.net.cn/rtf.html",
		"shelf_number": 12,
	})
	if err != nil {
		t.Fatal(err)
	} else {
		t.Log(utils.Format4Output(result, false))
	}
}

func TestSkuShopCategoryMap(t *testing.T) {
	err := api.SkuShopCategoryMap(testShopID, 0, "2212", 15637192270716, 100)
	if err != nil {
		t.Fatal(err)
	}
}

func TestSkuDelete(t *testing.T) {
	const (
		notExistSkuID = 12345678
		existSkuID    = 156406677407848
	)
	opResult, err := api.SkuDelete(utils.GetUUID(), testShopID, []int64{notExistSkuID}, nil)
	t.Log(utils.Format4Output(opResult, false))
	if err == nil {
		t.Log("应该要报错")
	}
	if opResult == nil || len(opResult.FailedList) != 1 || opResult.FailedList[0].SkuID != notExistSkuID {
		t.Logf("错误结果中应该要包含:%d", notExistSkuID)
	}
	opResult, err = api.SkuDelete(utils.GetUUID(), testShopID, []int64{existSkuID}, nil)
	t.Log(utils.Format4Output(opResult, false))
	if err != nil {
		t.Fatal(err)
	}
	if opResult == nil || len(opResult.SuccessList) != 1 || opResult.SuccessList[0].SkuID != existSkuID {
		t.Logf("成功结果中应该要包含:%d", existSkuID)
	}
}

func TestSkuOnline(t *testing.T) {
	const (
		notExistSkuID = 12345678
		existSkuID    = 156406688807623
	)

	opResult, err := api.SkuOnline(utils.GetUUID(), testShopID, []int64{notExistSkuID, existSkuID}, nil, nil)
	t.Log(utils.Format4Output(opResult, false))
	t.Log(err.Error())
	if err == nil {
		t.Log("应该要报错")
	}
	if opResult == nil || len(opResult.FailedList) != 1 || opResult.FailedList[0].SkuID != notExistSkuID {
		t.Logf("错误结果中应该要包含:%d", notExistSkuID)
	}
	if opResult == nil || len(opResult.SuccessList) != 1 || opResult.SuccessList[0].SkuID != existSkuID {
		t.Logf("成功结果中应该要包含:%d", existSkuID)
	}
}

func TestSkuOnlineOne(t *testing.T) {
	err := api.SkuOnlineOne(utils.GetUUID(), testShopID, 13211, "", "")
	if err == nil {
		t.Fatal("应该要报错才对")
	}
}

func TestSkuOffline(t *testing.T) {
	opResult, err := api.SkuOffline(utils.GetUUID(), testShopID, []int64{1564049914071288, 156389470507185}, nil, nil)
	t.Log(utils.Format4Output(opResult, false))
	if err != nil {
		t.Fatal(err)
	}
}

func TestSkuPriceUpdateBatch(t *testing.T) {
	opResult, err := api.SkuPriceUpdateBatch(utils.GetUUID(), testShopID, ShopSkuInfoList{
		&ShopSkuInfo{
			SkuID:     156369111807787,
			SalePrice: 100,
		},
		&ShopSkuInfo{
			SkuID:     156369111707770,
			SalePrice: 100,
		},
		&ShopSkuInfo{
			SkuID:     12321,
			SalePrice: 100,
		},
	}, SkuIDTypeSkuID)
	t.Log(utils.Format4Output(opResult, false))
	if err == nil {
		t.Fatal("应该要报错才对")
	}
	if len(opResult.FailedList) != 3 {
		t.Fatal("错误列表不正确")
	}
}

func TestSkuStockUpdateBatch(t *testing.T) {
	const (
		notExistSkuID = 12345678
		existSkuID    = 156406688807623

		leftNum = 123
	)

	opResult, err := api.SkuStockUpdateBatch(utils.GetUUID(), testShopID, ShopSkuInfoList{
		&ShopSkuInfo{
			SkuID: notExistSkuID,
			Stock: leftNum,
		},
		&ShopSkuInfo{
			SkuID: existSkuID,
			Stock: leftNum,
		},
	}, SkuIDTypeSkuID)
	t.Log(utils.Format4Output(opResult, false))
	if err == nil {
		t.Fatal("应该要报错才对")
	}
	if len(opResult.FailedList) != 1 {
		t.Fatal("错误列表不正确")
	}

	skuInfo, err := api.SkuList(testShopID, &SkuListParams{
		SkuID: existSkuID,
	})
	if err != nil {
		t.Fatal(err)
	}
	if skuInfo.List[0].LeftNum != leftNum {
		t.Fatal("设置库存失败")
	}
}

func TestSkuStockUpdateOne(t *testing.T) {
	const (
		notExistSkuID = 12345678
		existSkuID    = 156406688807623

		leftNum = 234
	)
	err := api.SkuStockUpdateOne(utils.GetUUID(), testShopID, &ShopSkuInfo{
		SkuID: existSkuID,
		Stock: leftNum,
	})
	if err != nil {
		t.Fatal(err)
	}
	skuInfo, err := api.SkuList(testShopID, &SkuListParams{
		SkuID: existSkuID,
	})
	if err != nil {
		t.Fatal(err)
	}
	if skuInfo.List[0].LeftNum != leftNum {
		t.Fatal("设置库存失败")
	}
}
func TestGetEbaiCatIDFromName(t *testing.T) {
	ebaiCatID := api.GetEbaiCatIDFromName("300280", "应季水果新鲜美味")
	t.Log(ebaiCatID)
}

func TestGetEbaiSkuIDFromCustomID(t *testing.T) {
	ebaiCatID := api.GetEbaiSkuIDFromCustomID("300001", "31554")
	t.Log(ebaiCatID)
}
