#ifndef EVENT_MANAGER_H
#define EVENT_MANAGER_H
#include <mutex>
#include <shared_mutex>
#include <string>
#include <nlohmann/json.hpp>

// #include "thread_pool.h"

namespace zsy
{
    class EventManager
    {
    public:
        class Event
        {
        public:
            const std::string name;
            std::optional<nlohmann::json> data;

            explicit Event(std::string name, const std::string &data);
        };

        using EventHandler = std::function<void(const Event &)>;
        using HandlerList = std::vector<std::shared_ptr<EventHandler> >;

        EventManager();

        void subscribe(const std::string &name, const EventHandler &handler);

        void publish(const std::string &name, const std::string &data = {}) noexcept;

    private:
        std::unordered_map<std::string, HandlerList> handlers;
        std::shared_mutex handlersMutex;
        // std::shared_ptr<ThreadPool> threadPool;
    };
} // zsy

#endif //EVENT_MANAGER_H
