#include "WHISPER/Messages/Message.hpp"
#include <WHISPER/Messages/Join.hpp>
#include <memory>




namespace WHISPER {

        Join::Join(std::string receivedMessage){
             msg = messages::header::Message();
        try {
            msg.ParseFromString(receivedMessage);
            topic_ = msg.topic();
            msgType_ = msg.msgtype();
            joinMessage = messages::join::Join();
            this->senderUUID_ = msg.senderuuid();

            
                if (msg.mutable_payload()->Is<messages::join::Join>()) 
                {
                    msg.mutable_payload()->UnpackTo(&joinMessage);
                }
            
            port = joinMessage.port();
            sourceAddr = joinMessage.srcaddress();


        } catch (const std::exception& e) {
            LOG_S(ERROR)<<e.what();
        }

        }


        WHISPER::Join::Join(std::string senderID, std::uint32_t port, std::string addr):
        Message(senderID,WHISPER::MsgTopics::MANAGEMENT,WHISPER::JOIN),port(port),sourceAddr(addr)
        {
            joinMessage = messages::join::Join();
            joinMessage.set_port(port);
            joinMessage.set_srcaddress(sourceAddr);

            auto payloadMessage = std::make_shared<google::protobuf::Any>();
            payloadMessage->PackFrom(joinMessage);
            addPayLoad(payloadMessage);


        }




}