package controller

/*
#include <uvdbus/smart_sound.h>
*/
import "C"
import (
	"encoding/json"
	"netease_control_center/globals"
	"netease_control_center/interfaces"
	"netease_control_center/interfaces/log"
	"netease_control_center/nembd/controller/adau1761"
)

func processManfactureMsg(buscmd uint32, msglen uint32, data []byte) {
	switch buscmd {
	case uint32(C.CMD_TEST_GET_VER_REQ):
		var tmp struct {
			KerVer string `json:"kerVer"`
			AppVer string `json:"appVer"`
			HwVer  string `json:"hwVer"`
		}
		tmp.KerVer = "2.1.2"
		tmp.AppVer = *globals.GitTag
		tmp.HwVer = "1.0.0"
		if data, err := json.Marshal(tmp); nil == err {
			interfaces.SendCommand(globals.MODULE_CONTROLLER, globals.MODULE_MANUFACTURE, uint32(C.CMD_TEST_GET_VER_RESP), &data)
		} else {
			log.Println(log.DebugLog, err.Error())
		}
	case uint32(C.CMD_TEST_KNOB_REQ):
		var tmp struct {
			KnobNo        int    `json:"knobNo"`
			KnobDir       int    `json:"knobDir"`
			KnobAttribute string `json:"knobAttribute"`
		}
		tmp.KnobNo = 1
		tmp.KnobDir = 1
		tmp.KnobAttribute = "Knob test"
		if data, err := json.Marshal(tmp); nil == err {
			interfaces.SendCommand(globals.MODULE_CONTROLLER, globals.MODULE_MANUFACTURE, uint32(C.CMD_TEST_KNOB_RESP), &data)
		} else {
			log.Println(log.DebugLog, err.Error())
		}
	case uint32(C.CMD_TEST_BUTTON_REQ):
		var tmp struct {
			ButtonNo     int `json:"buttonNo"`
			ButtonAction int `json:"buttonAction"`
		}
		tmp.ButtonNo = 2
		tmp.ButtonAction = 2
		if data, err := json.Marshal(tmp); nil == err {
			interfaces.SendCommand(globals.MODULE_CONTROLLER, globals.MODULE_MANUFACTURE, uint32(C.CMD_TEST_BUTTON_RESP), &data)
		} else {
			log.Println(log.DebugLog, err.Error())
		}
	case uint32(C.CMD_TEST_SET_VOLUME_REQ):
		var tmp struct {
			Vol int `json:"vol"`
		}
		if err := json.Unmarshal(data, &tmp); nil == err {
			if vol, err := adau1761.SetVol(tmp.Vol); nil == err {
				tmp.Vol = vol
				if data, err := json.Marshal(tmp); nil == err {
					interfaces.SendCommand(globals.MODULE_CONTROLLER, globals.MODULE_MANUFACTURE, uint32(C.CMD_TEST_SET_VOLUME_RESP), &data)
				} else {
					log.Println(log.DebugLog, err.Error())
				}
			}
		} else {
			log.Println(log.DebugLog, err.Error())
		}
	case uint32(C.CMD_TEST_GET_VOLUME_REQ):
		if vol, err := adau1761.GetVol(); nil == err {
			var tmp struct {
				Vol int `json:"vol"`
			}
			tmp.Vol = vol
			if data, err := json.Marshal(tmp); nil == err {
				interfaces.SendCommand(globals.MODULE_CONTROLLER, globals.MODULE_MANUFACTURE, uint32(C.CMD_TEST_GET_VOLUME_RESP), &data)
			} else {
				log.Println(log.DebugLog, err.Error())
			}
		}
	}
}
