#pragma once




#include "WHISPER/threadSafeQueue.hpp"
#include "zmq.hpp"
#include <atomic>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <sys/types.h>
#include <thread>
#include <vector>



namespace DirectCommunication 
{

    class DirectCommunicationServer
    {
        public:
            DirectCommunicationServer(ushort port);
            ~DirectCommunicationServer();

            void sendMessage(std::string msg);
            std::string getLatestMessage();

            void registerMessageCallback(std::function<void(std::string)> MessageHandle);

            int countClients();

        private:
            ushort port_;

            zmq::context_t context_;
            zmq::socket_t socket_;

            void workerFunc_();
            std::thread Worker_;
            std::atomic_bool stopWorker_;

            WHISPER::threadSafeQueue<std::string> receivedMessages_;
            std::vector<std::uint32_t> connectedClients_;

            bool hasClient(std::uint32_t clientId);
            void deleteClient(std::uint32_t clientId);

            std::function<void(std::string)> MessageHandle_ = nullptr;

            
    };


}