package controller

import (
	. "netease_control_center/globals"
	"netease_control_center/interfaces/log"
	"netease_control_center/interfaces/schedule"
	"netease_control_center/interfaces/vui"
	"netease_control_center/modules/hardware/led_mcu"
	"netease_control_center/modules/hardware/mcu_leds/atservice"
	"netease_control_center/nembd/controller/adau1761"
	"time"
)

var (
	ctrlMusicByVol bool = false
)

func init() {
	adau1761.VolChangeCb = volChangefunc
}

func VolInc(step int) (int, error) {
	IsSettingVolume = true
	v, e := adau1761.IncVol(step)
	ResetRecordVolumeTimer()

	return v, e
}

func VolDec(step int) (int, error) {
	IsSettingVolume = true
	v, e := adau1761.DecVol(step)
	ResetRecordVolumeTimer()

	return v, e
}

func RestoreDefaultVolWhen0() {
	if vol, _ := VolGet(); vol == 0 {
		VolSet(DefaultVol.GetCfgListVui())
	}
}

func VolSet(val int) (int, error) {
	defer func() {
		ResetRecordVolumeTimer()
	}()
	return adau1761.SetVol(val)
}

func VolGet() (int, error) {
	return adau1761.GetVol()
}

func VolGetPercent() (int, error) {
	vol, err := adau1761.GetVol()
	if nil != err {
		return 0, err
	}
	volPer := GetPerByVol(vol)
	return volPer, nil
}

/*func VolMute(isMute bool) error {
	err := adau1761.Mute(isMute)

	log.Println(log.DebugLog, "Mute control:", isMute, err)

	return err
}*/

func volBroadCast(action VolAction) {
	vol := getSysVol()
	var st VboxBroadcastSt = VboxBroadcastSt{
		PlayerStPtr:   &CurPlayerSt,
		VboxVolStPtr:  &vol,
		ScenesStPtr:   &CurScenesSt,
		CurrentStatus: CurCurrentStatus,
	}
	st.VboxVolStPtr.Action = action
	TrigBroadcastStatus(st)
}

func getSysVol() VboxVolSt {
	if vol, err := VolGet(); nil == err {
		CurVboxVolSt.VolumeScale = MaxVolume
		volPer := uint8(GetPerByVol(vol))
		CurVboxVolSt.VolumePer = uint8(volPer)
		CurVboxVolSt.MuteMic = false
		CurVboxVolSt.MuteVol = false
	} else {
		log.Println(log.ErrorLog, err.Error())
	}
	return CurVboxVolSt
}

func ShowVolumeLed() {
	//jumpinTime := time.Now()
	cur, _ := VolGet()
	//volgetTime := time.Now()
	led_mcu.DirectLedControl(atservice.GUIDE_EFFECT_ID_VOLUME_ADJUST, atservice.GUIDE_ACTION_START, 1, []byte{uint8(MaxVolume), uint8(cur)})
	//ledfinishTime := time.Now()
	tmp := vui.IconLed_ShowVolume

	tmp.Arg = cur
	if tmp.Arg == 0 {
		tmp.OnHold = 2 * time.Second
	}
	tmp.Show()
	//allfinishTime := time.Now()

	//log.Println(log.DebugLog, "Vol get time:", volgetTime.Sub(jumpinTime), " direct led time:", ledfinishTime.Sub(volgetTime), " 9*9 led time:", allfinishTime.Sub(ledfinishTime))
}

func AlwaysShowVolumeIcon() {
	cur, _ := VolGet()

	tmp := vui.IconLed_ShowVolume

	tmp.Arg = cur
	tmp.OnHold = 0 * time.Second
	tmp.Show()

}

func volChangefunc(vol int) {
	if MinVolume == vol {
		CurVboxVolSt.MuteVol = true

		if schedule.GetCurPlayer().IsPlaying() || (schedule.GetCurPlayer().InterruptStatus && schedule.GetCurPlayer().PlayAfterResume) {
			ctrlMusicByVol = true
			schedule.SendSCmd(schedule.SCMD_MusicPlayer_Pause, nil)
		}
	} else {
		CurVboxVolSt.MuteVol = false

		if ctrlMusicByVol {
			if !schedule.GetCurPlayer().IsPlaying() {
				ctrlMusicByVol = false
				schedule.SendSCmd(schedule.SCMD_MusicPlayer_Play, nil)
			}
		}
	}
}
