#pragma once

#include "ros_bridge/rapidjson/include/rapidjson/document.h"

namespace ros_bridge {
//! @brief Namespace containing classes and methodes ros message generation.
namespace messages {
//! @brief Namespace containing classes and methodes for std_msgs generation.
namespace std_msgs {
//! @brief Namespace containing classes and methodes for std_msgs/Time message generation.
namespace time {

std::string messageType();

//! @brief C++ representation of std_msgs/Time
class Time{
public:
    Time(): _secs(0), _nsecs(0) {}
    Time(uint32_t secs, uint32_t nsecs): _secs(secs), _nsecs(nsecs) {}
    Time(const Time &time): _secs(time.secs()), _nsecs(time.nSecs()) {}

    uint32_t secs()  const {return _secs;}
    uint32_t nSecs() const {return _nsecs;}

    void setSecs(uint32_t secs)     {_secs = secs;}
    void setNSecs(uint32_t nsecs)   {_nsecs = nsecs;}

private:
    uint32_t _secs;
    uint32_t _nsecs;
};

template<class TimeType>
bool toJson(const TimeType &time,
            rapidjson::Value &value,
            rapidjson::Document::AllocatorType &allocator)
{
    value.AddMember("secs", rapidjson::Value().SetUint(uint32_t(time.secs())), allocator);
    value.AddMember("nsecs", rapidjson::Value().SetUint(uint32_t(time.nSecs())), allocator);

    return true;
}

template<class TimeType>
bool fromJson(const rapidjson::Value &value,
              TimeType &time)
{
    if (!value.HasMember("secs") || !value["secs"].IsUint()){
        assert(false);
        return false;
    }
    if (!value.HasMember("nsecs")|| !value["nsecs"].IsUint()){
        assert(false);
        return false;
    }
    time.setSecs(value["secs"].GetUint());
    time.setNSecs(value["nsecs"].GetUint());

    return true;
}

} // namespace time
} // namespace std_msgs
} // namespace messages
} // namespace ros_bridge
