/****************************************************************************
 * 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 "./c_setdata.h"
#include "./aqhome_data_p.h"
#include "./loop.h"
#include "aqhome/ipc/data/ipc_data.h"
#include "aqhome/ipc/data/msg_data_set.h"
#include "aqhome/ipc/endpoint_ipc.h"
#include "aqhome/ipc/msg_ipc_result.h"
#include "aqhome/ipc/msg_ipc_tag16.h"

#include <gwenhywfar/debug.h>



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




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

static int _forwardDataToDriver(AQHOME_DATA *aqh, const AQH_VALUE *v, const char *data);



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

void AqHomeData_HandleSetData(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *epSrc, GWEN_MSG *recvdMsg)
{
  GWEN_MSG *outMsg;
  int resultCode=AQH_MSG_IPC_SUCCESS;
  AQH_VALUE *recvdValue;
  const char *valueName;
  char *valueDataFreeable;
  AQH_VALUE *systemValue;

  AQH_SetDataIpcMsg_Parse(recvdMsg, 0);
  recvdValue=AQH_SetDataIpcMsg_ReadValue(recvdMsg);
  valueName=recvdValue?AQH_Value_GetNameForSystem(recvdValue):NULL;
  valueDataFreeable=AQH_SetDataIpcMsg_ReadData(recvdMsg);

  systemValue=AQH_Storage_GetValueByNameForSystem(aqh->storage, valueName);
  if (systemValue) {
    if (AQH_Value_GetValueType(systemValue)==AQH_ValueType_Actor) {
      resultCode=_forwardDataToDriver(aqh, systemValue, valueDataFreeable);
    }
    else {
      DBG_INFO(NULL, "Value \"%s\" is not an actor", valueName);
      resultCode=AQH_MSG_IPC_ERROR_INVALID;
    }
  }
  else {
    DBG_INFO(NULL, "Unknown value \"%s\"", valueName);
    resultCode=AQH_MSG_IPC_ERROR_INVALID;
  }
  AQH_Value_free(recvdValue);
  free(valueDataFreeable);

  outMsg=AQH_ResultIpcMsg_new(AQH_MSGTYPE_IPC_DATA_RESULT, resultCode);
  GWEN_MsgEndpoint_AddSendMessage(epSrc, outMsg);
}



int _forwardDataToDriver(AQHOME_DATA *aqh, const AQH_VALUE *v, const char *data)
{
  const char *driverName;

  driverName=AQH_Value_GetDriver(v);
  if (driverName && *driverName) {
    GWEN_MSG_ENDPOINT *ep;

    ep=AqHomeData_GetIpcEndpointByServiceName(aqh, driverName);
    if (ep) {
      GWEN_MSG *driverMsg;

      DBG_INFO(AQH_LOGDOMAIN, "Sending SETDATA msg to driver endpoint (%s)", GWEN_MsgEndpoint_GetName(ep));
      driverMsg=AQH_SetDataIpcMsg_new(AQH_MSGTYPE_IPC_DATA_SETDATA, v, data);
      GWEN_MsgEndpoint_AddSendMessage(ep, driverMsg);
      return AQH_MSG_IPC_SUCCESS;
    }
    else {
      DBG_INFO(NULL, "Driver \"%s\" not available", driverName);
      return AQH_MSG_IPC_ERROR_GENERIC;
    }
  }
  else {
    DBG_INFO(NULL, "No driver name");
    return AQH_MSG_IPC_ERROR_GENERIC;
  }
}



