#include "WHISPER/Messages/Message.hpp"
#include "zmq.hpp"
#include <WHISPER/InternalUDPSender.hpp>
#include <memory>
#include <string>

namespace WHISPER {


            /// address to send to 
            std::string address_;
            /// port to send to
            std::uint16_t port_;
            /// zeromq io contex 
            zmq::context_t ctx;
            /// shared pointer to receiving socket 
            std::shared_ptr<zmq::socket_t> senderSocket_ = nullptr;

            InternalUDPSender::InternalUDPSender(std::string address, std::uint16_t port):address_(address),port_(port)
            {
                ctx = zmq::context_t(1);
                senderSocket_ = std::make_shared<zmq::socket_t>(ctx,zmq::socket_type::radio);
                senderSocket_->connect("udp://"+address_+":"+std::to_string(port_));
            }

            InternalUDPSender::~InternalUDPSender()
            {
                senderSocket_->close();
            }

            void InternalUDPSender::send(WHISPER::Message msg,WHISPER::MsgTopics topic)
            {
                std::string MessageString = msg.serialize();
                zmq::message_t zmqMsg(MessageString.begin(),MessageString.end());
                zmqMsg.set_group(WHISPER::MsgTopicsMap[topic].c_str());
                senderSocket_->send(zmqMsg,zmq::send_flags::none);
        
            }

   
}