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"
	"fmt"
	"netease_control_center/interfaces/eventPoint"
	"netease_control_center/interfaces/vui"
)

const (
	modename_wifiParingwifiparing = "wifiParing_service"
)

var (
	wifiParing_hd  TaskHd
)

func wifiParing_Interrupt(ByWho string) {
	log.Println(log.DebugLog, modename_wifiParingwifiparing, " be interrupt by ", ByWho)
	if CurBootState != BootStage_WIFIPARING {
		return
	}
	if IsNewBoard() {
		log.Println(log.DebugLog, "This is a new board, can not interrupt wifi paring!")
	} else {
		log.Println(log.DebugLog, "Current in wifi paring mode, jump check wifi and exit!")
		StartBoard(BootStage_BEFOREDONE, true)
	}
}

func wifiParing_Resume() {
	log.Println(log.DebugLog, "Wifi paring resume!")
}

func wifiParingTask_exec(canExec bool, scmd SCMD, arg ...interface{}) {
	if IsInBackupSystem() {
		if CurOTAStatus == OTAStatus_begin || CurBootState < BootStage_BASEINIT {
			log.Println(log.DebugLog, "Can not paring wifi, because ota or init system!")
			return
		}

		if CurBootState == BootStage_BACKUPSYSTEM_WIFIPARING {
			log.Println(log.DebugLog, "Jump into backup system, curboot: ", CurBootState)
			StartBoard(BootStage_BACKUPSYSTEM, true)
		} else {
			log.Println(log.DebugLog, "Jump into backup system wifiparing, curboot: ", CurBootState)
			StartBoard(BootStage_BACKUPSYSTEM_WIFIPARING, true)
		}
		return
	}

	if canExec == false {
		log.Println(log.DebugLog, modename_wifiParingwifiparing, " Can not exec!")
		return
	}

	switch scmd {
	case SCMD_SuperTask_System_WifiParing:
		if IsInBackupSystem() == false {
			if CurBootState == BootStage_WIFIPARING {
				if IsNewBoard() {
					log.Println(log.DebugLog, "This is a new board, and current in wifi paring, do nothing!")
					PlayNoticeAsync(AdSrcType_FILE, vui.SoundNotice_Alianwang002.GetPath(), CONST_AUDIO_ID, false, nil, nil)
					//StartBoard(BootStage_WIFIPARING, true)
				} else {
					log.Println(log.DebugLog, "Current in wifi paring mode, jump check wifi and exit!")
					PlayNoticeAsync(AdSrcType_FILE, vui.SoundNotice_Alianwang011.GetPath(), CONST_AUDIO_ID, false, nil, nil)
					StartBoard(BootStage_BEFOREDONE, true)
				}
			} else {
				log.Println(log.DebugLog, "Will jump into wifi paring by key triging in normal system!")
				CurWifiParingMode = WifiParingMode_BT
				StartBoard(BootStage_WIFIPARING, false)
				eventPoint.EpRecord("B211", map[string]interface{}{"type": CurWifiParingMode.String(), "net": false})
			}
		}

	case SCMD_SuperTask_System_WifiModeChange_AP_BT:
		switch CurBootState {
		case BootStage_WIFIPARING:
			log.Println(log.DebugLog, "jump into wifi ap paring in normal system!")
			if CurWifiParingMode == WifiParingMode_AP {
				CurWifiParingMode = WifiParingMode_BT
			} else {
				CurWifiParingMode = WifiParingMode_AP
			}
			StartBoard(BootStage_WIFIPARING, true)
			eventPoint.EpRecord("B211", map[string]interface{}{"type": CurWifiParingMode.String(), "net": false})

		case BootStage_BACKUPSYSTEM_WIFIPARING:
			log.Println(log.DebugLog, "jump into wifi ap paring in backup system!")
			if CurWifiParingMode == WifiParingMode_AP {
				CurWifiParingMode = WifiParingMode_BT
			} else {
				CurWifiParingMode = WifiParingMode_AP
			}
			StartBoard(BootStage_BACKUPSYSTEM_WIFIPARING, true)

		default:
			log.Println(log.DebugLog, "not in wifi paring, do nothing!")
		}
	}
}

func WifiParingTaskinit() {
	var tmp RegistedTask
	var err error

	tmp.Interrupt = wifiParing_Interrupt

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

	tmp.IsActive = func() bool {
		switch CurBootState {
		case BootStage_WIFIPARING, BootStage_BACKUPSYSTEM_WIFIPARING:
			return true;
		default:
			return false
		}
	}

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

	tmp.Resume = wifiParing_Resume

	tmp.GetSupportedSCmd = func() []SCMD {
		return []SCMD{SCMD_SuperTask_System_WifiParing,SCMD_SuperTask_System_WifiModeChange_AP_BT,}
	}

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

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

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

		return tmp
	}

	if err, wifiParing_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())
	}
}
