/****************************************************************************
 * 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_getlastdatapoint.h"
#include "./aqhome_data_p.h"
#include "aqhome/ipc/data/ipc_data.h"
#include "aqhome/ipc/data/msg_data_devices.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>
#include <gwenhywfar/text.h>



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



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



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


void AqHomeData_HandleModDevice(AQHOME_DATA *aqh, GWEN_MSG_ENDPOINT *ep, GWEN_MSG *recvdMsg)
{
  GWEN_MSG *outMsg;
  int resultCode=AQH_MSG_IPC_SUCCESS;

  if (AQH_IpcEndpoint_GetPermissions(ep) & AQH_IPCENDPOINT_PERMS_MODDEVICE) {
    AQH_DEVICE *device;

    AQH_DevicesDataIpcMsg_Parse(recvdMsg, 0);
    device=AQH_DevicesDataIpcMsg_ReadFirstDevice(recvdMsg);
    if (device) {
      const char *deviceNameForSystem;

      deviceNameForSystem=AQH_Device_GetNameForSystem(device);
      if (deviceNameForSystem && *deviceNameForSystem) {
        AQH_DEVICE *storedDevice;

        storedDevice=AQH_Storage_GetDeviceByNameForSystem(aqh->storage, deviceNameForSystem);
        if (storedDevice) {
          const char *s;

          s=AQH_Device_GetNameForGui(device);
          if (s && *s)
            AQH_Device_SetNameForGui(storedDevice, s);

          s=AQH_Device_GetRoomName(device);
          if (s && *s)
            AQH_Device_SetRoomName(storedDevice, s);

          s=AQH_Device_GetLocation(device);
          if (s && *s)
            AQH_Device_SetLocation(storedDevice, s);

          s=AQH_Device_GetDescription(device);
          if (s && *s)
            AQH_Device_SetDescription(storedDevice, s);

          AQH_Storage_AddRuntimeFlags(aqh->storage, AQH_STORAGE_RTFLAGS_MODIFIED);
          resultCode=AQH_MSG_IPC_SUCCESS;
        }
        else {
          DBG_INFO(NULL, "Device \"%s\" not found", deviceNameForSystem);
          resultCode=AQH_MSG_IPC_ERROR_NOTFOUND;
        }
      }
      else {
        DBG_INFO(NULL, "No name for value");
        resultCode=AQH_MSG_IPC_ERROR_NOTFOUND;
      }
    }
    else {
      DBG_INFO(NULL, "No device info in message");
      resultCode=AQH_MSG_IPC_ERROR_INVALID;
    }
  }
  else {
    DBG_ERROR(AQH_LOGDOMAIN, "No permissions to read data");
    resultCode=AQH_MSG_IPC_ERROR_PERMS;
  }

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



