#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();
            sourceType_ = msg.sourcetype();
            msgType_ = msg.msgtype();
            joinMessage = messages::join::Join();
            deviceId_ = msg.sourceid();
            parentId_ = msg.parentid();

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


        }




}