/****************************************************************************
 * 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 "./net_read.h"
#include "./server_p.h"
#include "aqhome-react/types/unit.h"
#include "aqhome-react/units/u_module.h"

#include "aqhome/aqhome.h"

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

#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>



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

static time_t _getNewestFiletimeFromFileList(const GWEN_STRINGLIST *sl);
static AQHREACT_UNIT *_readNetworkFromFile(AQH_OBJECT *o, const char *filename);
static GWEN_XMLNODE *_readUnitNetFileToXml(const char *sFilename);
static int _readUnitNetFilesIntoList(AQH_OBJECT *o, const GWEN_STRINGLIST *sl, AQHREACT_UNIT_LIST *unitList);



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


time_t AQH_ReactServer_GetNewestUnitNetFiletime(void)
{
  GWEN_STRINGLIST *sl;
  time_t t1;
  time_t t2;

  sl=AQH_GetListOfMatchingDataFiles("aqhome/react/networks", "*.xml");
  t1=_getNewestFiletimeFromFileList(sl);
  GWEN_StringList_free(sl);

  sl=AQH_GetListOfMatchingSysconfFiles("aqhome/react/networks", "*.xml");
  t2=_getNewestFiletimeFromFileList(sl);
  GWEN_StringList_free(sl);

  return (t1>t2)?t1:t2;
}



int AQH_ReactServer_ReadUnitNetFiles(AQH_OBJECT *aqh)
{
  AQH_REACT_SERVER *xo;

  xo=AQH_ReactServer_GetServerData(aqh);
  if (xo) {
    GWEN_STRINGLIST *sl;

    sl=AQH_GetListOfMatchingSysconfFiles("aqhome/react/networks", "*.xml");
    if (sl) {
      int rv;

      GWEN_StringList_Sort(sl, 1, GWEN_StringList_SortModeNoCase);
      rv=_readUnitNetFilesIntoList(aqh, sl, xo->unitList);
      GWEN_StringList_free(sl);
      if (rv<0) {
        DBG_INFO(NULL, "Error reading unit network files (%d)", rv);
        return rv;
      }
    }
    else {
      DBG_ERROR(NULL, "No unit network files");
    }
    return 0;
  }
  return GWEN_ERROR_INVALID;
}



AQHREACT_UNIT *AQH_ReactServer_FindAndReadDataDirNetwork(AQH_OBJECT *o, const char *networkName)
{
  if (o && networkName) {
    AQH_REACT_SERVER *xo;
  
    xo=AQH_ReactServer_GetServerData(o);
    if (xo) {
      AQHREACT_UNIT *unit;
      GWEN_BUFFER *bufFilename;
      GWEN_BUFFER *bufPath;
      const char *s;

      bufFilename=GWEN_Buffer_new(0, 256, 0, 1);
      GWEN_Buffer_AppendString(bufFilename, "aqhome/react/networks/");
      s=networkName;
      while(*s)
        GWEN_Buffer_AppendByte(bufFilename, tolower((unsigned char) *(s++)));
      GWEN_Buffer_AppendString(bufFilename, ".xml");

      bufPath=AQH_FindPathOfDataFile(GWEN_Buffer_GetStart(bufFilename));
      if (bufPath==NULL) {
        DBG_ERROR(NULL, "Network file \"%s\" not found in data folders", GWEN_Buffer_GetStart(bufFilename));
        GWEN_Buffer_free(bufFilename);
        return NULL;
      }
      GWEN_Buffer_free(bufFilename);

      unit=_readNetworkFromFile(o, GWEN_Buffer_GetStart(bufPath));
      if (unit==NULL) {
        DBG_ERROR(NULL, "Error reading network from file \"%s\"", GWEN_Buffer_GetStart(bufPath));
        GWEN_Buffer_free(bufPath);
        return NULL;
      }

      GWEN_Buffer_free(bufPath);
      return unit;
    }
  }
  return NULL;
}



time_t _getNewestFiletimeFromFileList(const GWEN_STRINGLIST *sl)
{
  time_t resultTime=0;

  if (sl) {
    GWEN_STRINGLISTENTRY *se;
  
    se=GWEN_StringList_FirstEntry(sl);
    while(se) {
      const char *s;

      s=GWEN_StringListEntry_Data(se);
      if (s && *s) {
        struct stat sb;

        if (stat(s, &sb)==0) {
          time_t t;

          t=sb.st_mtim.tv_sec;
          if (t>resultTime)
            resultTime=t;
        }
        else {
          DBG_WARN(NULL, "Error on stat(%s): %s (%d)", s, strerror(errno), errno);
        }
      }
      se=GWEN_StringListEntry_Next(se);
    }
  }
  return resultTime;
}



AQHREACT_UNIT *_readNetworkFromFile(AQH_OBJECT *o, const char *filename)
{
  GWEN_XMLNODE *n;
  AQHREACT_UNIT *unit;

  n=_readUnitNetFileToXml(filename);
  if (n==NULL) {
    DBG_ERROR(NULL, "Error reading network file \"%s\"", filename);
    return NULL;
  }

  unit=AqHomeReact_UnitModule_fromXml(o, n);
  if (unit==NULL) {
    DBG_ERROR(NULL, "Error reading network from file \"%s\"", filename);
    GWEN_XMLNode_free(n);
    return NULL;
  }

  GWEN_XMLNode_free(n);
  return unit;
}



GWEN_XMLNODE *_readUnitNetFileToXml(const char *sFilename)
{
  GWEN_XMLNODE *rootNode;
  GWEN_XMLNODE *netNode;
  int rv;

  rootNode=GWEN_XMLNode_new(GWEN_XMLNodeTypeTag, NULL);
  rv=GWEN_XML_ReadFile(rootNode, sFilename, GWEN_XML_FLAGS_DEFAULT);
  if (rv<0) {
    DBG_ERROR(NULL, "Error reading XML file \"%s\": %d", sFilename, rv);
    GWEN_XMLNode_free(rootNode);
    return NULL;
  }

  netNode=GWEN_XMLNode_FindFirstTag(rootNode, "network", NULL, NULL);
  if (netNode) {
    GWEN_XMLNode_UnlinkChild(rootNode, netNode);
    GWEN_XMLNode_free(rootNode);
    return netNode;
  }
  else {
    DBG_ERROR(NULL, "No \"network\" element in network file \"%s\"", sFilename);
    return NULL;
  }
}



int _readUnitNetFilesIntoList(AQH_OBJECT *o, const GWEN_STRINGLIST *sl, AQHREACT_UNIT_LIST *unitList)
{
  GWEN_STRINGLISTENTRY *se;

  se=GWEN_StringList_FirstEntry(sl);
  while(se) {
    const char *s;

    s=GWEN_StringListEntry_Data(se);
    if (s && *s) {
      AQHREACT_UNIT *unit;

      DBG_INFO(NULL, "Reading unit network file \"%s\"", s);
      unit=_readNetworkFromFile(o, s);
      if (unit==NULL) {
        DBG_WARN(NULL, "No network read from network file \"%s\"", s);
        return GWEN_ERROR_GENERIC;
      }
      AQHREACT_Unit_List_Add(unit, unitList);
    }
    se=GWEN_StringListEntry_Next(se);
  }
  return 0;
}



