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

#include "aqhome-cgi/modules/mdataclient.h"

#include <aqcgi/service/module.h>

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


/* ------------------------------------------------------------------------------------------------
 * defs and enums
 * ------------------------------------------------------------------------------------------------
 */

#define GBAS GWEN_Buffer_AppendString
#define GBAA GWEN_Buffer_AppendArgs

#define I18N(msg) msg



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

static void _runDeviceWithArgs(AQCGI_MODULE *m,
                               AQCGI_REQUEST *rq,
                               AQCGI_SESSION *session,
                               AQH_DATACLIENT *dc,
                               const char *sDeviceName,
                               GWEN_BUFFER *dbuf);
static void _mkDeviceForm(AQH_DATACLIENT *dc, const char *sDeviceName, const AQH_DEVICE *device, GWEN_BUFFER *dbuf);
static void _addFieldToForm(const char *sFieldTitle, const char *sFieldName, const char *sFieldContent, GWEN_BUFFER *dbuf);



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

void AQH_ModDevices_RunDevice(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf)
{
  GWEN_DB_NODE *dbQuery;
  const char *sDeviceName;

  DBG_ERROR(NULL, "RunValue");
  dbQuery=AQCGI_Request_GetDbQuery(rq);
  sDeviceName=GWEN_DB_GetCharValue(dbQuery, "device", 0, NULL);
  if (sDeviceName && *sDeviceName) {
    GWEN_BUFFER *bufDeviceName;

    bufDeviceName=GWEN_Buffer_new(0, 64, 0, 1);
    GWEN_Text_UnescapeToBufferTolerant(sDeviceName, bufDeviceName);
    _runDeviceWithArgs(m, rq, session, dc, GWEN_Buffer_GetStart(bufDeviceName), dbuf);
    GWEN_Buffer_free(bufDeviceName);
  }
}



void _runDeviceWithArgs(AQCGI_MODULE *m,
                        AQCGI_REQUEST *rq,
                        AQCGI_SESSION *session,
                        AQH_DATACLIENT *dc,
                        const char *sDeviceName,
                        GWEN_BUFFER *dbuf)
{
  GWEN_DB_NODE *dbQuery;
  AQH_DEVICE *device;

  dbQuery=AQCGI_Request_GetDbQuery(rq);
  DBG_ERROR(NULL, "Device=%s", sDeviceName?sDeviceName:"<empty>");

  GBAA(dbuf,"<h1>Device %s</h1>\n", sDeviceName);

  device=AQH_ModDevices_GetDevice(dc, sDeviceName);
  if (device) {
    _mkDeviceForm(dc, sDeviceName, device, dbuf);
    AQH_Device_free(device);
  }
}



void _mkDeviceForm(AQH_DATACLIENT *dc, const char *sDeviceName, const AQH_DEVICE *device, GWEN_BUFFER *dbuf)
{
  const char *s;

  GBAS(dbuf,"<form action=\"device.html\" method=\"post\">\n");
  GBAA(dbuf, "<input type=\"hidden\" name=\"device\" value=\"%s\">\n", sDeviceName);

  GBAS(dbuf,"<table>\n");
  _addFieldToForm(I18N("Room"), "roomName", AQH_Device_GetRoomName(device), dbuf);
  _addFieldToForm(I18N("GUI Name"), "nameForGui", AQH_Device_GetNameForGui(device), dbuf);
  _addFieldToForm(I18N("Location"), "location", AQH_Device_GetLocation(device), dbuf);
  _addFieldToForm(I18N("Description"), "description", AQH_Device_GetDescription(device), dbuf);
  GBAS(dbuf,"</table>\n");
  GBAS(dbuf,"<br>\n");

  GBAS(dbuf,"<input type=\"submit\" name=\"action\" value=\"Send\"/>");
  GBAS(dbuf, "</form>\n\n");
}



void _addFieldToForm(const char *sFieldTitle, const char *sFieldName, const char *sFieldContent, GWEN_BUFFER *dbuf)
{
  GBAS(dbuf, "<tr>");
  GBAA(dbuf, "<td><label for=\"%s\">%s</label></td>", sFieldName, sFieldTitle);
  GBAA(dbuf, "<td><input type=\"text\" name=\"%s\" value=\"%s\"/></td>", sFieldName, sFieldContent?sFieldContent:"");
  GBAS(dbuf, "</tr>");
}



