/****************************************************************************
 * 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/service/service_p.h"

#include <gwenhywfar/debug.h>



GWEN_INHERIT_FUNCTIONS(AQH_SERVICE);
GWEN_LIST_FUNCTIONS(AQH_SERVICE, AQH_Service);




AQH_SERVICE *AQH_Service_new(void)
{
  AQH_SERVICE *sv;

  GWEN_NEW_OBJECT(AQH_SERVICE, sv);
  GWEN_INHERIT_INIT(AQH_SERVICE, sv);
  GWEN_LIST_INIT(AQH_SERVICE, sv);

  sv->userList=AQH_User_List_new();
  sv->moduleList=AQH_Module_List_new();
  sv->sessionList=AQH_Session_List_new();

  return sv;
}



void AQH_Service_free(AQH_SERVICE *sv)
{
  if (sv) {
    GWEN_LIST_FINI(AQH_SERVICE, sv);
    GWEN_INHERIT_FINI(AQH_SERVICE, sv);

    AQH_User_List_free(sv->userList);
    AQH_Module_List_free(sv->moduleList);
    AQH_Session_List_free(sv->sessionList);

    GWEN_FREE_OBJECT(sv);
  }
}



AQH_USER_LIST *AQH_Service_GetUserList(const AQH_SERVICE *sv)
{
  return sv?sv->userList:NULL;
}



AQH_USER *AQH_Service_GetUserById(const AQH_SERVICE *sv, uint32_t id)
{
  return sv?AQH_User_List_GetById(sv->userList, id):NULL;
}



AQH_USER *AQH_Service_GetUserByAlias(const AQH_SERVICE *sv, const char *s)
{
  return sv?AQH_User_List_GetByAlias(sv->userList, s):NULL;
}



void AQH_Service_AddUser(AQH_SERVICE *sv, AQH_USER *u)
{
  if (sv && u)
    AQH_User_List_Add(u, sv->userList);
}



void AQH_Service_DelUser(AQH_SERVICE *sv, uint32_t userId)
{
  if (sv && userId) {
    AQH_USER *u;

    u=AQH_User_List_GetById(sv->userList, userId);
    if (u) {
      AQH_User_List_Del(u);
      AQH_User_free(u);
    }
  }
}



AQH_MODULE_LIST *AQH_Service_GetModuleList(const AQH_SERVICE *sv)
{
  return sv?sv->moduleList:NULL;
}



AQH_MODULE *AQH_Service_GetModuleById(const AQH_SERVICE *sv, uint32_t id)
{
  return sv?AQH_Module_List_GetById(sv->moduleList, id):NULL;
}



AQH_MODULE *AQH_Service_GetModuleByName(const AQH_SERVICE *sv, const char *s)
{
  return sv?AQH_Module_List_GetByName(sv->moduleList, s):NULL;
}



void AQH_Service_AddModule(AQH_SERVICE *sv, AQH_MODULE *m)
{
  if (sv && m)
    AQH_Module_List_Add(m, sv->moduleList);
}



void AQH_Service_DelModule(AQH_SERVICE *sv, uint32_t moduleId)
{
  if (sv && moduleId) {
    AQH_MODULE *m;

    m=AQH_Module_List_GetById(sv->moduleList, moduleId);
    if (m) {
      AQH_Module_List_Del(m);
      AQH_Module_free(m);
    }
  }
}



AQH_SESSION_LIST *AQH_Service_GetSessionList(const AQH_SERVICE *sv)
{
  return sv?sv->sessionList:NULL;
}



AQH_SESSION *AQH_Service_GetSessionByUid(const AQH_SERVICE *sv, const char *sessionUid)
{
  return sv?AQH_Session_List_GetByUid(sv->sessionList, sessionUid):NULL;
}



void AQH_Service_AddSession(AQH_SERVICE *sv, AQH_SESSION *session)
{
  if (sv && session)
    AQH_Session_List_Add(session, sv->sessionList);
}



void AQH_Service_DelSession(AQH_SERVICE *sv, const char *sid)
{
  if (sv && sid && *sid) {
    AQH_SESSION *session;

    session=AQH_Session_List_GetByUid(sv->sessionList, sid);
    if (session) {
      AQH_Session_List_Del(session);
      AQH_Session_free(session);
    }
  }
}








