/****************************************************************************
 * 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 "./s_moddevice.h"

#include "./server_p.h"
#include "aqhome/aqhome.h"
#include "aqhome/ipc2/endpoint.h"
#include "aqhome/msg/ipc/m_ipc.h"
#include "aqhome/msg/ipc/m_ipc_result.h"
#include "aqhome/msg/ipc/data/m_ipcd.h"
#include "aqhome/msg/ipc/data/m_ipcd_devices.h"
#include "aqhome/msg/ipc/m_ipc_result.h"
#include "aqhome/msg/ipc/m_ipc_tag16.h"

#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>



/* ------------------------------------------------------------------------------------------------
 * code
 * ------------------------------------------------------------------------------------------------
 */


void AqHomeDataServer_HandleModDevice(AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *recvdMsg, const GWEN_TAG16_LIST *tagList)
{
  if (tagList) {
    AQHOME_SERVER *xo;
  
    xo=AqHomeDataServer_GetServerData(o);
    if (xo) {
      int resultCode=AQH_MSGDATA_RESULT_SUCCESS;
  
      if (AQH_Endpoint_GetPermissions(ep) & AQH_ENDPOINT_PERMS_MODDEVICE) {
        AQH_DEVICE *device;
  
        device=AQH_IpcdMessageDevices_ReadFirstDevice(tagList);
        if (device) {
          const char *deviceNameForSystem;
  
          deviceNameForSystem=AQH_Device_GetNameForSystem(device);
          if (deviceNameForSystem && *deviceNameForSystem) {
            AQH_DEVICE *storedDevice;
  
            storedDevice=AQH_Storage_GetDeviceByNameForSystem(xo->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(xo->storage, AQH_STORAGE_RTFLAGS_MODIFIED);
              resultCode=AQH_MSGDATA_RESULT_SUCCESS;
            }
            else {
              DBG_INFO(NULL, "Device \"%s\" not found", deviceNameForSystem);
              resultCode=AQH_MSGDATA_RESULT_ERROR_NOTFOUND;
            }
          }
          else {
            DBG_INFO(NULL, "No name for value");
            resultCode=AQH_MSGDATA_RESULT_ERROR_NOTFOUND;
          }
        }
        else {
          DBG_INFO(NULL, "No device info in message");
          resultCode=AQH_MSGDATA_RESULT_ERROR_INVALID;
        }
      }
      else {
        DBG_ERROR(NULL, "No permissions to read data");
        resultCode=AQH_MSGDATA_RESULT_ERROR_PERMS;
      }
      AqHomeDataServer_SendResponseResultToEndpoint(ep, AQH_IpcMessage_GetMsgId(recvdMsg), resultCode);
    }
  }
}



