package globals

import (
	"container/list"
	"fmt"
	"netease_control_center/interfaces/log"
	"strconv"
	"testing"
	"time"
)

var (
	ttsListRec *TtsList = &TtsList{List: list.New(), CurPos: nil}
)

func TestTtsList_GetRemoveBack(t *testing.T) {
	var tts1 = "tts1"
	var ttsInfo1 TtsInfo = TtsInfo{Tts: &tts1}
	ttsListRec.Push(&ttsInfo1)

	var tts2 = "tts2"
	var ttsInfo2 TtsInfo = TtsInfo{Tts: &tts2, Level: 1}
	ttsListRec.Push(&ttsInfo2)

	ttsInfoGet, err := ttsListRec.GetRemoveBack()
	if nil == err {
		log.Println(log.DebugLog, "tts:", *(ttsInfoGet.Value.(*TtsInfo).Tts))
	} else {
		log.Println(log.DebugLog, err.Error())
	}

	ttsInfoGet, err = ttsListRec.GetRemoveBack()
	if nil == err {
		log.Println(log.DebugLog, "tts:", *(ttsInfoGet.Value.(*TtsInfo).Tts))
	} else {
		log.Println(log.DebugLog, err.Error())
	}

}

func TestTtsList_Push(t *testing.T) {
	var ttsInfo1 TtsInfo
	tts1 := fmt.Sprintf("tts 测试:%d", 0)
	ttsInfo1.Tts = &tts1
	ttsInfo1.Level = 0
	ttsListRec.Push(&ttsInfo1)

	var ttsInfo2 TtsInfo
	tts2 := fmt.Sprintf("tts 测试:%d", 1)
	ttsInfo2.Tts = &tts2
	ttsInfo2.Level = 1
	ttsListRec.Push(&ttsInfo2)

	var ttsInfo3 TtsInfo
	tts3 := fmt.Sprintf("tts 测试:%d", 2)
	ttsInfo3.Tts = &tts3
	ttsInfo3.Level = 1
	ttsListRec.Push(&ttsInfo3)

}

func TestGetPageIndexFromIndex(t *testing.T) {
	index := []int{0, 99, 100}
	for _, indextmp := range index {
		indexS := GetPageIndexFromIndex(MusicLenType(indextmp),
			PAGE_SIZE)
		t.Log("index:", indextmp, "indexS:", indexS)
	}
}

func TestCommon(t *testing.T) {
	var a int = 100
	t.Log("a str:", strconv.Itoa(a))

	tts := "wk %s hi"
	ttsinfo := fmt.Sprintf(tts, "say")
	t.Log(ttsinfo)

	var out chan bool = make(chan bool, 1)
	timeT := time.NewTimer(2 * time.Second)
	go func() {
		time.Sleep(time.Second * 5)
		timeT.Stop()
		out <- true
	}()

	select {
	case <-timeT.C:
		t.Log("timeout\n")
	case <-out:
		t.Log("out")
	}

	time1 := time.Now()
	time.Sleep(time.Second)
	t.Log("timesub:", time.Now().Sub(time1))
}

func TestGetPageIndex(t *testing.T) {
	var page *PageNode
	page = GetPageFromIndex(0, PAGE_SIZE)
	t.Log("page:", page.PageIndex)

	page = GetPageFromIndex(PAGE_SIZE-1, PAGE_SIZE)
	t.Log("page:", page.PageIndex)

	page = GetPageFromIndex(PAGE_SIZE, PAGE_SIZE)
	t.Log("page:", page.PageIndex)

	time1 := time.Now()
	time.Sleep(time.Second)
	t.Log("time1:", time.Duration(time.Now().Sub(time1)))

	var tmp int8 = 10
	t.Log("time2:", time.Duration(tmp)*time.Second)

	timed1 := 1 * time.Millisecond

	timed2 := timed1.Nanoseconds()

	t.Log("timed2:", timed2)
}

func TestGetCfgList(t *testing.T) {
	str := "[{\"id\":27,\"key\":\"cfgListVui\",\"subNodes\":[{\"id\":43,\"key\":\"defaultVol\",\"pid\":27,\"subNodes\":[],\"value\":\"\"},{\"id\":44,\"key\":\"incVolLevel\",\"pid\":27,\"subNodes\":[],\"value\":\"\"},{\"id\":45,\"key\":\"directLedBrightness\",\"pid\":27,\"subNodes\":[],\"value\":\"\"}]}]"

	GetCfgList(str)
}

func TestExecCb(t *testing.T) {
	ForceExecCfgListCb()
}
