package jxutils

import (
	"fmt"
	"testing"

	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model"
	//"git.rosy.net.cn/jx-callback/globals/testinit"
)

//func init() {
//	testinit.Init()
//}

func TestEarthDistance(t *testing.T) {
	lng1, lat1 := 118.796877, 32.060255
	lng2, lat2 := 116.407395, 39.904211
	distance := EarthDistance(lng1, lat1, lng2, lat2)
	fmt.Print(distance)
	fmt.Print("测试中....")
}

func TestMapValue2Scope(t *testing.T) {
	result := MapValue2Scope(-4, -10, 0, 0, 100)
	if result != 60 {
		t.Fatalf("result:%d is wrong", result)
	}

	result = MapValue2Scope(-4, 0, 10, 0, 100)
	if result != 0 {
		t.Fatalf("result:%d is wrong", result)
	}

	result = MapValue2Scope(100, 0, 10, 0, 100)
	if result != 100 {
		t.Fatalf("result:%d is wrong", result)
	}
}

func TestGetNameAndUnitFromSkuName(t *testing.T) {
	name, unit := GetNameAndUnitFromSkuName("【满59免运】蒜苔肉丝约400g/个(蒜苔约250g 肉丝约150g/份)")
	if name != "【满59免运】蒜苔肉丝约400g" || unit != "个" {
		t.Fatalf("TestGetNameAndUnitFromSkuName wrong, name:%s, unit:%s", name, unit)
	}
	name, unit = GetNameAndUnitFromSkuName("【满59免运】蒜苔肉丝约400g/g份(蒜苔约250g 肉丝约150g/份)")
	if name != "【满59免运】蒜苔肉丝约400g" || unit != "g" {
		t.Fatalf("TestGetNameAndUnitFromSkuName wrong, name:%s, unit:%s", name, unit)
	}
	name, unit = GetNameAndUnitFromSkuName("【满59免运】蒜苔肉丝约400g/个")
	if name != "【满59免运】蒜苔肉丝约400g" || unit != "个" {
		t.Fatalf("TestGetNameAndUnitFromSkuName wrong, name:%s, unit:%s", name, unit)
	}
	name, unit = GetNameAndUnitFromSkuName("【满59免运】蒜苔肉丝约400g/")
	if name != "【满59免运】蒜苔肉丝约400g" || unit != "份" {
		t.Fatalf("TestGetNameAndUnitFromSkuName wrong, name:%s, unit:%s", name, unit)
	}
	name, unit = GetNameAndUnitFromSkuName("【满59免运】蒜苔肉丝约400g")
	if name != "【满59免运】蒜苔肉丝约400g" || unit != "份" {
		t.Fatalf("TestGetNameAndUnitFromSkuName wrong, name:%s, unit:%s", name, unit)
	}
}

func TestSplitSkuName(t *testing.T) {
	prefix, name, comment, specUnit, unit, specQuality := SplitSkuName("[好吃]黄骨鱼约600G/非份(黄辣丁、昂刺鱼)")
	if prefix != "好吃" || name != "黄骨鱼" || comment != "黄辣丁、昂刺鱼" || specUnit != "g" || unit != "非份" || specQuality != 600 {
		t.Fatalf("SplitSkuName wrong, name:%s, unit:%s", name, unit)
	}
	prefix, name, comment, specUnit, unit, specQuality = SplitSkuName("【满59免运】蒜苔肉丝约400g")
	if prefix != "满59免运" || name != "蒜苔肉丝" || comment != "" || specUnit != "g" || unit != "份" || specQuality != 400 {
		t.Fatalf("SplitSkuName wrong, name:%s, unit:%s", name, unit)
	}
	prefix, name, comment, specUnit, unit, specQuality = SplitSkuName("【满59免运】蒜苔肉丝约400.1ML/")
	if prefix != "满59免运" || name != "蒜苔肉丝" || comment != "" || specUnit != "ml" || unit != "份" || specQuality != 400.1 {
		t.Fatalf("SplitSkuName wrong, name:%s, unit:%s", name, unit)
	}
}

func TestSplitUniversalOrderID(t *testing.T) {
	testData := [][]interface{}{
		[]interface{}{
			"800402581000221",
			"800402581000221",
			model.VendorIDJD,
		},
		[]interface{}{
			"3022716176275221584" + "|" + utils.Int2Str(model.VendorIDJD),
			"3022716176275221584",
			model.VendorIDJD,
		},
		[]interface{}{
			"3022716176275221584",
			"3022716176275221584",
			model.VendorIDELM,
		},
		[]interface{}{
			"15380342248732",
			"15380342248732",
			model.VendorIDEBAI,
		},
		[]interface{}{
			"33437032333978492",
			"33437032333978492",
			model.VendorIDMTWM,
		},
		[]interface{}{
			"5287873015048",
			"5287873015048",
			model.VendorIDWSC,
		},
	}
	for _, v := range testData {
		universalOrderID := v[0].(string)
		desiredOrderID := v[1].(string)
		desiredVendorID := v[2].(int)
		orderID, vendorID := SplitUniversalOrderID(universalOrderID)
		if orderID != desiredOrderID || vendorID != desiredVendorID {
			t.Fatalf("%s is not ok, orderID:%s, desiredOrderID:%s, vendorID:%d, desiredVendorID:%d", universalOrderID, orderID, desiredOrderID, vendorID, desiredVendorID)
		}
	}
}

func TestGetPossibleVendorIDFromVendorOrderID(t *testing.T) {
	testData := [][]interface{}{
		[]interface{}{
			"800402581000221",
			model.VendorIDJD,
		},
		[]interface{}{
			"X00402581000221",
			model.VendorIDUnknown,
		},
		[]interface{}{
			"8004025810002210",
			model.VendorIDUnknown,
		},
		[]interface{}{
			"3022716176275221584",
			model.VendorIDEBAI,
		},
		[]interface{}{
			"302271617627522158X",
			model.VendorIDUnknown,
		},
		[]interface{}{
			"15380342248732",
			model.VendorIDEBAI,
		},
		[]interface{}{
			"33437032333978492",
			model.VendorIDMTWM,
		},
		[]interface{}{
			"5287873015048",
			model.VendorIDWSC,
		},
	}
	for _, v := range testData {
		universalOrderID := v[0].(string)
		desiredVendorID := v[1].(int)
		vendorID := GetPossibleVendorIDFromVendorOrderID(universalOrderID)
		if vendorID != desiredVendorID {
			t.Fatalf("%s is not ok, should be:%d, but it's:%d", universalOrderID, desiredVendorID, vendorID)
		}
	}
}

func TestGetLastTimeFromList(t *testing.T) {
	timeList := []string{
		"08:00:00",
		"23:00:00",
	}
	time1 := GetLastTimeFromList(utils.Str2Time("2019-06-17 07:59:59"), timeList)
	if time1.Sub(utils.Str2Time("2019-06-16 23:00:00")) != 0 {
		t.Fatal("GetLastTimeFromList wrong1")
	}
	time1 = GetLastTimeFromList(utils.Str2Time("2019-06-17 08:59:59"), timeList)
	if time1.Sub(utils.Str2Time("2019-06-17 08:00:00")) != 0 {
		t.Fatal("GetLastTimeFromList wrong2")
	}

	time1 = GetNextTimeFromList(utils.Str2Time("2019-06-17 08:59:59"), timeList)
	if time1.Sub(utils.Str2Time("2019-06-17 23:00:00")) != 0 {
		t.Fatal("GetNextTimeFromList wrong1")
	}
	time1 = GetNextTimeFromList(utils.Str2Time("2019-06-17 23:01:59"), timeList)
	if time1.Sub(utils.Str2Time("2019-06-18 08:00:00")) != 0 {
		t.Fatal("GetNextTimeFromList wrong2")
	}

}

func TestGetShortNameFromURL(t *testing.T) {
	for _, v := range [][]string{
		[]string{
			"20b569a7cf7e8045b2d7bb550900cf86.jpeg",
			"http://image.jxc4.com/20b569a7cf7e8045b2d7bb550900cf86.jpeg?imageMogr2/thumbnail/x800/gravity/Center/crop/800x800",
		},
		[]string{
			"91b8dcb5cb6b462bc96078b7210470b7.jpg",
			"http://image.jxc4.com/image/91b8dcb5cb6b462bc96078b7210470b7.jpg",
		},
	} {
		if str := GetShortNameFromURL(v[1]); str != v[0] {
			t.Errorf("%s failed, result:%s, expect:%s", v[1], str, v[0])
		}
	}
}

func TestCalcStageValue(t *testing.T) {
	type tTestType struct {
		DesiredValue float64
		Params1      [][]float64
		Params2      float64
	}
	priceStage := [][]float64{
		[]float64{
			7,
			300,
		},
		[]float64{
			5,
			200,
		},
		[]float64{
			3,
			100,
		},
	}
	for _, v := range []*tTestType{
		&tTestType{
			DesiredValue: 0,
			Params1:      priceStage,
			Params2:      3,
		},
		&tTestType{
			DesiredValue: 0,
			Params1:      priceStage,
			Params2:      0,
		},
		&tTestType{
			DesiredValue: 200,
			Params1:      priceStage,
			Params2:      5,
		},
		&tTestType{
			DesiredValue: 400,
			Params1:      priceStage,
			Params2:      5.01,
		},
		&tTestType{
			DesiredValue: 600,
			Params1:      priceStage,
			Params2:      7,
		},
	} {
		value := CalcStageValue(v.Params1, v.Params2)
		if value != v.DesiredValue {
			t.Errorf("DesiredValue:%f, value:%f", v.DesiredValue, value)
		}
	}
}

func TestGetOneEmailFromStr(t *testing.T) {
	for _, v := range [][]string{
		[]string{
			"test@kk.com",
			"中人国test@kk.com",
		},
		[]string{
			"",
			"91b8dcb5cb6b462bc96078b7210470b7.jpg",
		},
		[]string{
			"test@kk.com",
			"中人国test@kk.com2342",
		},
		[]string{
			"donald.trump_1-3@163.com",
			"中人国donald.trump_1-3@163.com2342",
		},
	} {
		if str := GetOneEmailFromStr(v[1]); str != v[0] {
			t.Errorf("%s failed, result:%s, expect:%s", v[1], str, v[0])
		}
	}
}

func TestGenFakeUPC(t *testing.T) {
	for _, v := range [][]string{
		[]string{
			"6666000298034",
			"29803",
		},
		[]string{
			"6666076543212",
			"7654321",
		},
	} {
		if str := GenFakeUPC(int(utils.Str2Int64(v[1]))); str != v[0] {
			t.Errorf("%s failed, result:%s, expect:%s", v[1], str, v[0])
		}
	}
	if !IsUpcValid("6666076543212") {
		t.Fatal("wrong1")
	}
	if IsUpcValid("6666076543210") {
		t.Fatal("wrong2")
	}
}

func TestComposeSkuNameOriginal(t *testing.T) {
	for _, v := range [][]string{
		[]string{
			"单独商品名3g/件(商品注释)",
			"",
			"单独商品名",
			"商品注释",
			"件",
			"3.0",
			"g",
			"0",
		},
		[]string{
			"[高级]单独商品名约3g/份",
			"高级",
			"单独商品名",
			"",
			"份",
			"3.0",
			"g",
			"0",
		},
	} {
		if str := ComposeSkuNameOriginal(v[1], v[2], v[3], v[4], float32(utils.Str2Float64(v[5])), v[6], int(utils.Str2Int64(v[7]))); str != v[0] {
			t.Errorf("%s failed, result:%s, expect:%s", v[1], str, v[0])
		}
	}
}
