/****************************************************************************
 * 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_index.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



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

static void _addLinkForDevice(const char *page, const char *sDevice, const char *action, const char *imgName, GWEN_BUFFER *dbuf);



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

void AQH_ModDevices_RunIndex(AQCGI_MODULE *m, AQCGI_REQUEST *rq, AQCGI_SESSION *session, AQH_DATACLIENT *dc, GWEN_BUFFER *dbuf)
{
  AQH_DEVICE_LIST *deviceList;
  AQH_DEVICE *device;
  uint32_t perms;

  perms=AQH_ModService_GetUserPerms(m);

  deviceList=AQH_DataClient_GetDevices(dc, NULL);
  if (deviceList==NULL)  {
    DBG_ERROR(NULL, "No device received");
    GBAS(dbuf, "<p>No devices.</p>");
    return;
  }

  GBAS(dbuf, "<h1>Devices</h1>\n");
  GBAS(dbuf,
       "<table class=\"datatable\">\n"
       "<thead>\n"
       "<tr>"
       "<th>Name For System</th>"
       "<th>Name For GUI</th>"
       "<th>Room</th>"
       "<th>Location</th>"
       "<th>Description</th>"
       "<th>Actions</th>"
       "</tr>\n"
       "</thead>\n"
       "<tbody>\n");

  device=AQH_Device_List_First(deviceList);
  while(device) {
    const char *s;
    const char *sDevice;

    GBAA(dbuf, "<tr>");
    /* name for system */
    sDevice=AQH_Device_GetNameForSystem(device);
    GBAA(dbuf,"<td>%s</td>", sDevice?sDevice:"");
    /* nameForGui */
    s=AQH_Device_GetNameForGui(device);
    GBAA(dbuf, "<td>%s</td>", s?s:"");
    /* room */
    s=AQH_Device_GetRoomName(device);
    GBAA(dbuf, "<td>%s</td>", s?s:"");
    /* location */
    s=AQH_Device_GetLocation(device);
    GBAA(dbuf, "<td>%s</td>", s?s:"");
    /* description */
    s=AQH_Device_GetDescription(device);
    GBAA(dbuf, "<td>%s</td>", s?s:"");

    GBAS(dbuf, "<td>");
    if (perms & AQH_MODDEVICES_PERMS_VALUEREAD) {
      _addLinkForDevice("vtable.html", sDevice, "table view", "/pics/document-table.png", dbuf);
      _addLinkForDevice("vgraph.html", sDevice, "graph view", "/pics/graph.png", dbuf);
    }

    if (perms & AQH_MODDEVICES_PERMS_DEVICEWRITE) {
      _addLinkForDevice("device.html", sDevice, "edit device", "/pics/edit.png", dbuf);
    }

    GBAS(dbuf, "</td>");

    GBAA(dbuf, "</tr>");
    device=AQH_Device_List_Next(device);
  }

  GBAS(dbuf,
       "</tbody>\n"
       "</table>\n");
  AQH_Device_List_free(deviceList);
}



void _addLinkForDevice(const char *page, const char *sDevice, const char *action, const char *imgName, GWEN_BUFFER *dbuf)
{
  GBAA(dbuf,"<a href=\"%s?device=", page);
  GWEN_Text_EscapeToBufferTolerant(sDevice, dbuf);
  GBAS(dbuf,"\">");
  GBAA(dbuf,"<img src=\"%s\" alt=\"%s\" title=\"%s\" />", imgName, action, action);
  GBAS(dbuf,"</a>");
}




