#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();
            sourceType_ = msg.sourcetype();
            msgType_ = msg.msgtype();
            parentId_ = msg.parentid();

            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::uint32_t parentID,std::uint32_t deviceID, SourceType src,std::uint32_t port, std::string addr):
        Message(parentID,deviceID,WHISPER::MsgTopics::MANAGEMENT,WHISPER::LEAVE,src),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);


        }




}