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




namespace WHISPER {

        Leave::Leave(std::string receivedMessage){
             msg = messages::header::Message();
        try {
            msg.ParseFromString(receivedMessage);
            topic_ = msg.topic();
            msgType_ = msg.msgtype();
            senderUUID_ = msg.senderuuid();

            leaveMessage = messages::leave::Leave();

            if (msg.mutable_payload()->Is<messages::leave::Leave>()) 
            {
                msg.mutable_payload()->UnpackTo(&leaveMessage);
            }
        


            port = leaveMessage.port();
            sourceAddr = leaveMessage.srcaddress();



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

        }


        Leave::Leave(std::string senderID, std::uint32_t port, std::string addr):
        Message(senderID,WHISPER::MsgTopics::MANAGEMENT,WHISPER::LEAVE),port(port),sourceAddr(addr)
        {

            leaveMessage = messages::leave::Leave();
            leaveMessage.set_port(port);
            leaveMessage.set_srcaddress(sourceAddr);

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


        }




}