package player

/*
#include <uvdbus/smart_sound.h>
*/
import "C"
import (
	. "netease_control_center/globals"
	"netease_control_center/interfaces"
	"netease_control_center/interfaces/http"
	"netease_control_center/interfaces/log"
	. "netease_control_center/interfaces/schedule"
	"time"
)

const (
	BT_PLAYER_NAME = "bt_player"
)

var (
	btPlayerName = "BtPlayer_"

	btPlayIsOnline bool = false

	// 音乐播放状态变化时，需要更新
	btPlayPlayerSt = PlayerStatus{PlayerSrc: BT_SRC, MusicId: nil, MusicListId: nil, ContinuousTaskSt: MUSIC_ST_IDLE, CurPlayingPos: 0,
		TaskField:    FIELD_NULL,
		SingleTaskSt: AUDIO_ST_IDLE}
)

func BtPauseForIgnoreStartCmd() {
	//SendCmdToBt(uint32(C.CMD_BT_AVK_SOUND_CLOSE), nil)
	SendCmdToBt(uint32(C.CMD_BT_AVK_PAUSE), nil)
}

func BtPlayPause() {
	isChange := PlayerHander[PlayerType_Bt].IsPlayerChange(true)
	log.Println(log.DebugLog, "isChange:", isChange)
	if isChange {
		//SendCmdToBt(uint32(C.CMD_BT_AVK_SOUND_CLOSE), nil)
		SendCmdToBt(uint32(C.CMD_BT_AVK_PAUSE), nil)
	} else {
		//SendCmdToBt(uint32(C.CMD_BT_AVK_SOUND_CLOSE), nil)
		SendCmdToBt(uint32(C.CMD_BT_AVK_PAUSE), nil)
	}
	//MonitorBtPlayerBroadcast(MUSIC_ST_PAUSED)
}

func BtPlayPlay(start int, arg SCmd_Args) bool {
	SendCmdToBt(uint32(C.CMD_BT_AVK_SOUND_OPEN), nil)
	SendCmdToBt(uint32(C.CMD_BT_AVK_PLAY), nil)

	MonitorBtPlayerBroadcast(MUSIC_ST_PLAYING)
	return true
}

func BtPlayStop() {
	SendCmdToBt(uint32(C.CMD_BT_AVK_STOP), nil)
	//MonitorBtPlayerBroadcast(MUSIC_ST_IDLE)
}

func BtDiscon() {
	SendCmdToBt(uint32(C.CMD_BT_AVK_DISC_DEV), nil)
	//MonitorBtPlayerBroadcast(MUSIC_ST_IDLE)
}

func BtPlaySeek(pos uint32) {
	// nil
}

func BtPlayIsOnline() bool {

	return btPlayIsOnline
}

func BtPlayIfForcePause() bool {

	return true
}

func BtPlayPlayNext(arg interface{}) {
	SendCmdToBt(uint32(C.CMD_BT_AVK_NEXT), nil)
	MonitorBtPlayerBroadcast(MUSIC_ST_PLAYING)

	if tmp, ok := arg.(PlayerArg); ok && len(tmp.RspId) > 0 {
		if tmp.RspId == CONST_AUDIO_ID {
			http.InformLocalMsgToServer(http.Type_Inform_TtsPush, int(MSC_CMD_NEXT), TTS_MUSIC_NEXT, TTS_MUSIC_NEXT) //快捷指令上传信息流
		} else {
			http.PushInfoToServerForApp(http.InfoForApp{ResponseId: tmp.RspId, Cmd: int(MSC_CMD_NEXT), Data: TTS_MUSIC_NEXT, Tts: TTS_MUSIC_NEXT,
				PlayMode: CurPlayingMode, DataType: http.DATA_TYPE_STRING})
		}
	}
}

func BtPlayPlayPre(arg interface{}) {
	SendCmdToBt(uint32(C.CMD_BT_AVK_PRE), nil)
	MonitorBtPlayerBroadcast(MUSIC_ST_PLAYING)

	if tmp, ok := arg.(PlayerArg); ok && len(tmp.RspId) > 0 {
		http.PushInfoToServerForApp(http.InfoForApp{ResponseId: tmp.RspId, Cmd: int(MSC_CMD_PRE), Data: TTS_MUSIC_PRE, Tts: TTS_MUSIC_PRE,
			PlayMode: CurPlayingMode, DataType: http.DATA_TYPE_STRING})
	}
}

func BtPlayPrepareNext() {
	// nil
}

func BtPlayGetSupportedSCmd() []SCMD {

	return nil
}

func BtPlaySetArg(arg ...interface{}) bool {
	return true
}

func BtGetPlayerName() string {
	return BT_PLAYER_NAME
}

func BtPlayerStatusChange(status PlayerDetail) {

}

func BtPlayerChange(in bool) {
	if !in {
		//BtDiscon()
		//log.Println(log.WarningLog, "Disconnect bt by vbox")
	}
}

func GetBtOnLineSt() bool {
	return btPlayIsOnline
}

func ChangeBtOnLineSt(isOnline bool) {
	btPlayIsOnline = isOnline

	if !isOnline {
		ClearProjectorVolProcess()

		//MonitorBtPlayerBroadcast(MUSIC_ST_IDLE)
		PlayerHander[PlayerType_Bt].ReportOffline()

	} else {
		log.Println(log.DebugLog, "bt trig request play")

		if PlayerHander[PlayerType_Bt].RequestPlay_Sync(nil) {
			MonitorBtPlayerBroadcast(MUSIC_ST_PLAYING)
		} else {
			SendCmdToBt(uint32(C.CMD_BT_AVK_PAUSE), nil)
			log.Println(log.WarningLog, "bt request play err")
		}
	}

}

func SendCmdToBt(cmd uint32, data *[]byte) {
	interfaces.SendCommand(MODULE_CONTROLLER, MODULE_BT, cmd, data)
}

func ResetBt() {
	SendCmdToBt(uint32(C.CMD_BT_RESET), nil)
}

func OpenBtOfBt() {
	CurMusicSrcOnOffSt.BtOn = true
	SendCmdToBt(uint32(C.CMD_BT_BREDR_ENABLE), nil)
}

func CloseBtOfBt() {
	CurMusicSrcOnOffSt.BtOn = false
	SendCmdToBt(uint32(C.CMD_BT_BREDR_DISABLE), nil)
}

func CloseBtRebootCb(e interface{}, cbType CallCbType) {
	if eval, ok := e.(int); ok {
		if 0 != eval {
			// close bt
			log.Println(log.DebugLog, "close bt when reboot")
			//CloseBtOfBt()
			SendCmdToBt(uint32(C.CMD_BT_BREDR_REBOOT_CLOSE), nil)
		} else {
			log.Println(log.DebugLog, "not need to close bt")
		}
	} else {
		log.Println(log.ErrorLog, "err cfglist type")
	}
}

func MonitorBtPlayerBroadcast(st PlayerStatuses) {
	var tmp PlayerDetail = PlayerDetail{Status: st, PlayerId: uint32(PlayerType_Bt), Uuid: BtUuid}
	PlayerHander[PlayerType_Bt].SetPlayerStatus(tmp)

	log.Println(log.DebugLog, "---bt status:", st)

	CurPlayerSt.ContinuousTaskSt = st
	CurPlayerSt.Duration = 0
	CurPlayerSt.CurPlayingPos = 0
	CurPlayerSt.PlayerId = PlayerType_Bt
	CurPlayerSt.TaskField = FIELD_MUSIC
	CurPlayerSt.PageSize = PAGE_SIZE
	CurPlayerSt.PlayMode = CurPlayingMode

	CurPlayerSt.Page = 0
	CurPlayerSt.Type = 0
	CurPlayerSt.MusicId = &BtUuid

	CurCurrentStatus = WIFIMUSIC_FIELD_ST

	vol := getSysVol()
	var boxStTmp VboxBroadcastSt = VboxBroadcastSt{
		PlayerStPtr:   &CurPlayerSt,
		VboxVolStPtr:  &vol,
		ScenesStPtr:   &CurScenesSt,
		CurrentStatus: CurCurrentStatus,
	}
	boxStTmp.VboxVolStPtr.Action = VOL_NULL_ACTION

	go func(st VboxBroadcastSt) {
		time.Sleep(50 * time.Millisecond)
		TrigBroadcastStatus(st)
	}(boxStTmp)
}
