/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2025 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif


#include "./db.h"
#include "./server_p.h"

#include "aqhome/aqhome.h"
#include "aqhome/msg/node/m_node.h"
#include "aqhome/msg/node/m_sendstats.h"
#include "aqhome/msg/node/m_recvstats.h"
#include "aqhome/msg/node/m_memstats.h"
#include "aqhome/msg/node/m_value.h"
#include "aqhome/msg/node/m_addr.h"
#include "aqhome/msg/node/m_device.h"
#include "aqhome/msg/node/m_flashready.h"

#include "aqhome/data/value.h"
#include "aqhome/msg/ipc/data/m_ipcd.h"
#include "aqhome/msg/ipc/data/m_ipcd_values.h"
#include "aqhome/ipc2/endpoint.h"

#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/timestamp.h>



/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static void _handleMsgValue(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg);
static void _handleAddressMsg(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg);
static void _handleMsgComSendStat(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg);
static void _handleMsgComRecvStat(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg);
static void _handleMsgMemStat(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg);
static void _handleMsgDevice(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg);
static void _handleMsgFlashReady(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg);

static AQH_NODE_INFO *_getOrCreateNodeAndUpdateUidAddr(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg, uint32_t uid);
static void _updateTimestampLastChange(AQH_NODE_INFO *ni);
static void _assignDeviceId(AQH_OBJECT *o, AQH_NODE_INFO *ni, uint32_t uid);

static void _announceNodeValues(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_NODE_INFO *ni);
static void _setDeviceName(AQH_VALUE *value, uint32_t uid);
static void _announceValue(AQH_NODE_SERVER *xo, uint32_t uid, const AQHNODE_VALUE *v);




/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

void AQH_NodeServer_NodeMsgToDb(AQH_OBJECT *o, const AQH_MESSAGE *msg)
{
  AQH_NODE_SERVER *xo;

  xo=AQH_NodeServer_GetServerData(o);
  if (xo) {
    uint8_t msgType;

    msgType=AQH_NodeMessage_GetMsgType(msg);

    switch(msgType) {
    case AQH_MSG_TYPE_COMSENDSTATS:  _handleMsgComSendStat(xo, msg);     break;
    case AQH_MSG_TYPE_COMRECVSTATS:  _handleMsgComRecvStat(xo, msg);     break;
    case AQH_MSG_TYPE_MEMSTATS:      _handleMsgMemStat(xo, msg);         break;
    case AQH_MSG_TYPE_VALUE_REPORT:  _handleMsgValue(xo, msg);           break;
    case AQH_MSG_TYPE_NEED_ADDRESS:  _handleAddressMsg(xo, msg);         break;
    case AQH_MSG_TYPE_CLAIM_ADDRESS: _handleAddressMsg(xo, msg);         break;
    case AQH_MSG_TYPE_HAVE_ADDRESS:  _handleAddressMsg(xo, msg);         break;
    case AQH_MSG_TYPE_DEVICE:        _handleMsgDevice(o, xo, msg);       break;
    case AQH_MSG_TYPE_FLASH_READY:   _handleMsgFlashReady(o, xo, msg);   break;
    default:                         break;
    }
  }
}



void AQH_NodeServer_WriteNodeDb(AQH_OBJECT *o)
{
  AQH_NODE_SERVER *xo;

  xo=AQH_NodeServer_GetServerData(o);
  if (xo && xo->dbFile) {
    GWEN_DB_NODE *dbNodeDb;

    AQH_NodeDb_ClearModified(xo->nodeDb);
    dbNodeDb=GWEN_DB_Group_new("nodeDb");
    AQH_NodeDb_toDb(xo->nodeDb, dbNodeDb);
    GWEN_DB_WriteFile(dbNodeDb, xo->dbFile, GWEN_DB_FLAGS_DEFAULT);
    GWEN_DB_Group_free(dbNodeDb);
  }
}



void _handleMsgValue(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_ValueMessage_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(xo, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
  else {
    uint8_t valueId;
    uint16_t val;

    val=AQH_ValueMessage_GetValueNom(msg);
    valueId=AQH_ValueMessage_GetValueId(msg);

    switch(valueId) {
    case AQH_ENDPOINT_VID_STATS_PACKETS_IN:      AQH_NodeInfo_SetStatsPacketsIn(ni, val);  AQH_NodeDb_SetModified(xo->nodeDb); break;
    case AQH_ENDPOINT_VID_STATS_PACKETS_OUT:     AQH_NodeInfo_SetStatsPacketsOut(ni, val); AQH_NodeDb_SetModified(xo->nodeDb); break;
    case AQH_ENDPOINT_VID_STATS_ERRS_CONTENT:    AQH_NodeInfo_SetStatsCrcErrors(ni, val);  AQH_NodeDb_SetModified(xo->nodeDb); break;
    case AQH_ENDPOINT_VID_STATS_ERRS_IO:         AQH_NodeInfo_SetStatsIoErrors(ni, val);   AQH_NodeDb_SetModified(xo->nodeDb); break;
    case AQH_ENDPOINT_VID_STATS_ERRS_NOBUF:      break;
    case AQH_ENDPOINT_VID_STATS_ERRS_COLLISIONS: AQH_NodeInfo_SetStatsCollisions(ni, val); AQH_NodeDb_SetModified(xo->nodeDb); break;
    case AQH_ENDPOINT_VID_STATS_ERRS_BUSY:       AQH_NodeInfo_SetStatsBusy(ni, val);       AQH_NodeDb_SetModified(xo->nodeDb); break;
    case AQH_ENDPOINT_VID_STATS_HEAP_USED:       break;
    case AQH_ENDPOINT_VID_STATS_HEAP_FREE:       break;
    default:
    }
  }
}



void _handleAddressMsg(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_AddrMessage_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(xo, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



void _handleMsgComSendStat(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_SendStatsMessage_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(xo, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
  else {
    AQH_NodeInfo_SetStatsPacketsOut(ni, AQH_SendStatsMessage_GetPacketsOut(msg));
    AQH_NodeInfo_SetStatsCollisions(ni, AQH_SendStatsMessage_GetCollisions(msg));
    AQH_NodeInfo_SetStatsBusy(ni, AQH_SendStatsMessage_GetBusyErrors(msg));
    AQH_NodeDb_SetModified(xo->nodeDb);
    _updateTimestampLastChange(ni);
  }
}



void _handleMsgComRecvStat(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_RecvStatsMessage_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(xo, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
  AQH_NodeInfo_SetStatsPacketsIn(ni, AQH_RecvStatsMessage_GetPacketsIn(msg));
  AQH_NodeInfo_SetStatsCrcErrors(ni, AQH_RecvStatsMessage_GetCrcErrors(msg));
  AQH_NodeInfo_SetStatsIoErrors(ni, AQH_RecvStatsMessage_GetIoErrors(msg));
  AQH_NodeDb_SetModified(xo->nodeDb);
  _updateTimestampLastChange(ni);
}



void _handleMsgMemStat(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_MemStatsMessage_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(xo, msg, uid);
  if (ni==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
  AQH_NodeInfo_SetStatsStackUsed(ni, AQH_MemStatsMessage_GetStackUsage(msg));
  AQH_NodeInfo_SetStatsBuffersUsed(ni, AQH_MemStatsMessage_GetBuffersUsed(msg));
  AQH_NodeInfo_SetStatsMaxBuffersUsed(ni, AQH_MemStatsMessage_GetMaxBuffersUsed(msg));
  AQH_NodeInfo_SetStatsRecvNoBuffer(ni, AQH_MemStatsMessage_GetRecvNoBufferErrors(msg));
  AQH_NodeInfo_SetStatsHeapUsed(ni, AQH_MemStatsMessage_GetHeapUsed(msg));
  AQH_NodeInfo_SetStatsHeapFree(ni, AQH_MemStatsMessage_GetHeapFree(msg));
  AQH_NodeInfo_SetStatsXramSize(ni, AQH_MemStatsMessage_GetXramSize(msg));

  AQH_NodeDb_SetModified(xo->nodeDb);
  _updateTimestampLastChange(ni);
}



void _handleMsgDevice(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_DeviceMessage_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(xo, msg, uid);
  if (ni) {
    const char *s;

    AQH_NodeInfo_SetManufacturer(ni, AQH_DeviceMessage_GetManufacturer(msg));
    AQH_NodeInfo_SetDeviceType(ni, AQH_DeviceMessage_GetDeviceType(msg));
    AQH_NodeInfo_SetDeviceVersion(ni, (AQH_DeviceMessage_GetDeviceVersion(msg)<<8)+AQH_DeviceMessage_GetDeviceRevision(msg));
    AQH_NodeInfo_SetFirmwareVersion(ni,
				    (AQH_DeviceMessage_GetFirmwareVariant(msg)<<24) |
				    (AQH_DeviceMessage_GetFirmwareVersionMajor(msg)<<16) |
                                    (AQH_DeviceMessage_GetFirmwareVersionMinor(msg)<<8) |
                                    AQH_DeviceMessage_GetFirmwareVersionPatchlevel(msg));
    s=AQH_NodeInfo_GetDeviceId(ni);
    if (!(s && *s))
      _assignDeviceId(o, ni, uid);
    _updateTimestampLastChange(ni);
    AQH_NodeDb_SetModified(xo->nodeDb);
    if (uid!=0x00000000L && uid!=0xffffffff)
      _announceNodeValues(o, xo, ni);
  }
  else {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



void _announceNodeValues(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_NODE_INFO *ni)
{
  const char *devName;

  devName=AQH_NodeInfo_GetDeviceId(ni);
  if (devName) {
    const AQHNODE_DEVICE *devInfo;

    devInfo=AQH_NodeServer_GetDeviceDefByName(o, devName);
    if (devInfo) {
      const AQHNODE_VALUE_LIST *valueList;

      valueList=AQHNODE_Device_GetValueList(devInfo);
      if (valueList) {
        const AQHNODE_VALUE *v;

        v=AQHNODE_Value_List_First(valueList);
        while(v) {
          DBG_INFO(NULL, "Announcing value \"%08x/%s\" (%d=%s)",
                   AQH_NodeInfo_GetUid(ni), AQHNODE_Value_GetName(v),
                   AQHNODE_Value_GetModality(v),
                   AQH_ValueModality_toString(AQHNODE_Value_GetModality(v)));
          _announceValue(xo, AQH_NodeInfo_GetUid(ni), v);
          v=AQHNODE_Value_List_Next(v);
        }
      }
    }
    else {
      DBG_INFO(NULL, "Node type \"%s\" not in database", devName);
    }
  }
  else {
    DBG_INFO(NULL, "Node type not in database");
  }
}



void _setDeviceName(AQH_VALUE *value, uint32_t uid)
{
  GWEN_BUFFER *buf;

  buf=GWEN_Buffer_new(0, 64, 0, 1);
  GWEN_Buffer_AppendArgs(buf, "%08x", uid);
  AQH_Value_SetDeviceName(value, GWEN_Buffer_GetStart(buf));
  GWEN_Buffer_free(buf);
}



void _announceValue(AQH_NODE_SERVER *xo, uint32_t uid, const AQHNODE_VALUE *v)
{
  if (xo && xo->brokerEndpoint) {
    AQH_VALUE *value;
    AQH_MESSAGE *msg;

    value=AQH_Value_new();
    _setDeviceName(value, uid);
    AQH_Value_SetDriver(value, "nodes");
    AQH_Value_SetName(value, AQHNODE_Value_GetName(v));
    AQH_Value_SetValueUnits(value, AQHNODE_Value_GetValueUnits(v));
    AQH_Value_SetValueType(value, AQHNODE_Value_GetValueType(v));
    AQH_Value_SetModality(value, AQHNODE_Value_GetModality(v));

    msg=AQH_IpcdMessageValues_newForOne(AQH_MSGTYPE_IPC_DATA_ANNOUNCEVALUE,
                                        AQH_Endpoint_GetNextMessageId(xo->brokerEndpoint), 0,
                                        0, value);
    AQH_Endpoint_AddMsgOut(xo->brokerEndpoint, msg);
    AQH_Value_free(value);
  }
}



void _handleMsgFlashReady(AQH_OBJECT *o, AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg)
{
  AQH_NODE_INFO *ni;
  uint32_t uid;

  uid=AQH_FlashReadyMessage_GetUid(msg);
  ni=_getOrCreateNodeAndUpdateUidAddr(xo, msg, uid);
  if (ni) {
    const char *s;

    AQH_NodeInfo_SetManufacturer(ni, AQH_FlashReadyMessage_GetManufacturer(msg));
    AQH_NodeInfo_SetDeviceType(ni, AQH_FlashReadyMessage_GetDeviceType(msg));
    AQH_NodeInfo_SetDeviceVersion(ni, (AQH_FlashReadyMessage_GetDeviceVersion(msg)<<8)+AQH_FlashReadyMessage_GetDeviceRevision(msg));
    AQH_NodeInfo_SetFirmwareVersion(ni,
				    (AQH_FlashReadyMessage_GetFirmwareVariant(msg)<<24) |
				    (AQH_FlashReadyMessage_GetFirmwareVersionMajor(msg)<<16) |
				    (AQH_FlashReadyMessage_GetFirmwareVersionMinor(msg)<<8) |
				    AQH_FlashReadyMessage_GetFirmwareVersionPatchlevel(msg));
    s=AQH_NodeInfo_GetDeviceId(ni);
    if (!(s && *s))
      _assignDeviceId(o, ni, uid);
    _updateTimestampLastChange(ni);
    AQH_NodeDb_SetModified(xo->nodeDb);
    if (uid!=0x00000000L && uid!=0xffffffff)
      _announceNodeValues(o, xo, ni);
  }
  else {
    DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
  }
}



AQH_NODE_INFO *_getOrCreateNodeAndUpdateUidAddr(AQH_NODE_SERVER *xo, const AQH_MESSAGE *msg, uint32_t uid)
{
  uint8_t busAddr;
  AQH_NODE_INFO *ni;

  busAddr=AQH_NodeMessage_GetSourceAddress(msg);
  ni=AQH_NodeDb_GetNodeInfoByUid(xo->nodeDb, uid);
  if (ni) {
    uint8_t storedBusAddr;

    storedBusAddr=AQH_NodeInfo_GetBusAddress(ni);
    if (busAddr!=0 && storedBusAddr!=busAddr) {
      DBG_INFO(AQH_LOGDOMAIN, "Changed busaddr for %08x from %02x to %02x", uid, storedBusAddr, busAddr);
      AQH_NodeInfo_SetBusAddress(ni, busAddr);
      _updateTimestampLastChange(ni);
      AQH_NodeDb_SetModified(xo->nodeDb);
    }
  }
  else {
    int rv;

    ni=AQH_NodeInfo_new();
    AQH_NodeInfo_SetBusAddress(ni, busAddr);
    AQH_NodeInfo_SetUid(ni, uid);
    _updateTimestampLastChange(ni);
    rv=AQH_NodeDb_AddNodeInfo(xo->nodeDb, ni);
    if (rv<0) {
      DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
      AQH_NodeInfo_free(ni);
      return NULL;
    }
    else {
      DBG_INFO(AQH_LOGDOMAIN, "Added node %08x (%02x)", uid, busAddr);
    }
  }
  return ni;
}



void _assignDeviceId(AQH_OBJECT *o, AQH_NODE_INFO *ni, uint32_t uid)
{
  const AQHNODE_DEVICE *dev;

  dev=AQH_NodeServer_FindDeviceDef(o,
                                   AQH_NodeInfo_GetManufacturer(ni),
                                   AQH_NodeInfo_GetDeviceType(ni),
                                   AQH_NodeInfo_GetDeviceVersion(ni));
  if (dev==NULL) {
    DBG_ERROR(NULL,
              "Unknown NODE device encountered (%08x, %04x, %04x)",
              AQH_NodeInfo_GetManufacturer(ni),
              AQH_NodeInfo_GetDeviceType(ni),
              AQH_NodeInfo_GetDeviceVersion(ni));
  }
  else {
    const char *s;

    s=AQHNODE_Device_GetName(dev);
    DBG_ERROR(NULL, "Found device \"%s\" (%08x)", s?s:"<no name>", uid);
    AQH_NodeInfo_SetDeviceId(ni, s);
  }
}



void _updateTimestampLastChange(AQH_NODE_INFO *ni)
{
  GWEN_TIMESTAMP *t;

  t=GWEN_Timestamp_NowInLocalTime();
  AQH_NodeInfo_SetTimestampLastChange(ni, t);
  GWEN_Timestamp_free(t);
}



