package jxutils

import (
	"fmt"
	"testing"

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

func TestSplitSlice(t *testing.T) {
	testValue1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	list := utils.Interface2Slice(testValue1)
	result := SplitSlice(list, 3)
	if !(len(result) == 4 && len(result[3]) == 1) {
		t.Log("result is not ok")
	}

	testValue2 := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}
	list = utils.Interface2Slice(testValue2)
	result = SplitSlice(list, 1)
	if !(len(result) == 10 && len(result[3]) == 1) {
		t.Log("result is not ok")
	}
	t.Log(result)
}

func TestGetPolygonFromCircle(t *testing.T) {
	points := GetPolygonFromCircle(104.054727, 30.575362, 1000, 8)
	for _, v := range points {
		t.Log(fmt.Sprintf("lng=%f&lat=%f", v[0], v[1]))
	}
	t.Log(points)
}

func TestFakeID(t *testing.T) {
	id := GenFakeID()
	if !IsFakeID(id) {
		t.Fatalf("wrong result for id:%d", id)
	}
	id = 0
	if !IsFakeID(id) {
		t.Fatalf("wrong result for id:%d", id)
	}
	id = 23424234
	if IsFakeID(id) {
		t.Fatalf("wrong result for id:%d", id)
	}
}

func TestIsStringLikeMobile(t *testing.T) {
	if !IsStringLikeMobile("13888888888") {
		t.Fatal("wrong 1")
	}
	if IsStringLikeMobile("13888888888-123") {
		t.Fatal("wrong 2")
	}
	if IsStringLikeMobile("13888888888,123") {
		t.Fatal("wrong 3")
	}
	if IsStringLikeMobile("138****8888") {
		t.Fatal("wrong 4")
	}
}

func TestSplitStoreName(t *testing.T) {
	for _, v := range [][]string{
		[]string{
			"京西菜市 站北店",
			"京西菜市",
			"站北店",
		},
		[]string{
			"京西菜市(站北店)",
			"京西菜市",
			"站北店",
		},
		[]string{
			"京西菜市[(站北店",
			"京西菜市",
			"站北店",
		},
		[]string{
			"京西菜市  站北店)",
			"京西菜市",
			"站北店",
		},
		[]string{
			"京西菜市-站北店",
			"京西菜市",
			"站北店",
		},
		[]string{
			"站北店",
			"京西菜市",
			"站北店",
		},
	} {
		prefix, bareName := SplitStoreName(v[0], "-", "京西菜市")
		if prefix != v[1] || bareName != v[2] {
			t.Fatalf("SplitStoreName(%s) is wrong, %s, %s", v[0], prefix, bareName)
		}
	}
}

func TestCalcPolygonAreaAutonavi(t *testing.T) {
	// pointers := GetPolygonFromCircle(104.065702, 30.657488, 3000, 128)
	// area := CalcPolygonAreaAutonavi(pointers)
	// t.Logf("area:%f", area)
	// db := dao.GetDB()
	// storeList, err := dao.GetStoreList(db, nil, nil, "")
	// if err != nil {
	// 	t.Fatal(err)
	// }

	// strBuilder := &strings.Builder{}
	// strBuilder.WriteString("\n")
	// for _, v := range storeList {
	// 	if v.DeliveryRangeType == model.DeliveryRangeTypePolygon {
	// 		pointers := CoordinateStr2Points(v.DeliveryRange)
	// 		area1 := CalcPolygonAreaAutonavi(pointers)
	// 		strBuilder.WriteString(fmt.Sprintf("%d,%f\n", v.ID, area1))
	// 	}
	// }
	// t.Log(strBuilder.String())
}

func TestCaculateSkuPrice(t *testing.T) {
	type tTestInfo struct {
		DesiredPrice int
		UnitPrice    int
		SpecQuality  float32
		SpecUnit     string
		Unit         string
	}
	for _, v := range []*tTestInfo{
		&tTestInfo{
			DesiredPrice: 458,
			UnitPrice:    915,
			SpecQuality:  250,
			SpecUnit:     "g",
			Unit:         "份",
		},
		&tTestInfo{
			DesiredPrice: 123,
			UnitPrice:    123,
			SpecQuality:  888,
			SpecUnit:     "g",
			Unit:         "个",
		},
		&tTestInfo{
			DesiredPrice: 1,
			UnitPrice:    5,
			SpecQuality:  1,
			SpecUnit:     "g",
			Unit:         "份",
		},
	} {
		price := CaculateSkuPrice(v.UnitPrice, v.SpecQuality, v.SpecUnit, v.Unit)
		if price != v.DesiredPrice {
			t.Errorf("price:%d,desiredPrice:%d", price, v.DesiredPrice)
		}
	}
}

func TestGetPricePercentage(t *testing.T) {
	type tTestInfo struct {
		DesiredPrice int
		UnitPrice    int
		SpecQuality  float32
		SpecUnit     string
		Unit         string
	}

	l := []*model.PricePercentageItem{
		&model.PricePercentageItem{
			BeginPrice:      0,
			PricePercentage: 0,
			PriceAdd:        0,
		},
		&model.PricePercentageItem{
			BeginPrice:      10,
			PricePercentage: 10,
			PriceAdd:        1,
		},
		&model.PricePercentageItem{
			BeginPrice:      20,
			PricePercentage: 20,
			PriceAdd:        2,
		},
		&model.PricePercentageItem{
			BeginPrice:      30,
			PricePercentage: 30,
			PriceAdd:        3,
		},
		&model.PricePercentageItem{
			BeginPrice:      60,
			PricePercentage: 60,
			PriceAdd:        6,
		},
	}

	for _, v := range [][]int{
		[]int{88, 0, -1, 88},
		[]int{0, 0, 0, 0},
		[]int{30, 3, 40, 0},
		[]int{20, 2, 25, 0},
		[]int{10, 1, 10, 0},
		[]int{60, 6, 60, 0},
		[]int{60, 6, 1000, 10},
	} {
		pricePercentage, priceAdd := GetPricePercentage(l, v[2], v[3])
		if pricePercentage != v[0] || priceAdd != v[1] {
			t.Errorf("price:%d, defPricePercentage:%d, expected pricePercentage:%d, priceAdd:%d, actual pricePercentage:%d, priceAdd:%d",
				v[2], v[3], v[0], v[1], pricePercentage, priceAdd)
		}
		t.Logf("%d  %d,%d,%d\n", CaculateSkuVendorPrice(v[2], pricePercentage, priceAdd), v[2], pricePercentage, priceAdd)
	}
}

func TestCaculateJxPriceByPricePack(t *testing.T) {
	type tTestInfo struct {
		DesiredPrice int
		UnitPrice    int
		SpecQuality  float32
		SpecUnit     string
		Unit         string
	}

	l := []*model.PricePercentageItem{
		&model.PricePercentageItem{
			BeginPrice:      0,
			PricePercentage: 0,
			PriceAdd:        0,
		},
		&model.PricePercentageItem{
			BeginPrice:      10,
			PricePercentage: 10,
			PriceAdd:        1,
		},
		&model.PricePercentageItem{
			BeginPrice:      20,
			PricePercentage: 20,
			PriceAdd:        2,
		},
		&model.PricePercentageItem{
			BeginPrice:      30,
			PricePercentage: 30,
			PriceAdd:        3,
		},
		&model.PricePercentageItem{
			BeginPrice:      60,
			PricePercentage: 60,
			PriceAdd:        6,
		},
	}

	for _, v := range [][]int{
		[]int{1, 1, 88},
		[]int{1, 1, 0},
		[]int{40, 15, 0},
		[]int{25, 7, 0},
		[]int{10, 2, 0},
		[]int{60, 42, 0},
		[]int{1000, 606, 10},
	} {
		jxPrice := CaculateJxPriceByPricePack(l, v[2], v[1])
		if CaculatePriceByPricePack(l, v[2], jxPrice) != v[1] {
			t.Fatalf("vendorPrice:%d, defPercentage:%d, expected:%d, actual:%d", v[1], v[2], v[0], jxPrice)
			break
		}
	}
}
