// __author__ = Juan Carlos De Abreu (jcabreur@gmail.com)

#include "Time.h"

BEGIN_TEG_NAMESPACE();

// Constructor of a Timer
Time::Time(const long& _interval, const int& nSecDuration)
: last(clock()), interval(_interval), end(last + nSecDuration*1000), isRunning(false) {

}


// TODO: change the name
// Method to execute the time
bool Time::run() {
	// Check if the timer is running
	if (!isRunning) return false;
	
	// Obtain the time 
	const clock_t tmp = clock();
	
	// Check if the timer 
	if (tmp >= end && end > 0) {
		// Execute the final task
		//funcEnd.call();
		funcEnd->call();
		// Stop the timer
		isRunning = false;
	}

	// Check if pass an interval to do the task
	if ((tmp - last) > interval) {
		// Update the last time obtained
		last = tmp;
		//printf("%lld\n", tmp);
		// Execute the task
		//funcInterval.call();
		funcInterval->call();
		// Notify that the time passed the interval
		return true;
	}
	// No passed yet the interval
	return false;
}

// TODO: maybe change the name to play ...
// Method to start the time
void Time::start() {
	isRunning = true;
}

// Method to stop the clock
void Time::stop() {
	isRunning = false;
}

// method to reset the clock
void Time::reset() {
	// TODO: implementation..	
}

/*
// method to set the interval to do a task
void Time::setInterval(const long long& _interval) {
	interval = _interval;
}
*/

END_TEG_NAMESPACE();
