/****************************************************************************
 * 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

//#define DISABLE_DEBUGLOG


#include "aqhome/http/httpservice_p.h"

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



GWEN_INHERIT(AQH_SERVICE, AQH_HTTP_SERVICE)


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




AQH_SERVICE *AQH_HttpService_new(const char *configFolder, const char *sourceFolder)
{
  AQH_SERVICE *sv;

  sv=AQH_Service_new();
  AQH_HttpService_Extend(sv, configFolder, sourceFolder);

  return sv;
}



void AQH_HttpService_Extend(AQH_SERVICE *sv, const char *configFolder, const char *sourceFolder)
{
  AQH_HTTP_SERVICE *xsv;

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

  AQH_HttpService_SetConfigFolder(sv, configFolder);
  AQH_HttpService_SetSourceFolder(sv, sourceFolder);

  xsv->moduleMutex=GWEN_Mutex_new();
  xsv->userMutex=GWEN_Mutex_new();
  xsv->sessionMutex=GWEN_Mutex_new();

  xsv->urlHandlerList=AQH_HttpUrlHandler_List_new(sv);

}



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

  xsv=(AQH_HTTP_SERVICE*) p;

  AQH_HttpUrlHandler_List_free(xsv->urlHandlerList);

  GWEN_Mutex_free(xsv->moduleMutex);
  GWEN_Mutex_free(xsv->userMutex);
  GWEN_Mutex_free(xsv->sessionMutex);

  free(xsv->configFolder);
  free(xsv->sourceFolder);
  free(xsv->siteHeader);
  free(xsv->siteFooter);
  GWEN_FREE_OBJECT(xsv);
}



const char *AQH_HttpService_GetConfigFolder(const AQH_SERVICE *sv)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv)
      return xsv->configFolder;
  }
  return NULL;
}



void AQH_HttpService_SetConfigFolder(AQH_SERVICE *sv, const char *s)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv) {
      free(xsv->configFolder);
      xsv->configFolder=s?strdup(s):NULL;
    }
  }
}



const char *AQH_HttpService_GetSourceFolder(const AQH_SERVICE *sv)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv)
      return xsv->sourceFolder;
  }
  return NULL;
}



void AQH_HttpService_SetSourceFolder(AQH_SERVICE *sv, const char *s)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv) {
      free(xsv->sourceFolder);
      xsv->sourceFolder=s?strdup(s):NULL;
    }
  }
}



uint32_t AQH_HttpService_GetNextModuleId(AQH_SERVICE *sv)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv) {
      return ++(xsv->lastModuleId);
    }
  }
  return 0;
}



uint32_t AQH_HttpService_GetNextUserId(AQH_SERVICE *sv)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv) {
      return ++(xsv->lastUserId);
    }
  }
  return 0;
}



int AQH_HttpService_GetMaxSessionAgeInSecs(const AQH_SERVICE *sv)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv) {
      return xsv->maxSessionAgeInSecs;
    }
  }
  return 0;
}



void AQH_HttpService_SetMaxSessionAgeInSecs(AQH_SERVICE *sv, int i)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv)
      xsv->maxSessionAgeInSecs=i;
  }
}



const char *AQH_HttpService_GetSiteHeader(const AQH_SERVICE *sv)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv)
      return xsv->siteHeader;
  }
  return NULL;
}



void AQH_HttpService_SetSiteHeader(AQH_SERVICE *sv, const char *s)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv) {
      free(xsv->siteHeader);
      xsv->siteHeader=s?strdup(s):NULL;
    }
  }
}



const char *AQH_HttpService_GetSiteFooter(const AQH_SERVICE *sv)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv)
      return xsv->siteFooter;
  }
  return NULL;
}



void AQH_HttpService_SetSiteFooter(AQH_SERVICE *sv, const char *s)
{
  if (sv) {
    AQH_HTTP_SERVICE *xsv;

    xsv=GWEN_INHERIT_GETDATA(AQH_SERVICE, AQH_HTTP_SERVICE, sv);
    if (xsv) {
      free(xsv->siteFooter);
      xsv->siteFooter=s?strdup(s):NULL;
    }
  }
}



