#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();
            msgType_ = msg.msgtype();
            pingMessage = messages::ping::Ping();
            senderUUID_ = msg.senderuuid();

            
                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::string senderID, std::uint32_t port):
        Message(senderID,WHISPER::MsgTopics::MANAGEMENT,WHISPER::PING),port_(port)
        {
            pingMessage = messages::ping::Ping();
            pingMessage.set_port(port_);

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


        }




}