/****************************************************************************
 * 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 <aqhome/api.h>
#include <aqhome/aqhome.h>

#include "./aqhomestorage.h"
#include "./init.h"
#include "./fini.h"
#include "./loop.h"
#include "./loop_mqtt.h"
#include "./cleanup.h"

#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/logger.h>
#include <gwenhywfar/cgui.h>
#include <gwenhywfar/debug.h>

#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif



//#define CLEANUP_INTERVAL_IN_SECS (5*60)
//#define WRITE_INTERVAL_IN_SECS   (5*60)

#define CLEANUP_INTERVAL_IN_SECS (60)
#define WRITE_INTERVAL_IN_SECS   (60)
#define PING_INTERVAL            120



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




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

#ifdef HAVE_SIGNAL_H
static int _setSignalHandlers(void);
static int _setupSigAction(struct sigaction *sa, int sig);
static void _signalHandler(int s);
#endif

static void _runService(AQHOME_STORAGE *aqh);
static void _writeCurrentState(AQHOME_STORAGE *aqh);



/* ------------------------------------------------------------------------------------------------
 * static vars
 * ------------------------------------------------------------------------------------------------
 */

#ifdef HAVE_SIGNAL_H
static struct sigaction saINT,saTERM, saHUP, saTSTP, saCONT;
#endif

static int stopService=0;



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

int main(int argc, char **argv)
{
  int rv;
  AQHOME_STORAGE *aqh;
  GWEN_GUI *gui;

  rv=GWEN_Init();
  if (rv) {
    fprintf(stderr, "ERROR: Unable to init Gwen.\n");
    return 2;
  }

  GWEN_Logger_Open(0, "aqhome-storage", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User);
  //GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning);
  GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Info);

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

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

  gui=GWEN_Gui_CGui_new();
  GWEN_Gui_SetGui(gui);

  aqh=AqHomeStorage_new();
  rv=AqHomeStorage_Init(aqh, argc, argv);
  if (rv<0) {
    DBG_INFO(NULL, "here (%d)", rv);
    return 2;
  }

  _runService(aqh);

  AqHomeStorage_Fini(aqh);
  AqHomeStorage_free(aqh);

  GWEN_Gui_SetGui(NULL);
  GWEN_Gui_free(gui);

  return 0;
}



void _runService(AQHOME_STORAGE *aqh)
{
  time_t timeStart;
  time_t timeLastCleanup;
  time_t timeLastWrite;
  time_t timeLastPing;
  int timeout;

  timeout=AqHomeStorage_GetTimeout(aqh);
  timeStart=time(NULL);
  timeLastCleanup=time(NULL);
  timeLastWrite=time(NULL);
  timeLastPing=time(NULL);

  while(!stopService) {
    time_t now;

    DBG_DEBUG(NULL, "Next loop");
    AqHomeStorage_Loop(aqh, 2000);

    now=time(NULL);

    if (((int)difftime(now, timeLastCleanup))>CLEANUP_INTERVAL_IN_SECS) {
      DBG_INFO(NULL, "Cleanup time");
      AqHomeStorage_Cleanup(aqh);
      timeLastCleanup=now;
    }

    if (((int)difftime(now, timeLastWrite))>WRITE_INTERVAL_IN_SECS) {
      DBG_INFO(NULL, "Write time");
      _writeCurrentState(aqh);
      timeLastWrite=now;
    }

    if (timeout && ((int)difftime(now, timeLastPing))>timeout) {
      DBG_INFO(NULL, "Sending ping");
      AqHomeStorage_MqttPing(aqh);
      timeLastPing=now;
    }


    if (timeout && ((int)difftime(now, timeStart))>timeout) {
      DBG_INFO(NULL, "Timeout");
      _writeCurrentState(aqh);
      break;
    }
  } /* while */
}



void _writeCurrentState(AQHOME_STORAGE *aqh)
{
  int rv;

  rv=AqHomeStorage_WriteStorageIfChanged(aqh);
  if (rv<0) {
    DBG_ERROR(NULL, "ATTENTION: Could not write storage statefile (%d)", rv);
  }
  
  rv=AqHomeStorage_WriteServiceIfChanged(aqh);
  if (rv<0) {
    DBG_ERROR(NULL, "ATTENTION: Could not write current config (%d)", rv);
  }
}




int _setSignalHandlers(void)
{
#ifdef HAVE_SIGNAL_H
  int rv;

  rv=_setupSigAction(&saINT, SIGINT);
  if (rv)
    return rv;

  rv=_setupSigAction(&saTERM, SIGTERM);
  if (rv)
    return rv;

  rv=_setupSigAction(&saHUP, SIGHUP);
  if (rv)
    return rv;

#  ifdef SIGTSTP
  rv=_setupSigAction(&saTSTP, SIGTSTP);
  if (rv)
    return rv;
#  endif

#  ifdef SIGCONT
  rv=_setupSigAction(&saCONT, SIGCONT);
  if (rv)
    return rv;
#  endif
#endif
  return 0;
}



int _setupSigAction(struct sigaction *sa, int sig)
{
  sa->sa_handler=_signalHandler;
  sigemptyset(&sa->sa_mask);
  sa->sa_flags=0;
  if (sigaction(sig, sa, 0)) {
    DBG_ERROR(NULL, "Could not setup signal handler for signal %d", sig);
    return GWEN_ERROR_IO;
  }

  return 0;
}



void _signalHandler(int s)
{
  switch(s) {
  case SIGINT:
  case SIGTERM:
  case SIGHUP:
    DBG_WARN(0, "Received signal %d, stopping service in next loop.",s);
    stopService=1;
    break;
  default:
    DBG_WARN(0, "Unknown signal %d",s);
    break;
  }
}


