package controller

/*
#include <uvdbus/smart_sound.h>
*/
import "C"
import (
	"encoding/json"
	"netease_control_center/globals"
	"netease_control_center/interfaces/eventPoint"
	"netease_control_center/interfaces/log"
	"netease_control_center/interfaces/schedule"
	. "netease_control_center/interfaces/vui"
	"netease_control_center/modules/player"
	"sync"
	"time"
)

var (
	timeBtPause             time.Time
    timeBtPlay              time.Time
	enableBtMixProtect      bool = false
	waitLock                sync.Mutex
	waitPlayAfterPauseTimer *time.Timer = nil

	btCon bool = false
	pauseBtNeedResumeWifiPlay bool = false
)

func BtMsgProcess(cmdid uint32, msg string) {
	if !*log.TerminalCmd {
		log.Println(log.DebugLog, "bt cmd: ", cmdid, "  msg:", msg)
	}

	switch cmdid {
	case uint32(C.CMD_BT_AVK_RC_PLAY):
		//case uint32(C.CMD_BT_AVK_RC_PLAY), uint32(C.CMD_BT_AVK_SOUND_START_EVT):
		switch cmdid {
		case uint32(C.CMD_BT_AVK_RC_PLAY):
			log.Println(log.WarningLog, "bt---------------------------- CMD_BT_AVK_RC_PLAY")
		case uint32(C.CMD_BT_AVK_SOUND_START_EVT):
			log.Println(log.WarningLog, "bt---------------------------- CMD_BT_AVK_SOUND_START_EVT")
		}

		if !player.PlayerHander[globals.PlayerType_Bt].IsPlaying() {
			pauseBtNeedResumeWifiPlay = false
			if player.PlayerHander[globals.PlayerType_Wifi].IsPlaying() {
				pauseBtNeedResumeWifiPlay = true
				timeBtPlay = time.Now()
			}
			
			player.ChangeBtOnLineSt(true)
		} else {
			log.Printf(log.DebugLog, "IsPlaying: %v\n", player.PlayerHander[globals.PlayerType_Bt].IsPlaying())
		}
	//case uint32(C.CMD_BT_AVK_RC_PAUSE):
	case uint32(C.CMD_BT_AVK_RC_PAUSE), uint32(C.CMD_BT_AVK_SOUND_STOP_EVT):
		switch cmdid {
		case uint32(C.CMD_BT_AVK_RC_PAUSE):
			log.Println(log.WarningLog, "bt---------------------------- CMD_BT_AVK_RC_PAUSE")
		case uint32(C.CMD_BT_AVK_SOUND_STOP_EVT):
			log.Println(log.WarningLog, "bt---------------------------- CMD_BT_AVK_SOUND_STOP_EVT")
		}

		if player.PlayerHander[globals.PlayerType_Bt].IsPlaying() {
			timeBtPause = time.Now()
			// prd reverse : 20180328
			//IconLed_IconChaozuo001.Show()
			//player.SoundEffectPlayCmd(globals.CMD_SE_PLAY, SoundNotice_Slianjiechenggong001.GetPath())

			if cmdid == C.CMD_BT_AVK_SOUND_STOP_EVT {
				player.MonitorBtPlayerBroadcast(globals.MUSIC_ST_PAUSED)
			}

			//#PV1-4403
			if time.Now().Sub(timeBtPlay) <= globals.BT_WIFI_RESUME_TIME_LIMIT  && pauseBtNeedResumeWifiPlay {
				log.Println(log.WarningLog, "bt offline , resume to wifi.")
				player.ChangeBtOnLineSt(false)
			}
		} else {
			log.Printf(log.DebugLog, "IsPlaying: %v\n", player.PlayerHander[globals.PlayerType_Bt].IsPlaying())
		}
	case uint32(C.CMD_BT_CREATED):
		log.Println(log.WarningLog, "bt---------------------------- CMD_BT_CREATED")
		player.SendCmdToBt(uint32(C.CMD_BT_AVK_OPEN), nil)
	case uint32(C.CMD_BT_AVK_CON_DEV), uint32(C.CMD_BT_AVK_CON_EVT):
		switch cmdid {
		case uint32(C.CMD_BT_AVK_CON_DEV):
			log.Println(log.WarningLog, "bt-------------connect----------- CMD_BT_AVK_CON_DEV")
		case uint32(C.CMD_BT_AVK_CON_EVT):
			log.Println(log.WarningLog, "bt-------------connect----------- CMD_BT_AVK_CON_EVT")
		}
		if btCon {
			log.Println(log.DebugLog, "bt has con, ignore cmd")
			return
		} else {
			btCon = true
		}
		if schedule.GetCurPlayerId() != globals.PlayerType_Bt {
			player.SoundEffectPlayCmd(globals.CMD_SE_PLAY, SoundNotice_Slianjiechenggong001.GetPath())
		}
		IconLed_IconLianjie001.Show()

		eventPoint.DeviceConEvent(eventPoint.ModelValBt)
	case uint32(C.CMD_BT_AVK_DISC_DEV), uint32(C.CMD_BT_AVK_DISC_EVT):
		switch cmdid {
		case uint32(C.CMD_BT_AVK_DISC_DEV):
			log.Println(log.WarningLog, "bt-------------disconnect----------- CMD_BT_AVK_DISC_DEV")
		case uint32(C.CMD_BT_AVK_DISC_EVT):
			log.Println(log.WarningLog, "bt-------------disconnect----------- CMD_BT_AVK_DISC_EVT")
		}
		if !btCon {
			log.Println(log.DebugLog, "bt has discon, ignore cmd")
			return
		} else {
			btCon = false
		}
		if time.Now().Sub(timeBtPause) > player.TIME_PAUSE_STOP {
			player.SoundEffectPlayCmd(globals.CMD_SE_PLAY, SoundNotice_Scaozuofankui001.GetPath())
		} else {
			log.Println(log.WarningLog, "time disconnect is:", time.Now().Sub(timeBtPause), ", ignore se")
		}

		if time.Now().Sub(timeBtPlay) > globals.BT_WIFI_RESUME_TIME_LIMIT  && player.BtPlayIsOnline() && !player.AirPlayIsOnline2() && !player.DlnaIsOnline(){    //20180912 lupeng add #PV1-4403
			log.Println(log.WarningLog, "bt offline , send GloablePause.")
			schedule.SendSCmd(schedule.SCMD_MusicPlayer_Pause, schedule.PlayerArg{GloablePause: true, NotShowIcon: true})
		}
		player.ChangeBtOnLineSt(false)

		eventPoint.DeviceDisconEvent(eventPoint.ModelValBt)
	case uint32(C.CMD_BT_AVK_CRTED):
		log.Println(log.WarningLog, "bt---------------------------- CMD_BT_AVK_CRTED")
		player.SoundEffectPlayCmd(globals.CMD_SE_PLAY, SoundNotice_Scaozuofankui001.GetPath())
	case uint32(C.CMD_BT_AVK_DELETED):
		log.Println(log.WarningLog, "bt---------------------------- CMD_BT_AVK_DELETED")
		player.SoundEffectPlayCmd(globals.CMD_SE_PLAY, SoundNotice_Scaozuofankui001.GetPath())
	case uint32(C.CMD_BT_VOL_CHANGE):
		log.Println(log.WarningLog, "bt---------------------------- CMD_BT_VOL_CHANGE")
		if schedule.GetCurPlayerId() != globals.PlayerType_Bt {
			log.Printf(log.WarningLog, "cur player is not bt(playerId:%d)\n", schedule.GetCurPlayerId())
			return
		}

		if len(msg) < 1 {
			log.Println(log.WarningLog, "param nil")
			return
		}
		var tmp struct {
			VolPer int `json:"volume"`
		}
		if err := json.Unmarshal([]byte(msg), &tmp); nil == err {
			player.ProjectorVolProcess(tmp.VolPer)
		} else {
			log.Println(log.WarningLog, err.Error())
			return
		}
	case uint32(C.CMD_BT_AVK_DEV_CON_CONFLICT):
		log.Println(log.WarningLog, "bt---------------------------- CMD_BT_AVK_DEV_CON_CONFLICT")
		MscToastTts(globals.TTS_TEXT_BT_CONNECT_CONFLICT, globals.CONST_AUDIO_ID, false, false, nil)
	}
}
