#pragma once

#include "ros_bridge/include/messages/geographic_msgs/geopoint.h"

#include <vector>

#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 message
//! generation.
namespace tile {

QString messageType();

namespace {
const char *progressKey = "progress";
const char *idKey = "id";
const char *tileKey = "tile";
} // namespace

//! @brief C++ representation of nemo_msgs/tile message
template <class GeoPoint = geographic_msgs::geo_point::GeoPoint,
          template <class, class...> class Container = std::vector>
class GenericTile {
public:
  typedef Container<GeoPoint> GeoContainer;
  GenericTile() {}
  GenericTile(const GeoContainer &tile, double progress, const QString &id)
      : _tile(tile), _id(id), _progress(progress) {}

  QString id() const { return _id; }
  void setId(const QString &id) { _id = id; }

  double progress() const { return _progress; }
  void setProgress(double progress) { _progress = progress; }

  const GeoContainer &tile() const { return _tile; }
  GeoContainer &tile() { return _tile; }
  void setTile(const GeoContainer &tile) { _tile = tile; }

protected:
  GeoContainer _tile;
  QString _id;
  double _progress;
};
typedef GenericTile<> Tile;

template <class TileType>
bool toJson(const TileType &tile, QJsonObject &value) {

  value[idKey] = tile.id();
  value[progressKey] = tile.progress();

  using namespace messages::geographic_msgs;
  QJsonArray geoPoints;
  for (unsigned long i = 0; i < std::uint64_t(tile.tile().size()); ++i) {
    QJsonObject geoPoint;
    if (!geo_point::toJson(tile.tile()[i], geoPoint)) {
      return false;
    }
    geoPoints.append(std::move(geoPoint));
  }

  value[tileKey] = std::move(geoPoints);

  return true;
}

template <class TileType> bool fromJson(const QJsonObject &value, TileType &p) {
  if (!value.contains(progressKey) || !value[progressKey].isDouble()) {
    return false;
  }

  if (!value.contains(idKey) || !value[idKey].isString()) {
    return false;
  }

  if (!value.contains(tileKey) || !value[tileKey].isArray()) {
    return false;
  }

  p.setId(value[idKey].toString());
  p.setProgress(value[progressKey].toDouble());

  using namespace geographic_msgs;

  const auto jsonArray = value[tileKey].toArray();
  p.tile().clear();
  p.tile().reserve(jsonArray.size());
  typedef decltype(p.tile()[0]) PointTypeCVR;
  typedef
      typename std::remove_cv_t<typename std::remove_reference_t<PointTypeCVR>>
          PointType;
  for (long i = 0; i < jsonArray.size(); ++i) {
    PointType pt;
    if (!geo_point::fromJson(jsonArray[i].toObject(), pt)) {
      return false;
    }
    p.tile().push_back(std::move(pt));
  }

  return true;
}

} // namespace tile
} // namespace nemo_msgs
} // namespace messages
} // namespace ros_bridge
