package schedule

import (
	"fmt"
	"netease_control_center/interfaces/log"
	"testing"
	"time"
)

var (
	wifihd    PlayerHd
	wifihderr error

	task1hd    TaskHd
	task1hderr error

	task2hd    TaskHd
	task2hderr error

	task1active = false
	task2active = false
)

func PlayPauseTest() {
	for {
		SendSCmd(SCMD_MusicPlayer_Play, nil)
		time.Sleep(2 * time.Second)
		SendSCmd(SCMD_MusicPlayer_Pause, nil)
		time.Sleep(2 * time.Second)
	}
}

func RegistePlayer1() {
	wifihderr, wifihd = RegistePlayer(RegistedPlayer{
		Pause: func() {
			log.Println(log.DebugLog, "player 1 pause!")
		},
		Play: func(start int) (isSuccess bool) {
			log.Println(log.DebugLog, "Will start play ", start)
			return true
		},
		Stop: func() {
			fmt.Println("will stop")
		},
		Seek: func(pos uint32) {
			fmt.Println("will seek ", pos)
		},
		IsOnline: func() bool {
			return true
		},
		IfForcePause: func() bool {
			return false
		},
		PlayNext: func() {
			fmt.Println("will Play next")
		},
		PlayPre: func() {
			fmt.Println("will play pre")
		},
		PrepareNext: func() {
			fmt.Println("will play prepare next")
		},
		GetSupportedSCmd: func() []SCMD {
			var tmp []SCMD
			return tmp
		},
		SetArg: func(arg ...interface{}) bool {
			fmt.Println("Set arg:", arg)
			return true
		},
		GetPlayerName: func() string {
			return "wifi_player"
		},
	}, PlayerType_Wifi)
}

func RegistePlayer2() {
	wifihderr, wifihd = RegistePlayer(RegistedPlayer{
		Pause: func() {
			log.Println(log.DebugLog, "player 1 pause!")
		},
		Play: func(start int) (isSuccess bool) {
			log.Println(log.DebugLog, "Will start play ", start)
			return true
		},
		Stop: func() {
			fmt.Println("will stop")
		},
		Seek: func(pos uint32) {
			fmt.Println("will seek ", pos)
		},
		IsOnline: func() bool {
			return true
		},
		IfForcePause: func() bool {
			return false
		},
		PlayNext: func() {
			fmt.Println("will Play next")
		},
		PlayPre: func() {
			fmt.Println("will play pre")
		},
		PrepareNext: func() {
			fmt.Println("will play prepare next")
		},
		GetSupportedSCmd: func() []SCMD {
			var tmp []SCMD
			return tmp
		},
		SetArg: func(arg ...interface{}) bool {
			fmt.Println("Set arg:", arg)
			return true
		},
		GetPlayerName: func() string {
			return "wifi_player"
		},
	}, PlayerType_Wifi)
}

func LetTask1Run(delay uint32) {
	for {
		time.Sleep(time.Duration(delay) * time.Second)
		SendSCmd(SCMD_SuperTask_Test0, nil)
	}
}

func RegisteTask1() {
	task1hderr, task1hd = RegisteSTask(RegistedTask{
		Interrupt: func(ByWho string) {
			fmt.Println("Task 1 by interrupted by:", ByWho)
		},
		Resume: func() (isFinish bool) {
			fmt.Println("Task 1 resume!")
			return false
		},
		Do: func(scmd SCMD, arg interface{}) (isFinish bool) {
			fmt.Println("Begin exe cmd:", scmd)
			task1active = true
			go func() {
				time.Sleep(2 * time.Second)
				task1active = false
				task1hd.FinishExec()
				log.Println(log.DebugLog, "Task1 exec finish!")
			}()
			return false
		},
		IsActive: func() bool {
			return task1active
		},
		GetName: func() string {
			return "Task1"
		},
		GetTaskLevel: func() TaskLevel {
			return TaskLevel_0
		},
		GetSupportedSCmd: func() []SCMD {
			return []SCMD{SCMD_SuperTask_Test0}
		},
	})
}

func RegisteTask2() {
	task2hderr, task2hd = RegisteSTask(RegistedTask{
		Interrupt: func(ByWho string) {
			fmt.Println("Task 2 by interrupted by:", ByWho)
		},
		Resume: func() (isFinish bool) {
			fmt.Println("Task 2 resume!")
			go func() {
				time.Sleep(10 * time.Second)
				task2active = false
				task2hd.FinishExec()
			}()
			return false
		},
		Do: func(scmd SCMD, arg interface{}) (isFinish bool) {
			fmt.Println("Begin exe cmd:", scmd)
			task2active = true
			go func() {
				time.Sleep(20 * time.Second)
				log.Println(log.DebugLog, "Task2 exec finish!")
				task2active = false
				task1hd.FinishExec()
			}()
			return false
		},
		IsActive: func() bool {
			return task2active
		},
		GetName: func() string {
			return "Task2"
		},
		GetTaskLevel: func() TaskLevel {
			return TaskLevel_1
		},
		GetSupportedSCmd: func() []SCMD {
			return []SCMD{SCMD_SuperTask_Test1}
		},
	})
}

func TestSPlayer(t *testing.T) {
	RegistePlayer1()
	RegisteTask1()
	RegisteTask2()

	wifihd.RequestPlay_Sync(nil)

	SendSCmd(SCMD_SuperTask_Test0, nil)
	time.Sleep(time.Second)
	task2active = true
	SendSCmd(SCMD_SuperTask_Test1, nil)
	//go PlayPauseTest()
	go LetTask1Run(10)
	select {}
}
