/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#ifndef BLINKER_MODULEMGR_IMPL_H
#define BLINKER_MODULEMGR_IMPL_H

#include <list>
#include <string>

#include "Directory.h"
#include "ListTracker.h"
#include "ListTracker_impl.h"
#include "Mgrs.h"
#include "Module.h"
#include "ModuleMgr.h"
#include "ModuleMgrCntr.h"
#include "ModuleMgrCntr_impl.h"
#include "TimeCallee.h"

namespace Blinker {

/**
 * @brief constructor
 * @param[in] name module manager name / module class name
 * @param[in] name module name
 * @param[in] mgrs managers
 * @param[in] dirBase base directory
 */
template<typename MODULE>
ModuleMgr<MODULE>::ModuleMgr(const std::string &name, Mgrs &mgrs,
                             const Directory &dirBase):
  m_name(name),
  m_mgrs(mgrs),
  m_dirBase(dirBase),
  m_moduleListTracker(*this, dirBase)
{
  m_moduleListTracker.init();

  // request call in 1s
  m_mgrs.m_callMgr.requestTimeCall(this, Time::now() + Time(1));
}

/// destructor
template<typename MODULE>
ModuleMgr<MODULE>::~ModuleMgr()
{
  m_moduleListTracker.clear();
}

/// callback when requested time reached
template<typename MODULE>
void ModuleMgr<MODULE>::timeCall()
{
  updateConfig();

  // request next call in 1s
  m_mgrs.m_callMgr.requestTimeCall(this, Time::now() + Time(1));
}

/// check for update of configuration
template<typename MODULE>
void ModuleMgr<MODULE>::updateConfig()
{
  // module list update
  m_moduleListTracker.updateConfig();
}

} // namespace Blinker

#endif // #ifndef BLINKER_MODULEMGR_IMPL_H

