/*
 * manager.hpp
 *
 *  Created on: Sep 19, 2023
 *      Author: Dario
 */

#ifndef INCLUDE_STA_TACOS_MANAGER_HPP_
#define INCLUDE_STA_TACOS_MANAGER_HPP_


#include <sta/config.hpp>

#ifndef STA_TACOS_MANAGER_PRIORITY
#	error "Manger task priority not specified in config.hpp"
#else

#include <set>
#include <list>
#include <memory>
#include <sta/tacos/thread.hpp>

namespace sta
{
	namespace tacos
	{
		class Manager : public TacosThread
		{
		public:
			static Manager* instance()
			{
				static CGuard g;

				if (!_instance)
				{
					// Create a the manager singleton instance.
					Manager::_instance = new Manager();
				}

				return _instance;
			}

			/**
			 *
			 */
			void registerThread(std::shared_ptr<TacosThread> thread, std::list<uint16_t> states);

			void init() override;

			void func() override;

		private:
			static Manager* _instance;

			class CGuard
			{
			public:
			   ~CGuard()
			   {
				  if( NULL != Manager::_instance )
				  {
					 delete Manager::_instance;
					 Manager::_instance = NULL;
				  }
			   }
			};

			Manager();

			Manager(const Manager&);

			//~Manager();

			void updateThreads();

			void startThreads(uint16_t state);

			void stopThreads(uint16_t state);

			std::set<std::shared_ptr<TacosThread>> threads_[STA_TACOS_NUM_STATES];
		};
	}  // namespace tacos
}  // namespace sta

#endif // STA_TACOS_MANAGER_PRIORITY

#endif /* INCLUDE_STA_TACOS_MANAGER_HPP_ */
