/// © MiroZ 2024

#ifndef __TASK_FACTORY_H__
#define __TASK_FACTORY_H__

#include "app_config.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include <stdint.h>
#include <esp_err.h>
#include <functional>
#include <list>

typedef std::function<void()> TASK_FUNCTION;

class TaskMgr
{
protected:
	SemaphoreHandle_t m_xMutex = NULL;
	TASK_FUNCTION m_task_function = nullptr;
	std::list<TaskHandle_t> m_tasks;

protected:
	static void taskStarter(void * ptr)	{ (static_cast<TaskMgr*>(ptr)->run()); }
	void run(){	m_task_function(); }

#ifdef TASKMGR_TASK_NAME
	void task();
#endif

private:
	TaskMgr();

public:
	static TaskMgr & getInstance();

	TaskHandle_t createTask(TASK_FUNCTION task_function, const char *name, uint32_t stack_size, UBaseType_t priority, BaseType_t core);
	void deleteTask(TaskHandle_t task = NULL);
	void getInfo();

	~TaskMgr();
};

#endif /* __TASK_FACTORY_H__ */