/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2023 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 "./loop.h"
#include "./loop_http.h"
#include "./loop_mqtt.h"
#include "./aqhomehttp.h"
#include "./aqhomestorage_p.h"
#include "aqhome/http/httpservice_conf.h"

#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint.h>



/* ------------------------------------------------------------------------------------------------
 * defines
 * ------------------------------------------------------------------------------------------------
 */



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

static int _writeModifiedSessions(AQHOME_STORAGE *aqh);



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

void AqHomeStorage_Loop(AQHOME_STORAGE *aqh, int timeoutInMsecs)
{
  if (aqh) {
    GWEN_MsgEndpoint_ChildrenIoLoop(aqh->rootEndpoint, timeoutInMsecs);
    AqHomeStorage_ReadAndHandleHttpMessages(aqh);
    AqHomeStorage_ReadAndHandleMqttMessages(aqh);

//    AqHomeStorage_ReadAndHandleIpcMessages(aqh);
  }
}



int AqHomeStorage_WriteStorageIfChanged(AQHOME_STORAGE *aqh)
{
  if (AQH_Storage_GetRuntimeFlags(aqh->storage) & AQH_STORAGE_RTFLAGS_MODIFIED) {
    int rv;

    DBG_INFO(NULL, "Storage modified, writing statefile");
    rv=AqHomeHttpService_LockStorage(aqh->httpService);
    if (rv<0) {
      DBG_INFO(NULL, "Error locking storage (%d)", rv);
      return rv;
    }
    rv=AQH_Storage_WriteState(aqh->storage);
    if (rv<0) {
      DBG_INFO(NULL, "Error writing state file (%d)", rv);
      AqHomeHttpService_UnlockStorage(aqh->httpService);
      return rv;
    }

    rv=AqHomeHttpService_UnlockStorage(aqh->httpService);
    if (rv<0) {
      DBG_INFO(NULL, "Error unlocking storage (%d)", rv);
      return rv;
    }
  }

  return 0;
}



int AqHomeStorage_WriteServiceIfChanged(AQHOME_STORAGE *aqh)
{
  int rv;

  rv=_writeModifiedSessions(aqh);
  if (rv<0) {
    DBG_INFO(NULL, "here (%d)", rv);
    return rv;
  }

  return 0;
}



int _writeModifiedSessions(AQHOME_STORAGE *aqh)
{
  AQH_SESSION_LIST *sessionList;
  int rv;

  rv=AQH_HttpService_LockSessions(aqh->httpService);
  if (rv<0) {
    DBG_INFO(NULL, "Error locking sessions (%d)", rv);
    return rv;
  }

  sessionList=AQH_Service_GetSessionList(aqh->httpService);
  if (sessionList) {
    AQH_SESSION *session;

    session=AQH_Session_List_First(sessionList);
    while(session) {
      if (AQH_Session_GetRuntimeFlags(session) & AQH_SESSION_RTFLAGS_MODIFIED) {
        DBG_INFO(NULL, "Session \"%s\" modified, writing", AQH_Session_GetUid(session));
        rv=AQH_HttpService_SaveSession(aqh->httpService, session);
        if (rv<0) {
          DBG_INFO(NULL, "Error writing session \"%s\" (%d)", AQH_Session_GetUid(session), rv);
        }
        else {
          DBG_DEBUG(NULL, "Session \"%s\" written", AQH_Session_GetUid(session));
          AQH_Session_SubRuntimeFlags(session, AQH_SESSION_RTFLAGS_MODIFIED);
        }
      }
      session=AQH_Session_List_Next(session);
    }
  }

  rv=AQH_HttpService_UnlockSessions(aqh->httpService);
  if (rv<0) {
    DBG_INFO(NULL, "Error unlocking sessions (%d)", rv);
    return rv;
  }

  return 0;
}




