#include "WHISPER/Messages/Message.hpp"
#include "WHISPER/Messages/Protos/ping.pb.h"
#include <WHISPER/Messages/Ping.hpp>
#include <memory>




namespace WHISPER {

        Ping::Ping(std::string receivedMessage){
             msg = messages::header::Message();
        try {
            msg.ParseFromString(receivedMessage);
            topic_ = msg.topic();
            sourceType_ = msg.sourcetype();
            msgType_ = msg.msgtype();
            pingMessage = messages::ping::Ping();
            deviceId_ = msg.sourceid();
            parentId_ = msg.parentid();

            
                if (msg.mutable_payload()->Is<messages::ping::Ping>()) 
                {
                    msg.mutable_payload()->UnpackTo(&pingMessage);
                }
            
            port_ = pingMessage.port();



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

        }


        WHISPER::Ping::Ping(std::uint32_t parentID,std::uint32_t deviceID, SourceType src,std::uint32_t port):
        Message(parentID,deviceID,WHISPER::MsgTopics::MANAGEMENT,WHISPER::PING,src),port_(port)
        {
            pingMessage = messages::ping::Ping();
            pingMessage.set_port(port_);

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


        }




}