
#include "common/dispatcher.h"
#include <config/xf-config.h>

#if (USE_XF_COMMON_TIMEOUTMANAGER_CLASS != 0)

#include <cassert>
#include "xf/interface/behavior.h"
#include "xf/interface/mutex.h"
#include "timeoutmanager.h"
#if defined(XF_TRACE_EVENT_PUSH_POP) && (XF_TRACE_EVENT_PUSH_POP != 0)
    #include "trace/trace.h"
#endif // XF_TRACE_EVENT_PUSH_POP

using Mutex = interface::XFMutex;       // Rename XFMutex interface class to Mutex for easier use.

// Implementation of the getInstance() method of the 'interface::XFTimeoutManager' class.
//
// Note: The implementation is done here because only in this file the real XFTimeoutManager
//       class is known (port specific class). An instance of the XFTimeoutManager class is
//       returned by the 'interface::XFTimeoutManager' class.
interface::XFTimeoutManager * interface::XFTimeoutManager::getInstance() {
    static ::XFTimeoutManager timeoutManager;
    return &timeoutManager;
}

// TODO done: Implement code for XFTimeoutManager class

XFTimeoutManager::XFTimeoutManager() {
    this->pMutex_ = interface::XFMutex::create();
}

XFTimeoutManager::~XFTimeoutManager() {

}

void XFTimeoutManager::addTimeout(XFTimeout *pNewTimeout) {
    this->pMutex_->lock();
    this->timeouts_.push_front(pNewTimeout);
    this->pMutex_->unlock();

}

void XFTimeoutManager::returnTimeout(XFTimeout *pTimeout) {
    this->pMutex_->lock();
    XFDispatcher::getInstance()->pushEvent(pTimeout);
    this->timeouts_.remove(pTimeout);
    this->pMutex_->unlock();
}

void XFTimeoutManager::start(std::function<void (uint32_t)> startTimeoutManagerTimer) {
    startTimeoutManagerTimer(this->tickInterval_);
}

void XFTimeoutManager::scheduleTimeout(int32_t timeoutId, int32_t interval, interface::XFBehavior *pBehavior) {
    string str = "[DEBUG] - Schedule Timeout: ";
    str += to_string(timeoutId);
    //Trace::out(str);
    ::XFTimeout* timeout = new XFTimeout(timeoutId, interval, pBehavior);
    addTimeout(timeout);
}

void XFTimeoutManager::unscheduleTimeout(int32_t timeoutId, interface::XFBehavior *pBehavior) {
    string str = "[DEBUG] - Unschedule Timeout: ";
    str += to_string(timeoutId);
    //Trace::out(str);
    this->pMutex_->lock();
    TimeoutList::iterator it;
    for(it = this->timeouts_.begin(); it != this->timeouts_.end(); it++){

        if((*it)->getId()==timeoutId && (*it)->getBehavior() == pBehavior) {
            it = this->timeouts_.erase(it);
        }

    }
    this->pMutex_->unlock();
}

void XFTimeoutManager::tick() {
    //Trace::out("[DEBUG] - Tick");
    if(!this->timeouts_.empty()) {
        TimeoutList::iterator it;

        this->pMutex_->lock();
        for(it=this->timeouts_.begin(); it != this->timeouts_.end(); it++) {
            if((*it)->getRelTicks() > 0) {
                (*it)->substractFromRelTicks(this->tickInterval_);
            } else {
                XFEvent* ev = *(it);
                XFDispatcher::getInstance()->pushEvent(ev);
                it = this->timeouts_.erase(it);
            }
        }
        this->pMutex_->unlock();
    }
}

#endif // USE_XF_COMMON_TIMEOUTMANAGER_CLASS
