package led_mcu

import (
	"netease_control_center/globals"
	"netease_control_center/interfaces/log"
	. "netease_control_center/modules/hardware/mcu_leds/atservice"
)

var (
	McuReadyCb func()
)

//export LedInitResult
func LedInitResult(mcuStatus McuStatusT) {
	log.Println(log.DebugLog, "Init mcu result:", mcuStatus)
	switch mcuStatus {
	case MCU_STATUS_AT_NOT_READY:
		globals.CurMcuStatus = globals.McuStatus_UnReady
	case MCU_STATUS_AT_READY:
		globals.CurMcuStatus = globals.McuStatus_Ready
		go func() {
			LampIsShowWhenReset(MCU_LAMP_INIT_SHOW, nil)
			McuGetVersion()
			if McuReadyCb != nil {
				log.Println(log.DebugLog, "Begin Mcu ready cb!")
				McuReadyCb()
			} else {
				log.Println(log.DebugLog, "Mcu ready cb is nil!")
			}
		}()
	case MCU_STATUS_NEED_OTA:
		//do ota
		go McuCheckAndOta("0.0.0.0")
	}
}

//export GAtCmdEndCallback
func GAtCmdEndCallback(cmdId uint32, status AT_CMD_RSP, buf []byte) {
	var bufstring string
	log.Println(log.DebugLog, "GAtCmdEndCallback: type:", cmdId, " buffsize:", len(buf), " rsp_status:", status)
	if len(buf) > 0 {
		bufstring = string(buf[:])
		log.Println(log.DebugLog, "Get C string: ", bufstring)
	}
	switch cmdId {
	case AT_CMD_VERSION:
		if len(buf) > 0 {
			log.Println(log.DebugLog, "Cur Version:", bufstring)
			globals.CurMcuVersion = bufstring
			go McuCheckAndOta(bufstring)
		}
	}
}

//export OtaCallbk
func OtaCallbk(isFinish bool, progress uint8, err error) {
	log.Println(log.DebugLog, "Ota: isFinish:", isFinish, " Progress:", progress)
}
