package player

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

const (
	SPLAYER_ACTION_PAUSE       = "OnPause"
	SPLAYER_ACIION_PLAY        = "OnPlay"
	SPLAYER_ACTION_PLAYPAUSE   = "OnPlaypause"
	SPLAYER_ACTION_PLAYRESU    = "OnPlayResume"
	SPLAYER_ACTION_SHUFFER     = "OnShuffe"
	SPLAYER_ACTION_VOL_DOWN    = "OnVolDown"
	SPLAYER_ACTION_VOL_UP      = "OnVolUp"
	SPLAYER_ACTION_NEXT        = "OnNext"
	SPLAYER_ACTION_PRE         = "OnPre"
	SPLAYER_ACTION_MUTE        = "OnMute"
	SPLAYER_ACTION_BEGINFWD    = "OnBeginFwd"
	SPLAYER_ACTION_BEGINREWIND = "OnBeginRewind"
)

type AirplayCtrl struct {
	Port   int    `json:"port"`
	Mac    string `json:"mac"`
	Device string `json:"device"`
}

type AirPlayFifo struct {
	Fifo string `json:"sfifo"`
}
type AirPlayParams struct {
	Channel    uint8 `json:"channels"`
	Bits       uint8 `json:"bits"`
	SampleRate int   `json:"samplerate"`
}
type AirParams2Play struct {
	AirPlayParams
	AirPlayFifo
}

const (
	AIRPLAY_PLAYER_NAME = "airplay_player"
	AIRPLAY_SET_ARG_LEN = 1

	AIRPLAY_PORT = 5002
	AIRPLAY_MAC  = "112233445577"
)

var (
	//airPlayerName = "APlayer_"

	airPlayIsOnline bool = false
	airPlayHasSetrg bool = false
	airPlayPlayArg  interface{}

	airplayPlayState bool = false //false:pause  true:play

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

func init() {
	AirPlayPrivilege.RegisterCb(func(e interface{}, cbType CallCbType) {
		if val, ok := e.(int); ok && val == 0 {
			CtrlAirplayOnOff(CMD_SPLAYER_STOP)
		}

		if val, ok := e.(int); ok && val == 1 {
			if CurWifiStatus == WifiStatuses_CONNECTED {
				CtrlAirplayOnOff(CMD_SPLAYER_START)
			}
		}
	})
}

func AirplayPlayStateSet(st bool) {
	airplayPlayState = st
}

func AirplayDisCon() {
	CtrlAirplayOnOff(CMD_SPLAYER_STOP)
}

func ctrlAirPlayCmdToPhone(cmd string) {
	var tmp struct {
		Action string `json:"action"`
		Val    int    `json:"val"`
	}
	tmp.Action = cmd

	if data, err := json.Marshal(tmp); nil == err {
		interfaces.SendCommand(MODULE_CONTROLLER, MODULE_AIRPLAY, uint32(C.CMD_SPLAYER_HOST_ACTION), &data)
	} else {
		log.Println(log.ErrorLog, err.Error())
	}
}

func CtrlAirplayOnOff(cmd uint32) {
	if CMD_SPLAYER_START == cmd {
		ariplayPrivilege := privilegeManagement.GetModulePrivilege(privilegeManagement.Privilege_AirPlay)
		if !ariplayPrivilege.HasPrivilege {
			log.Println(log.DebugLog, "airplay is disabled!!!")
			return
		}

		log.Printf(log.DebugLog, "cmd to on airplay\n")
		//var tmp AirplayCtrl = AirplayCtrl{Port: AIRPLAY_PORT, Mac: AIRPLAY_MAC, Device: airPlayerName + BTNAME} //jira pv1-32
		var tmp AirplayCtrl = AirplayCtrl{Port: AIRPLAY_PORT, Mac: AIRPLAY_MAC, Device: BTNAME}
		data, err := json.Marshal(tmp)
		if nil != err {
			log.Println(log.DebugLog, err.Error())
			return
		}
		interfaces.SendCommand(MODULE_CONTROLLER, MODULE_AIRPLAY, cmd, &data)

	} else {
		log.Printf(log.DebugLog, "cmd to off airplay\n")
		interfaces.SendCommand(MODULE_CONTROLLER, MODULE_AIRPLAY, cmd, nil)
	}

}

func MonitorAirplayPlayerBroadcast(st PlayerStatuses) {
	var tmp PlayerDetail = PlayerDetail{Status: st, PlayerId: uint32(PlayerType_Airplay), Uuid: AirPlayUuid}
	PlayerHander[PlayerType_Airplay].SetPlayerStatus(tmp)

	if GetCurPlayerId() != PlayerType_Airplay {
		log.Println(log.DebugLog, "Cur player not airplay, do not broadcast airplay stataus!")
		return
	}
	log.Println(log.DebugLog, "---airplay st:", st)

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

	CurPlayerSt.Page = 0
	CurPlayerSt.Type = 0
	CurPlayerSt.MusicId = &AirPlayUuid
	vol := getSysVol()
	var boxStTmp VboxBroadcastSt = VboxBroadcastSt{
		PlayerStPtr:   &CurPlayerSt,
		VboxVolStPtr:  &vol,
		ScenesStPtr:   &CurScenesSt,
		CurrentStatus: CurCurrentStatus,
	}
	boxStTmp.VboxVolStPtr.Action = VOL_NULL_ACTION
	TrigBroadcastStatus(boxStTmp)
}
