package controller

/*
#include <uvdbus/smart_sound.h>
#include <uvdbus/ota.h>
*/
import "C"
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/modules/player"
	"fmt"
	"netease_control_center/interfaces/http"
)

const (
	modename_btOnWifiOffwifiparing = "btOnWifiOff_service"
)

var (
	btOnWifiOff_hd               TaskHd
	backupBtstatus               bool
	btOnWifiOffneedInform2server bool = false
)

func btOnWifiOff_Interrupt(ByWho string) {
	log.Println(log.DebugLog, modename_btOnWifiOffwifiparing, " be interrupt by ", ByWho)
	switch globals.WifiSwitch {
	case globals.WifiSwitch_Oning:
		log.Println(log.DebugLog, "Wifi is oning!")
		globals.WifiSwitch = globals.WifiSwitch_On

	case globals.WifiSwitch_Offing:
		log.Println(log.DebugLog, "Wifi is offing!")
		globals.WifiSwitch = globals.WifiSwitch_Off
	}
}

func btOnWifiOff_Resume() {
	log.Println(log.DebugLog, "Should not come here!")
}

func btOnWifiOffCheckFlag() {
	if btOnWifiOffneedInform2server {
		if err := http.InformMsgToServer(http.Type_Inform_BtOnWifiOff, map[string]interface{}{"switch": 0}); err == nil {
			btOnWifiOffneedInform2server = false
		}
	}
}

func btOnWifiOffTask_exec(canExec bool, scmd SCMD, arg ...interface{}) {
	if canExec {
		switch scmd {
		case SCMD_SuperTask_Enter_btOnWifiOff:
			switch globals.WifiSwitch {
			case globals.WifiSwitch_On:
				log.Println(log.DebugLog, "Turn off wifi!")

				globals.WifiSwitch = globals.WifiSwitch_Offing
				if err := http.InformMsgToServer(http.Type_Inform_BtOnWifiOff, map[string]interface{}{"switch": 1}); err == nil {
					btOnWifiOffneedInform2server = true
				}
				SendWifiCmd(uint32(C.CMD_WIFI_OFF))
				SetInternetConnect(false, "user force")
				led_mcu.DirectLedControl(atservice.GUIDE_EFFECT_ID_NET_MODE, atservice.GUIDE_ACTION_START, 0, []byte{})
				player.SoundEffectPlayCmd(globals.CMD_SE_PLAY, vui.SoundNotice_Exit_BtOnWifiOff.GetPath())
				backupBtstatus = globals.CurMusicSrcOnOffSt.BtOn
				if backupBtstatus == false {
					//open bt
					log.Println(log.DebugLog, "Turn on bt!")
					player.OpenBtOfBt()
				}

			case globals.WifiSwitch_Off:
				log.Println(log.DebugLog, "wifi already off, do nothing!")

			case globals.WifiSwitch_Oning:
				log.Println(log.DebugLog, "Wifi is oning, do nothing!")

			case globals.WifiSwitch_Offing:
				log.Println(log.DebugLog, "Wifi is offing, do nothing!")
			}

		case SCMD_SuperTask_Exit_btOnWifiOff:

			switch globals.WifiSwitch {
			case globals.WifiSwitch_On:
				log.Println(log.DebugLog, "wifi on, do nothing!")

			case globals.WifiSwitch_Off:
				log.Println(log.DebugLog, "Turn on wifi")
				globals.WifiSwitch = globals.WifiSwitch_Oning
				led_mcu.DirectLedControl(atservice.GUIDE_EFFECT_ID_NET_MODE, atservice.GUIDE_ACTION_START, 0, []byte{})
				SendWifiCmd(uint32(C.CMD_WIFI_ON))
				player.SoundEffectPlayCmd(globals.CMD_SE_PLAY, vui.SoundNotice_Enter_BtOnWifiOff.GetPath())
				if backupBtstatus == false {
					log.Println(log.DebugLog, "Turn off bt!")
					player.CloseBtOfBt()
				}

			case globals.WifiSwitch_Oning:
				log.Println(log.DebugLog, "Wifi is oning!")

			case globals.WifiSwitch_Offing:
				log.Println(log.DebugLog, "Wifi is offing!")
			}
		}
	} else {
		log.Println(log.DebugLog, modename_btOnWifiOffwifiparing, "can not run.", canExec, scmd)
	}
}

func BtOnWifiOffTaskinit() {
	var tmp RegistedTask
	var err error

	tmp.Interrupt = btOnWifiOff_Interrupt

	tmp.GetName = func() string {
		return modename_btOnWifiOffwifiparing
	}

	tmp.IsActive = func() bool {
		switch globals.WifiSwitch {
		case globals.WifiSwitch_Oning, globals.WifiSwitch_Offing:
			return true
		default:
			return false
		}
	}

	tmp.GetTaskLevel = func() TaskLevel {
		return TaskLevel_BtOnWifiOff
	}

	tmp.Resume = btOnWifiOff_Resume

	tmp.GetSupportedSCmd = func() []SCMD {
		return []SCMD{SCMD_SuperTask_Enter_btOnWifiOff, SCMD_SuperTask_Exit_btOnWifiOff,}
	}

	tmp.GetScheduleDomain = func() ScheduleDomain {
		return ScheduleDomain_NoneMusic
	}

	tmp.Do = btOnWifiOffTask_exec
	tmp.CanBeInterruptByKey = func() bool {
		return false
	}

	tmp.Debug = func() string {
		var tmp string
		tmp += fmt.Sprintln("Backup bt status:", backupBtstatus)
		tmp += fmt.Sprintln("Wifi switch:", globals.WifiSwitch)

		return tmp
	}

	if err, btOnWifiOff_hd = RegisteSTask(tmp); err == nil {
		log.Printf(log.DebugLog, "Registe %s task successfully!!\n", tmp.GetName())
	} else {
		log.Printf(log.ErrorLog, "Registe %s task fail!!\n", tmp.GetName())
	}
}
