package globals

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

const (
	BRAODCAST_ST_PERIOD = 30 * time.Second
)

type MusicFieldSt uint16

const (
	NULL_FIELD_ST MusicFieldSt = iota + 50000
	WIFIMUSIC_FIELD_ST
	SCENE_FIELD_ST
	DLNA_FIELD_ST
	AIRPLAY_FIELD_ST
	BT_FIELD_ST
)

type VolAction uint8

const (
	VOL_NULL_ACTION     VolAction = iota + 0
	VOL_SRC_CTRL_ACTION           // 投射播放源的音量控制反馈
	VOL_VBOX_ACTION               // 目前音响端调节时候,没有立即广播数据
	VOL_APP_ACTION
	VOL_VOICE_ACTION
)

const (
	MUSIC_ACTION_PREV = "prev"
	MUSIC_ACTION_NEXT = "next"
)

const (
	MUSIC_ACTION_SRC_APP   = "app"
	MUSIC_ACTION_SRC_VOICE = "voice"
	MUSIC_ACTION_SRC_LOCAL = "local"
)

var (
	BroadcastStTimer = time.NewTimer(BRAODCAST_ST_PERIOD)
	BroadcastStChan  = make(chan VboxBroadcastSt, 10)
)

func ResetBroadCastTimer() {
	BroadcastStTimer.Reset(0)
}

func SetBroadCastTimer(afterTime time.Duration) {
	BroadcastStTimer.Reset(afterTime)
}

func TrigBroadcastStatus(st VboxBroadcastSt) {
	st.TimeStamp = time.Now().Unix()
	select {
	case BroadcastStChan <- st:
		//log.Printf(log.DebugLog, "Send success, cmd:%+v\n", st)
	default:
		log.Printf(log.DebugLog, "Send fail, cmd:%+v\n", st)
	}

	//BroadcastStChan <- st
	//BroadcastStTimer.Reset(0)
}

var (
	CurPlayerSt      PlayerSt
	CurVboxVolSt     VboxVolSt // 声音变动，需要同步到这个变量
	CurScenesSt      ScenesSt
	CurCurrentStatus MusicFieldSt = NULL_FIELD_ST

	VboxIp string

	MusicActionNew    *string
	MusicActionSrcNew *string
)

type DeviceMode uint8

const (
	Exec_MusicPlay_mode        DeviceMode = iota + 0
	Exec_Non_Music_Played_mode            // 非音乐执行,且有被动暂停的音乐任务
	Exec_Non_Music_Idle_mode              // 非音乐执行,且没有被动暂停的音乐任务
	Pause_mode
	Idle_mode
)

func (this DeviceMode) IsActive() bool {
	switch this {
	case Exec_MusicPlay_mode, Exec_Non_Music_Played_mode:
		return true
	default:
		return false
	}
}

func ClearCurPlayerSt(playerId PlayerType) {
	//log.Printf(log.DebugLog, "reset playerId to:%+v\n", playerId)
	CurPlayerSt = PlayerSt{PlayerId: playerId}
}

type VboxBroadcastSt struct {
	PlayerStPtr  *PlayerSt  `json:"musicStatus"`
	VboxVolStPtr *VboxVolSt `json:"commonStatus"`
	ScenesStPtr  *ScenesSt  `json:"sceneStatus"`

	CurrentStatus MusicFieldSt `json:"currentStatus"`
	ModeId        int64        `json:"modeId"`

	CurTask uint8 `json:"curTask"`

	DeviceMode DeviceMode `json:"deviceMode"`

	TimeStamp int64 `json:"timeStamp"`
	Type      uint8 `json:"type"` // 1: event trig      0: hear trig
}

/* 播放信息 */
type PlayerSt struct {
	/* 连续播放任务状态 */
	// 播放状态
	MusicId          *string        `json:"songId"`   // 歌曲id
	MusicListId      *string        `json:"listId"`   // 歌单id
	ContinuousTaskSt PlayerStatuses `json:"status"`   // 当前音乐播放状态（不包含提示音和tts的状态）
	CurPlayingPos    uint32         `json:"pos"`      // 当前播放位置，单位ms
	Duration         uint32         `json:"duration"` // 歌曲总时长, 单位ms
	// 播放器以及播放源信息
	PlayerId  PlayerType    `json:"source"` // 播放器的播放源
	PlayMode  MusicPlayMode `json:"playmode"`
	Page      MusicLenType  `json:"page"`
	PageSize  MusicLenType  `json:"pageSize"`
	Type      MusicSrcType  `json:"type"`
	TaskField TaskFieldType `json:"field"`

	ArtistId   *string `json:"artistId"`
	ArtistName *string `json:"artistName"`
	AlbumId    *string `json:"albumId"`
	AlbumName  *string `json:"albumName"`
	OfTag      *string `json:"ofTag"`
	Tag        uint8   `json:"tag"`
	CloudId    *string `json:"cloudId"`

	Action    string `json:"action"`
	ActionSrc string `json:"actionSrc"`
	/* 单次播放任务变量 */
	SingleTaskSt PlayerStatuses `json:"singleTaskSt,omitempty"` // 单次播放任务状态

	BizType BizTypeDef   `json:"bizType"`
	Order   MusicLenType `json:"order"`

	ExtraData string `json:"extraData"`
}

/* 音响音量信息 */
type VboxVolSt struct {
	VolumePer   uint8     `json:"volPer"` // 音量传输百分比
	VolumeScale uint8     `json:"volScale"`
	MuteMic     bool      `json:"muteMic"`
	MuteVol     bool      `json:"muteVolume"`
	Action      VolAction `json:"action"`
	VboxIp      string    `json:"ip"`
}

/* 场景信息 */
type ScenesSt struct {
	SceneId        string `json:"sceneId"`
	TaskId         int    `json:"taskId"`
	Type           int    `json:"type"`
	TaskProgress   int    `json:"sceneTaskProgress"`
	SceneStartTime int64  `json:"sceneStartTime"`
}
