/****************************************************************************
 * This file is part of the project Gwenhywfar.
 * Gwenhywfar (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 "./eventtimer_p.h"

#include <gwenhywfar/debug.h>



GWEN_INHERIT_FUNCTIONS(AQH_EVENT_TIMER)
GWEN_LIST_FUNCTIONS(AQH_EVENT_TIMER, AQH_EventTimer)




AQH_EVENT_TIMER *AQH_EventTimer_new()
{
  AQH_EVENT_TIMER *evt;

  GWEN_NEW_OBJECT(AQH_EVENT_TIMER, evt);
  GWEN_INHERIT_INIT(AQH_EVENT_TIMER, evt);
  GWEN_LIST_INIT(AQH_EVENT_TIMER, evt);

  return evt;
}



void AQH_EventTimer_free(AQH_EVENT_TIMER *evt)
{
  if (evt) {
    GWEN_LIST_FINI(AQH_EVENT_TIMER, evt);
    GWEN_INHERIT_FINI(AQH_EVENT_TIMER, evt);

    GWEN_FREE_OBJECT(evt);
  }
}



uint32_t AQH_EventTimer_GetFlags(const AQH_EVENT_TIMER *evt)
{
  return evt?evt->flags:0;
}



void AQH_EventTimer_SetFlags(AQH_EVENT_TIMER *evt, uint32_t f)
{
  if (evt)
    evt->flags=f;
}



void AQH_EventTimer_AddFlags(AQH_EVENT_TIMER *evt, uint32_t f)
{
  if (evt)
    evt->flags|=f;
}



void AQH_EventTimer_SubFlags(AQH_EVENT_TIMER *evt, uint32_t f)
{
  if (evt)
    evt->flags&=~f;
}



int AQH_EventTimer_GetTimerType(const AQH_EVENT_TIMER *evt)
{
  return evt?evt->timerType:0;
}



void AQH_EventTimer_SetTimerType(AQH_EVENT_TIMER *evt, int i)
{
  if (evt)
    evt->timerType=i;
}



uint64_t AQH_EventTimer_GetId(const AQH_EVENT_TIMER *evt)
{
  return evt?evt->id:0;
}



void AQH_EventTimer_SetId(AQH_EVENT_TIMER *evt, uint64_t i)
{
  if (evt)
    evt->id=i;
}



uint64_t AQH_EventTimer_GetCurrentValue(const AQH_EVENT_TIMER *evt)
{
  return evt?evt->currentValue:0;
}



void AQH_EventTimer_SetCurrentValue(AQH_EVENT_TIMER *evt, uint64_t i)
{
  if (evt)
    evt->currentValue=i;
}



uint64_t AQH_EventTimer_GetReloadOrAlarmValue(const AQH_EVENT_TIMER *evt)
{
  return evt?evt->reloadOrAlarmValue:0;
}



void AQH_EventTimer_SetReloadOrAlarmValue(AQH_EVENT_TIMER *evt, uint64_t i)
{
  if (evt)
    evt->reloadOrAlarmValue=i;
}



int AQH_EventTimer_GetEventType(const AQH_EVENT_TIMER *evt)
{
  return evt?evt->eventType:0;
}



void AQH_EventTimer_SetEventType(AQH_EVENT_TIMER *evt, int i)
{
  if (evt)
    evt->eventType=i;
}



int AQH_EventTimer_CheckWhetherTickTriggers(AQH_EVENT_TIMER *evt, uint64_t t)
{
  if (evt && evt->flags & AQH_EVENT_TIMER_FLAGS_RUNNING) {
    if (evt->timerType==AQH_EventTimerType_CountDown) {
      if (evt->currentValue>0) {
	evt->currentValue--;
	if (evt->currentValue==0) {
	  if (evt->flags & AQH_EVENT_TIMER_FLAGS_RESTART) {
	    DBG_INFO(AQH_LOGDOMAIN, "Timer %lu expired, restarting", (unsigned long int) (evt->id));
	    evt->currentValue=evt->reloadOrAlarmValue;
	  }
	  else {
	    DBG_INFO(AQH_LOGDOMAIN, "Timer %lu expired", (unsigned long int) (evt->id));
	    evt->flags&=~AQH_EVENT_TIMER_FLAGS_RUNNING;
	  }
	  return 1;
	}
      }
    }
    else if (evt->timerType==AQH_EventTimerType_Alarm) {
      if (t>=evt->reloadOrAlarmValue) {
	evt->flags&=~AQH_EVENT_TIMER_FLAGS_RUNNING;
	return 1;
      }
    }
    else {
      DBG_INFO(AQH_LOGDOMAIN, "Unknown timer type %d", evt->timerType);
    }
  }

  return 0;
}







