/****************************************************************************
 * 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 "./aqhomehttp_p.h"
#include "aqhome/http/httpservice.h"
#include "aqhome/http/httpservice_http.h"

#include <gwenhywfar/db.h>
#include <gwenhywfar/buffer.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/debug.h>



GWEN_INHERIT(AQH_SERVICE, AQHOME_HTTP)



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

static void GWENHYWFAR_CB _freeData(void *bp, void *p);



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

void AqHomeHttpService_Extend(AQH_SERVICE *sv)
{
  AQHOME_HTTP *xsv;

  GWEN_NEW_OBJECT(AQHOME_HTTP, xsv);
  GWEN_INHERIT_SETDATA(AQH_SERVICE, AQHOME_HTTP, sv, xsv, _freeData);

  xsv->contentTree=AQH_HttpContent_new("root");
  xsv->storageMutex=GWEN_Mutex_new();
}



void _freeData(void *bp, void *p)
{
  AQHOME_HTTP *xsv;

  xsv=(AQHOME_HTTP*) p;

  xsv->storage=NULL;
  AQH_HttpContent_free(xsv->contentTree);
  xsv->contentTree=NULL;
  GWEN_Mutex_free(xsv->storageMutex);

  GWEN_FREE_OBJECT(xsv);
}



AQH_STORAGE *AqHomeHttpService_GetStorage(const AQH_SERVICE *sv)
{
  if (sv) {
    AQHOME_HTTP *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQHOME_HTTP, sv);
    if (xsv)
      return xsv->storage;
  }
  return NULL;
}



void AqHomeHttpService_SetStorage(AQH_SERVICE *sv, AQH_STORAGE *sto)
{
  if (sv) {
    AQHOME_HTTP *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQHOME_HTTP, sv);
    if (xsv)
      xsv->storage=sto;
  }
}



AQH_HTTP_CONTENT *AqHomeHttpService_GetContentTree(const AQH_SERVICE *sv)
{
  if (sv) {
    AQHOME_HTTP *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQHOME_HTTP, sv);
    if (xsv)
      return xsv->contentTree;
  }
  return NULL;
}



void AqHomeHttpService_SetContentTree(AQH_SERVICE *sv, AQH_HTTP_CONTENT *c)
{
  if (sv) {
    AQHOME_HTTP *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQHOME_HTTP, sv);
    if (xsv) {
      AQH_HttpContent_free(xsv->contentTree);
      xsv->contentTree=c;
    }
  }
}



void AqHomeHttpService_MarkStorageChanged(AQH_SERVICE *sv)
{
  if (sv) {
    AQHOME_HTTP *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQHOME_HTTP, sv);
    if (xsv) {
      AQH_Storage_AddRuntimeFlags(xsv->storage, AQH_STORAGE_RTFLAGS_MODIFIED);
    }
  }
}



int AqHomeHttpService_LockStorage(AQH_SERVICE *sv)
{
  if (sv) {
    AQHOME_HTTP *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQHOME_HTTP, sv);
    if (xsv) {
      int rv;

      rv=GWEN_Mutex_Lock(xsv->storageMutex);
      if (rv<0) {
        DBG_ERROR(AQH_LOGDOMAIN, "Error obtaining lock on storage mutex");
        return rv;
      }
      return 0;
    }
  }
  return GWEN_ERROR_GENERIC;
}



int AqHomeHttpService_UnlockStorage(AQH_SERVICE *sv)
{
  if (sv) {
    AQHOME_HTTP *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQHOME_HTTP, sv);
    if (xsv) {
      int rv;

      rv=GWEN_Mutex_Unlock(xsv->storageMutex);
      if (rv<0) {
        DBG_ERROR(AQH_LOGDOMAIN, "Error releasing lock on storage mutex");
        return rv;
      }
      return 0;
    }
  }

  return GWEN_ERROR_GENERIC;
}








