package globals

import (
	"fmt"
	"netease_control_center/interfaces/log"
	"netease_control_center/utils/ping"
	"time"
)

var (
	IsSettingVolume bool = false
	IsVoiceCalling  bool = false

	IsMicClose   bool        = false
	MicCloseTrig WhoCLoseMic = WhoCLoseMic_User
	IsMicLightOn bool        = false

	CurVoiceStatus VoiceStatuses = VoiceStatus_IDLE

	VoiceWakeupAngle     uint16         = 0
	VolumSettingShowTime                = 2 * time.Second
	CurMicLedStatus      MicLedStatuses = MicLedStatuses_ALLCLOSE

	//wifi related
	CurWifiStatus        WifiStatuses = WifiStatuses_DISCONNECT
	IsWifiAutoConnecting bool         = false
	IsWifiChanging       bool         = false
	WifiRssi             int          = -9999
	RouterSsid           string
	RouterMac            string

	//boot stage
	CurBootState BootStage = BootStage_POWERON
	//boot mode
	CurBootMode BootMode = BootMode_UNKNOW

	HasBooted  = false
	NewBoard   = false
	BootDoneCb func()

	HasParedWifi = false

	InternetConnect = false
	WifiIp          string
	WifiMac         string

	DevNetworkStatus NetworkStatus

	CurMcuStatus  McuStatus = McuStatus_Unknow
	CurMcuVersion string

	SilentBoot = false

	WifiSwitch = WifiSwitch_On

	FirstCalTime = time.Now()
)

type WifiSwitch_Status int8
const (
	WifiSwitch_On WifiSwitch_Status = iota
	WifiSwitch_Oning
	WifiSwitch_Off
	WifiSwitch_Offing
)

type WhoCLoseMic uint8

const (
	WhoCLoseMic_User WhoCLoseMic = iota
	WhoCLoseMic_Auto
)

func SetWifiRssi(rssi int) {
	WifiRssi = rssi
}
func GetWifiRssi() int {
	return WifiRssi
}

func ClearWifiStatus() {
	IsWifiChanging = false
	IsWifiAutoConnecting = false
	CurWifiStatus = WifiStatuses_UNKNOW

	log.Println(log.DebugLog, "Clear wifi status:", IsWifiChanging, IsWifiAutoConnecting, CurWifiStatus)
}

type McuStatus uint8

const (
	McuStatus_Unknow McuStatus = iota
	McuStatus_Ready
	McuStatus_UnReady
)

type NetworkStatus struct {
	Ping ping.Statistics
	Time time.Time
}

type BootMode uint8

const (
	BootMode_UNKNOW BootMode = iota
	BootMode_NORMAL
	BootMode_OTA
	BootMode_BACKUPSYSTEM
)

type VoiceStatuses uint8

const (
	VoiceStatus_IDLE VoiceStatuses = iota
	VoiceStatus_WAKEUP
	VoiceStatus_SessionBegin //unused
	VoiceStatus_RECOGING
	VoiceStatus_ERROR
	VoiceStatus_FINISH
)

func (status VoiceStatuses) CanExec() bool {
	switch status {
	case VoiceStatus_WAKEUP:
		if IsMicClose == false {
			return true
		} else {
			return false
		}

	case VoiceStatus_RECOGING:
		if CurVoiceStatus == VoiceStatus_WAKEUP && IsMicClose == false {
			return true
		} else {
			return false
		}

	case VoiceStatus_ERROR:
		if CurVoiceStatus != VoiceStatus_IDLE {
			return true
		} else {
			return false
		}

	case VoiceStatus_FINISH:
		if (CurVoiceStatus == VoiceStatus_RECOGING || CurVoiceStatus == VoiceStatus_WAKEUP) && IsMicClose == false {
			return true
		} else {
			return false
		}

	default:
		return false
	}
}

func (status VoiceStatuses) Debug() string {
	return fmt.Sprintln(status, " can run:", status.CanExec(), " CurVoiceStatus:", CurVoiceStatus, " Mic:", IsMicClose)
}

var (
//VoiceMicDisable = false
)

type MicLedStatuses uint8

const (
	MicLedStatuses_ALLCLOSE MicLedStatuses = iota
	MicLedStatuses_WAKEUP
	MicLedStatuses_RECOGING
	MicLedStatuses_RECOGERROR
	MicLedStatuses_VOLUME
	MicLedStatuses_KEEP_AND_STOP

	MicLedStatuses_WIFIPARING
	MicLedStatuses_TEST
	MicLedStatuses_MAX
)

type WifiStatuses uint8

const (
	WifiStatuses_UNKNOW WifiStatuses = iota
	WifiStatuses_PARING
	WifiStatuses_CONNECTING
	WifiStatuses_CONNECTED
	WifiStatuses_DISCONNECT
)

type BootStage uint8

const (
	BootStage_POWERON BootStage = iota
	BootStage_INIT
	BootStage_WAITEBOOTDONE
	BootStage_CHECKOTA
	BootStage_BTINIT
	BootStage_CHECKUSER
	BootStage_BASEINIT

	BootStage_WIFIPARING
	BootStage_WAITWIFI

	BootStage_BEFOREDONE
	BootStage_FINISH

	BootStage_BACKUPSYSTEM
	BootStage_BACKUPSYSTEM_WIFIPARING

	BootStage_DONOTHING
)

func IsInBackupSystem() bool {
	switch CurBootState {
	case BootStage_BACKUPSYSTEM, BootStage_BACKUPSYSTEM_WIFIPARING:
		return true
	default:
		return false
	}
}
