package player

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

func AirPlayStartTrig() {
	airPlayIsOnline = true
}

func AirPlayStopTrig(globalsPause bool) {
	ClearProjectorVolProcess()
	airPlayIsOnline = false

	if globalsPause {
		// has trig pause to phone by AirPlayPause
	} else {
		PlayerHander[PlayerType_Airplay].ReportOffline()
	}
}

func AirPlayPause() {
	sendMusicCmd(PlayerType_Airplay, CMD_AIRPLAYER_PAUSE)
	ctrlAirPlayCmdToPhone(SPLAYER_ACTION_PAUSE)
}

func AirPlayPauseForIgnoreCmd() {
	ctrlAirPlayCmdToPhone(SPLAYER_ACTION_PAUSE)
}

func AirPlayPlay(start int, arg SCmd_Args) bool {
	ctrlAirPlayCmdToPhone(SPLAYER_ACIION_PLAY)

	if REQUEST_PLAY_PARAMS == start {
		if airPlayHasSetrg {
			airPlayHasSetrg = false
			airPlayPlayApi(airPlayPlayArg)
			return true
		} else {
			log.Println(log.ErrorLog, "hasSetAirPlayArg false")
			return false
		}
	} else {
		sendMusicCmd(PlayerType_Airplay, CMD_AIRPLAYER_PLAY)
		return true
	}
}

func AirPlayStop() {
	//sendMusicCmd(PlayerType_Airplay, CMD_PLAYER_STOP)
	// todo : trig phone stop
	ctrlAirPlayCmdToPhone(SPLAYER_ACTION_PAUSE)
}

func AirPlaySeek(pos uint32) {
	// nil
}

func AirPlayIsOnline() bool {
	//
	return airPlayIsOnline
}

func AirPlayIfForcePause() bool {

	return false
}

func AirPlayPlayNext(arg interface{}) {
	// todo : 反向airplay控制
	ctrlAirPlayCmdToPhone(SPLAYER_ACTION_NEXT)
	//MonitorAirplayPlayerBroadcast(MUSIC_ST_PLAYING)
	sendMusicCmd(PlayerType_Airplay, CMD_AIRPLAYER_PLAY)
}

func AirPlayPlayPre(arg interface{}) {
	// todo : 反向airplay控制
	ctrlAirPlayCmdToPhone(SPLAYER_ACTION_PRE)
	//MonitorAirplayPlayerBroadcast(MUSIC_ST_PLAYING)
	sendMusicCmd(PlayerType_Airplay, CMD_AIRPLAYER_PLAY)
}

func AirPlayPrepareNext() {
	// nil
}

func AirPlayGetSUpportedSCmd() []SCMD {

	return nil
}

func AirPlaySetArg(arg ...interface{}) bool {
	if AIRPLAY_SET_ARG_LEN == len(arg) {
		switch arg0 := arg[0].(type) {
		case AirParams2Play:
			airPlayPlayArg = arg0
		default:
			log.Println(log.ErrorLog, "arg0:", arg0)
			goto errOut
		}
		airPlayHasSetrg = true
		return true
	} else {
		log.Println(log.ErrorLog, "len:", len(arg))
	}
errOut:
	log.Printf(log.ErrorLog, "err arg params:%+v\n", arg)
	airPlayHasSetrg = false
	return false
}

func AirPlayGetPlayerName() string {
	return AIRPLAY_PLAYER_NAME
}

func AirPlayChange(in bool) {
	if !in {
		ClearProjectorVolProcess()
		//AirplayDisCon()
		log.Println(log.WarningLog, "Dis Airplay by vbox")
	}
}

func AirPlayPlayerStatusChange(status PlayerDetail) {
	switch status.Status {
	case MUSIC_ST_PLAYING:
		if "" != status.Uuid {
			updateAirPlayPlayerStatus(status)

			if ttsMusicUuid != status.Uuid {
				ttsNoMusic = false
			}
		}
	case MUSIC_ST_IDLE, MUSIC_ST_PAUSED, MUSIC_ST_COMPLETED, MUSIC_ST_PREPARING:
		if "" != status.Uuid {
			updateAirPlayPlayerStatus(status)
		}
	default:
		log.Printf(log.WarningLog, "invalid st:0x%x\n", status.Status)
	}
}

func airPlayPlayApi(arg interface{}) {
	if nil != arg {
		switch argVal := arg.(type) {
		case AirParams2Play:
			log.Printf(log.DebugLog, "airplay arg:%+v\n", argVal)

			sendMusicCmd(PlayerType_Airplay, CMD_AIRPLAYER_PLAY, arg, &AirPlayUuid)
		default:
			log.Println(log.WarningLog, "err params type")
		}
	}

}

func updateAirPlayPlayerStatus(status PlayerDetail) uint8 {
	if status.Status < MUSIC_ST_MAX {
		airPlayPlayerSt.PlayerSrc = CurPlayerSrc
		// 连续任务状态
		airPlayPlayerSt.MusicId = &status.Uuid
		airPlayPlayerSt.ContinuousTaskSt = status.Status
		airPlayPlayerSt.CurPlayingPos = status.CurPos
		airPlayPlayerSt.MusicListId = PlayListSrcRec.MusicListId
		airPlayPlayerSt.Duration = status.Duration
		airPlayPlayerSt.ContinuousStChangeTime = time.Now()
		airPlayPlayerSt.Type = PlayListSrcRec.MusicSrc
		airPlayPlayerSt.TaskField = FIELD_MUSIC
	} else if status.Status < AUDIO_ST_MAX && status.Status >= AUDIO_ST_IDLE {
		airPlayPlayerSt.PlayerSrc = WIFI_SRC
		airPlayPlayerSt.SingleTaskSt = status.Status
		airPlayPlayerSt.SingleStChangeTime = time.Now()
	}

	var precent uint8
	if 0 == airPlayPlayerSt.Duration {
		precent = 0
	} else {
		precent = uint8(airPlayPlayerSt.CurPlayingPos * 100 / airPlayPlayerSt.Duration)
	}

	var printStr string = ""
	if nil != airPlayPlayerSt.MusicId && len(*airPlayPlayerSt.MusicId) > 0 {
		printStr += fmt.Sprintf("musicId:%s ", *airPlayPlayerSt.MusicId)
	}
	if nil != airPlayPlayerSt.MusicListId && len(*airPlayPlayerSt.MusicListId) > 0 {
		printStr += fmt.Sprintf("listId:%s ", *airPlayPlayerSt.MusicListId)
	}
	printStr += fmt.Sprintf("sSt:0x%x field:%d cSt:0x%x cur:%ds dur:%ds src:%d id:%d(%d%%)",
		airPlayPlayerSt.SingleTaskSt,
		airPlayPlayerSt.TaskField,
		airPlayPlayerSt.ContinuousTaskSt,
		status.CurPos/1000,
		status.Duration/1000,
		airPlayPlayerSt.PlayerSrc,
		PlayerType_Airplay,
		precent)
	if MUSIC_ST_PLAYING == status.Status {
		log.Printf(log.DebugLog, "%s\r", printStr)
	} else {
		log.Printf(log.DebugLog, "%s\n", printStr)
	}
	return precent
}
