package utils

import (
	"encoding/json"
	"testing"
	"time"
)

func TestConv(t *testing.T) {
	for _, v := range [][]interface{}{
		[]interface{}{
			json.Number("123"),
			int64(123),
		},
		[]interface{}{
			json.Number("abc"),
			int64(0),
		},
		[]interface{}{
			"123",
			int64(0),
		},
		[]interface{}{
			int64(123),
			int64(123),
		},
	} {
		data := Interface2Int64WithDefault(v[0], 0)
		if data != v[1].(int64) {
			t.Fatal("Interface2Int64WithDefault failed")
		}
	}
}

func TestMarshal(t *testing.T) {
	type InnerStruct struct {
		InnerIntData int
	}
	type OutStruct struct {
		InnerStruct
		IntData      int
		StrData      string
		InnerData    InnerStruct
		PtrInnerData *InnerStruct
	}
	type OutStruct2 struct {
		*InnerStruct
		IntData int
		StrData string
	}

	obj := OutStruct{
		InnerStruct: InnerStruct{
			InnerIntData: 3,
		},
		IntData: 1,
		StrData: "2",
		InnerData: InnerStruct{
			InnerIntData: 4,
		},
	}
	t.Log(Format4Output(obj, false))
	t.Log(Format4Output(Struct2FlatMap(obj), false))
	obj2 := OutStruct2{
		InnerStruct: &InnerStruct{
			InnerIntData: 2,
		},
	}

	t.Log(Format4Output(obj2, false))
	t.Log(Format4Output(Struct2FlatMap(obj2), false))
}

func TestTime(t *testing.T) {
	for _, v := range [][]interface{}{
		[]interface{}{
			"2019-04-01",
			Str2Time("2019-04-01 00:00:00"),
		},
	} {
		data := Str2Time(v[0].(string))
		if data != v[1].(time.Time) {
			t.Fatal("Interface2Int64WithDefault failed")
		}
	}
}

func TestStruct2MapByJson(t *testing.T) {
	type InnerKK struct {
		IntData int
		StrData string
		ObjData time.Time
	}
	type KK struct {
		IntData int
		A       int
		B       string
		C       time.Time
		InnerKK
		InnerKK2 InnerKK
	}
	kk := &KK{
		InnerKK: InnerKK{
			IntData: 1,
			StrData: "hello",
		},
	}
	mapData := Struct2MapByJson(kk)
	t.Log(Format4Output(mapData, false))
	// t.Log(mapData)
	// t.Log(kk)
}

// func TestStruct2MapByJson(t *testing.T) {
// 	mapData := Struct2MapByJson(&struct {
// 		IntData int    `structs:"dataInt"`
// 		StrData string `json:"-"`
// 	}{
// 		IntData: 1,
// 		StrData: "2",
// 	})
// 	t.Log(mapData)
// }
