/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2023 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 "./loop.h"
#include "./c_connect.h"
#include "./c_updatedata.h"
#include "./c_getdatapoints.h"
#include "./c_getlastdatapoint.h"
#include "./c_getvalues.h"
#include "./c_getdevices.h"
#include "./c_setdata.h"
#include "./c_addvalue.h"
#include "./c_annvalue.h"
#include "./c_moddevice.h"
#include "./aqhome_data_p.h"
#include "aqhome/ipc/data/ipc_data.h"
#include "aqhome/ipc/data/msg_data_values.h"
#include "aqhome/ipc/data/msg_data_datapoints.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/msg_ipc_result.h"

#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint_tcpd.h>
#include <gwenhywfar/msg_ipc.h>



/* ------------------------------------------------------------------------------------------------
 * defines
 * ------------------------------------------------------------------------------------------------
 */

#define DISABLE_DEBUGLOG


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

static void _readAndHandleIpcMessages(AQHOME_DATA *aqh);
static void _handleIpcEndpoint(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep);
static void _handleIpcMsg(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg);
static AQH_DEVICE *_getOrCreateDeviceForDriver(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *epDriver, const char *nameForDriver);



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

void AqHomeData_Loop(AQHOME_DATA *aqh, int timeoutInMsecs)
{
  if (aqh) {
    GWEN_MsgEndpoint_IoLoop(aqh->ipcdEndpoint, timeoutInMsecs);
    _readAndHandleIpcMessages(aqh);
  }
}



int AqHomeData_WriteStorageIfChanged(AQHOME_DATA *aqh)
{
  if (AQH_Storage_GetRuntimeFlags(aqh->storage) & AQH_STORAGE_RTFLAGS_MODIFIED) {
    int rv;

    DBG_INFO(NULL, "Storage modified, writing statefile");
    rv=AqHomeData_LockStorage(aqh);
    if (rv<0) {
      DBG_INFO(NULL, "Error locking storage (%d)", rv);
      return rv;
    }
    rv=AQH_Storage_WriteState(aqh->storage);
    if (rv<0) {
      DBG_INFO(NULL, "Error writing state file (%d)", rv);
      AqHomeData_UnlockStorage(aqh);
      return rv;
    }

    rv=AqHomeData_UnlockStorage(aqh);
    if (rv<0) {
      DBG_INFO(NULL, "Error unlocking storage (%d)", rv);
      return rv;
    }
  }

  return 0;
}



AQH_VALUE *AqHomeData_GetOrCreateValueForDriverWithTemplate(AQHOME_DATA *aqh,
                                                            GWEN_MSG_ENDPOINT *epDriver,
                                                            const AQH_VALUE *valueTemplate)
{
  const char *serviceName;
  AQH_VALUE *v;
  GWEN_BUFFER *buf;
  const char *valueName;
  const char *deviceName;

  serviceName=AQH_IpcEndpoint_GetServiceName(epDriver);
  valueName=AQH_Value_GetName(valueTemplate);
  deviceName=AQH_Value_GetDeviceName(valueTemplate);

  buf=GWEN_Buffer_new(0, 256, 0, 1);
  if (deviceName && *deviceName)
    GWEN_Buffer_AppendArgs(buf, "%s/%s/%s", (serviceName && *serviceName)?serviceName:"unknown", deviceName, valueName);
  else
    GWEN_Buffer_AppendArgs(buf, "%s/%s", (serviceName && *serviceName)?serviceName:"unknown", valueName);

  v=AQH_Storage_GetValueByNameForSystem(aqh->storage, GWEN_Buffer_GetStart(buf));
  if (v==NULL) {
    if (AQH_IpcEndpoint_GetPermissions(epDriver) & AQH_IPCENDPOINT_PERMS_ADDVALUE) {
      AQH_DEVICE *device;

      DBG_INFO(AQH_LOGDOMAIN, "Creating value \"%s\"", GWEN_Buffer_GetStart(buf));
      device=(deviceName && *deviceName)?_getOrCreateDeviceForDriver(aqh, epDriver, deviceName):NULL;

      v=AQH_Value_new();
      AQH_Value_SetDriver(v, serviceName);
      AQH_Value_SetName(v, AQH_Value_GetName(valueTemplate));
      AQH_Value_SetNameForSystem(v, GWEN_Buffer_GetStart(buf));
      AQH_Value_SetValueUnits(v, AQH_Value_GetValueUnits(valueTemplate));
      AQH_Value_SetValueType(v, AQH_Value_GetValueType(valueTemplate));
      AQH_Value_SetTimestampCreation(v, (uint64_t) time(NULL));
      if (device) {
        AQH_Value_SetDeviceNameForSystem(v, AQH_Device_GetNameForSystem(device));
        AQH_Value_SetDeviceName(v, AQH_Device_GetName(device));
      }
      AQH_Storage_AddValue(aqh->storage, v);
    }
    else {
      DBG_ERROR(AQH_LOGDOMAIN, "No permissions to create value \"%s\"", GWEN_Buffer_GetStart(buf));
      GWEN_Buffer_free(buf);
      return NULL;
    }
  }
  GWEN_Buffer_free(buf);
  return v;
}



AQH_DEVICE *_getOrCreateDeviceForDriver(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *epDriver, const char *deviceName)
{
  const char *serviceName;
  AQH_DEVICE *device;
  GWEN_BUFFER *buf;

  serviceName=AQH_IpcEndpoint_GetServiceName(epDriver);

  buf=GWEN_Buffer_new(0, 256, 0, 1);
  GWEN_Buffer_AppendArgs(buf, "%s/%s", (serviceName && *serviceName)?serviceName:"unknown", deviceName);

  device=AQH_Storage_GetDeviceByNameForSystem(aqh->storage, GWEN_Buffer_GetStart(buf));
  if (device==NULL) {
    if (AQH_IpcEndpoint_GetPermissions(epDriver) & AQH_IPCENDPOINT_PERMS_ADDDEVICE) {
      DBG_INFO(AQH_LOGDOMAIN, "Creating device \"%s\"", GWEN_Buffer_GetStart(buf));
      device=AQH_Device_new();
      AQH_Device_SetDriver(device, serviceName);
      AQH_Device_SetName(device, deviceName);
      AQH_Device_SetNameForSystem(device, GWEN_Buffer_GetStart(buf));
      AQH_Device_SetTimestampCreation(device, (uint64_t) time(NULL));
      AQH_Storage_AddDevice(aqh->storage, device);
    }
    else {
      DBG_ERROR(AQH_LOGDOMAIN, "No permissions to create device \"%s\"", GWEN_Buffer_GetStart(buf));
      GWEN_Buffer_free(buf);
      return NULL;
    }
  }
  GWEN_Buffer_free(buf);
  return device;
}



void _readAndHandleIpcMessages(AQHOME_DATA *aqh)
{
  if (aqh->ipcdEndpoint) {
    GWEN_MSG_ENDPOINT *ep;

    ep=GWEN_MsgEndpoint_Tree2_GetFirstChild(aqh->ipcdEndpoint);
    while(ep) {
      _handleIpcEndpoint(aqh, ep);
      ep=GWEN_MsgEndpoint_Tree2_GetNext(ep);
    }
  }
}



void _handleIpcEndpoint(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep)
{
  GWEN_MSG *msg;

  while( (msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(ep)) ) {
    _handleIpcMsg(aqh, ep, msg);
    GWEN_Msg_free(msg);
  }
}



void _handleIpcMsg(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg)
{
  uint16_t code;
  uint8_t protoId;

  /* exec IPC message */
  code=GWEN_IpcMsg_GetCode(msg);
  protoId=GWEN_IpcMsg_GetProtoId(msg);
  if (protoId==AQH_IPC_PROTOCOL_DATA_ID) {
    DBG_DEBUG(AQH_LOGDOMAIN, "Received IPC packet %d (%x)", (int) code, code);
    switch(code) {
    case AQH_MSGTYPE_IPC_DATA_CONNECT_REQ:           AqHomeData_HandleConnect(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_UPDATEDATA:            AqHomeData_HandleUpdateData(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_GETVALUES_REQ:         AqHomeData_HandleGetValues(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_GETDATA_REQ:           AqHomeData_HandleGetDataPoints(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_GETLASTDATA_REQ:       AqHomeData_HandleGetLastDataPoint(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_SETDATA:               AqHomeData_HandleSetData(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_ADDVALUE:              AqHomeData_HandleAddValue(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_ANNOUNCEVALUE:         AqHomeData_HandleAnnounceValue(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_GETDEVICES_REQ:        AqHomeData_HandleGetDevices(aqh, ep, msg); break;
    case AQH_MSGTYPE_IPC_DATA_MODDEVICE_REQ:         AqHomeData_HandleModDevice(aqh, ep, msg); break;
    default:                                         break;
    }
  }
  else {
    DBG_ERROR(NULL, "Invalid IPC protocol %d (%02x)", protoId, protoId);
  }
}



