/****************************************************************************
 * 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_valuestable.h"
#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 _writeValueListToTable(const char *sDeviceName, const AQH_VALUE_LIST *valueList, uint32_t perms, GWEN_BUFFER *dbuf);
static void _writeValueToTable(const char *sDeviceName, const AQH_VALUE *value, uint32_t perms, GWEN_BUFFER *dbuf);



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

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

  perms=AQH_ModService_GetUserPerms(m);

  dbQuery=AQCGI_Request_GetDbQuery(rq);
  sDeviceName=GWEN_DB_GetCharValue(dbQuery, "device", 0, NULL);
  if (!(sDeviceName && *sDeviceName))
    AQH_ModDevices_RunIndex(m, rq, session, dc, dbuf);
  else {
    AQH_VALUE_LIST *valueList;

    valueList=AQH_DataClient_GetValues(dc, sDeviceName, 0);
    if (valueList && AQH_Value_List_GetCount(valueList)) {

      GBAA(dbuf,"<h1>Values for Device %s</h1>\n", sDeviceName);
      _writeValueListToTable(sDeviceName, valueList, perms, dbuf);
      GBAS(dbuf, "\n");
    }
    else {
      GBAS(dbuf,"<p>No values.</p>\n");
    }
    AQH_Value_List_free(valueList);
  }
}



void _writeValueListToTable(const char *sDeviceName, const AQH_VALUE_LIST *valueList, uint32_t perms, GWEN_BUFFER *dbuf)
{
  const AQH_VALUE *value;

  GBAS(dbuf,
       "<table class=\"datatable\">\n"
       "<thead>"
       "<tr>"
       "<th>Name</th>"
       "<th>Type</th>"
       "<th>Modality</th>"
#if 0
       "<th>Driver</th>"
       "<th>Device</th>"
       "<th>Name for System</th>"
#endif
       "</tr>"
       "</thead>\n"
       "<tbody>\n");

  value=AQH_Value_List_First(valueList);
  while(value) {
    //if (AQH_Value_GetModality(value)!=AQH_ValueModality_Stats)
      _writeValueToTable(sDeviceName, value, perms, dbuf);
    value=AQH_Value_List_Next(value);
  }
  GBAS(dbuf,
       "</tbody>\n"
       "</table>\n");
}



void _writeValueToTable(const char *sDeviceName, const AQH_VALUE *value, uint32_t perms, GWEN_BUFFER *dbuf)
{
  const char *s;
  
  GBAS(dbuf, "<tr>");

  /* name for system */
  s=AQH_Value_GetName(value);
  if (perms & AQH_MODDEVICES_PERMS_VALUEREAD) {
    uint32_t pos;

    pos=GWEN_Buffer_GetPos(dbuf);
    GBAS(dbuf,"<td><a href=\"value.html?device=");
    GWEN_Text_EscapeToBuffer(sDeviceName, dbuf);
    GBAS(dbuf,"&value=");
    GWEN_Text_EscapeToBuffer(s, dbuf);
    GBAA(dbuf,"\">%s</a></td>", s);
  }
  else
    GBAA(dbuf,"<td>%s</td>", s?s:"");

  s=AQH_ValueType_toString(AQH_Value_GetValueType(value));
  GBAA(dbuf, "<td>%s</td>", s?s:"");
  
  s=AQH_ValueModality_toString(AQH_Value_GetModality(value));
  GBAA(dbuf, "<td>%s</td>", s?s:"");
  
  GBAA(dbuf, "</tr>\n");
}



