#ifndef TIMER_H_
#define TIMER_H_

#include <chrono>
#include <thread>
#include <functional>

namespace thermoCamera {

	class Timer {

	public:

		/**
		* Standard constructor, interval is 1 second
		*/
		Timer() :
			_interval(1000),
			_state(false)
		{}

		/**
		* Standard constructor
		* @param[in] mode: 0 for read, 1 for write
		*/
		Timer(unsigned int interval)
		{
			_interval = interval;
			_state = false;
		}

		~Timer()
		{
			_state = false;
		}

		void setFunction(std::function<void(void)> function)
		{
			_timer_start(function,_interval);
		}
		
		/**
		* Set interval 
		* @param[in] interval in ms
		*/
		void setInterval(unsigned int interval)
		{
			_interval = interval;
		}

		unsigned int getInterval() { return _interval; }

		bool GetStatus() { return _state; }

	private:
		
		unsigned int _interval;

		bool _state;

		void _timer_start(std::function<void(void)> func, unsigned int interval)
		{
			_state = true;
			std::thread([this, func, interval]() {
				while (_state)
				{
					if (_state == false) std::terminate();
					func();
					std::this_thread::sleep_for(std::chrono::milliseconds(interval));
				}
				}).detach();
		}
		
	};

}

#endif