/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2024 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 "aqhome/data/storage_readxml.h"
#include "aqhome/data/storage_p.h"

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



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


static void _readLastIdsFromXml(AQH_STORAGE *sto, GWEN_XMLNODE *rootNode);
static void _readValuesFromXml(AQH_STORAGE *sto, GWEN_XMLNODE *rootNode);
static void _readDevicesFromXml(AQH_STORAGE *sto, GWEN_XMLNODE *rootNode);



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

int AQH_Storage_ReadStateFile(AQH_STORAGE *sto, const char *sFilename)
{
  GWEN_XMLNODE *rootNode;
  int rv;

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

  _readLastIdsFromXml(sto, rootNode);
  _readValuesFromXml(sto, rootNode);
  _readDevicesFromXml(sto, rootNode);

  GWEN_XMLNode_free(rootNode);

  return 0;
}



void _readLastIdsFromXml(AQH_STORAGE *sto, GWEN_XMLNODE *rootNode)
{
  GWEN_XMLNODE *nLastIds;

  nLastIds=GWEN_XMLNode_FindFirstTag(rootNode, AQH_STORAGE_XML_ELEMENTNAME_LASTIDS, NULL, NULL);
  if (nLastIds) {
    sto->lastValueId=GWEN_XMLNode_GetIntValue(nLastIds, "lastValueId", 0);
    sto->lastDeviceId=GWEN_XMLNode_GetIntValue(nLastIds, "lastDeviceId", 0);
  }
  else {
    sto->lastValueId=0;
  }
}



void _readValuesFromXml(AQH_STORAGE *sto, GWEN_XMLNODE *rootNode)
{
  GWEN_XMLNODE *nValues;

  nValues=GWEN_XMLNode_FindFirstTag(rootNode, AQH_STORAGE_XML_ELEMENTNAME_VALUES, NULL, NULL);
  if (nValues) {
    GWEN_XMLNODE *nValue;

    nValue=GWEN_XMLNode_FindFirstTag(nValues, AQH_STORAGE_XML_ELEMENTNAME_VALUE, NULL, NULL);
    while(nValue) {
      AQH_VALUE *value;

      value=AQH_Value_fromXml(nValue);
      if (value) {
        AQH_Value_List_Add(value, sto->valueList);
      }
      nValue=GWEN_XMLNode_FindNextTag(nValue, AQH_STORAGE_XML_ELEMENTNAME_VALUE, NULL, NULL);
    }
  }
}



void _readDevicesFromXml(AQH_STORAGE *sto, GWEN_XMLNODE *rootNode)
{
  GWEN_XMLNODE *nDevices;

  nDevices=GWEN_XMLNode_FindFirstTag(rootNode, AQH_STORAGE_XML_ELEMENTNAME_DEVICES, NULL, NULL);
  if (nDevices) {
    GWEN_XMLNODE *nDevice;

    nDevice=GWEN_XMLNode_FindFirstTag(nDevices, AQH_STORAGE_XML_ELEMENTNAME_DEVICE, NULL, NULL);
    while(nDevice) {
      AQH_DEVICE *device;

      device=AQH_Device_fromXml(nDevice);
      if (device) {
        AQH_Device_List_Add(device, sto->deviceList);
      }
      nDevice=GWEN_XMLNode_FindNextTag(nDevice, AQH_STORAGE_XML_ELEMENTNAME_DEVICE, NULL, NULL);
    }
  }
}










