#pragma once

#include "ros_bridge/include/messages/nemo_msgs/tile.h"

#include <QJsonArray>
#include <QString>

namespace ros_bridge {
//! @brief Namespace containing classes and methodes ros message generation.
namespace messages {
//! @brief Namespace containing classes and methodes for nemo_msgs
//! generation.
namespace nemo_msgs {
//! @brief Namespace containing methodes for nemo_msgs/tile_array message
//! generation.
namespace tile_array {

template <class TileArray>
bool toJson(const TileArray &array, QJsonArray &jsonArray) {

  for (unsigned long i = 0; i < std::uint64_t(array.size()); ++i) {
    QJsonObject jsonTile;
    if (!tile::toJson(array[i], jsonTile)) {
      return false;
    }
    jsonArray.push_back(std::move(jsonTile));
  }

  return true;
}

template <class TileArray>
bool fromJson(const QJsonArray &jsonArray, TileArray &array) {
  using namespace geographic_msgs;

  array.clear();
  array.reserve(jsonArray.size());
  typedef decltype(array[0]) TileCVR;
  typedef typename std::remove_cv_t<typename std::remove_reference_t<TileCVR>>
      Tile;
  for (long i = 0; i < jsonArray.size(); ++i) {
    Tile t;
    if (!tile::fromJson(jsonArray[i], t)) {
      return false;
    }
    array.push_back(std::move(t));
  }

  return true;
}

} // namespace tile_array
} // namespace nemo_msgs
} // namespace messages
} // namespace ros_bridge
