#pragma once


#include "WHISPER/Messages/Message.hpp"
#include "WHISPER/whisper.hpp"
#include <atomic>
#include <cstdint>
#include <list>
#include <memory>
#include <vector>
#include <WHISPER/localClients.hpp>

#define ZMQ_BUILD_DRAFT_API 1
#include <zmq.hpp>
#include <zmq_addon.hpp>

namespace WHISPER {

    class InternalUDPListener{

        private:
            std::uint16_t port_;
            //zeromq io contex 
            zmq::context_t ctx;
            ///shared pointer to receiving socket 
            std::shared_ptr<zmq::socket_t> receiverSocket_ = nullptr;
            std::thread listeningThread;

            std::atomic<bool> listening_ = false;
            std::atomic<bool> stoplistening_ = false;

            std::vector<WHISPER::MsgTopics> subscribedTopics;

            std::shared_ptr<threadSafeQueue<WHISPER::Message>> receiverQueue_;


            void listen();

            std::uint16_t checkPort(std::uint16_t port);

        public:
            InternalUDPListener(std::uint16_t port);
            ~InternalUDPListener();
            void connect(std::shared_ptr<threadSafeQueue<WHISPER::Message>> receiver);
            void start();
            void stop();

            std::uint16_t getPort();
            
            void subscribe(WHISPER::MsgTopics topic);
            void unsubscribe(WHISPER::MsgTopics topic);



    };
}
