#include "WimaAreaData.h"

WimaAreaData::WimaAreaData(QObject *parent) : QObject(parent) {}

WimaAreaData::~WimaAreaData() {}

bool WimaAreaData::operator==(const WimaAreaData &data) const {
  return this->_path == data._path && this->_center == data._center;
}

bool WimaAreaData::operator!=(const WimaAreaData &data) const {
  return !this->operator==(data);
}

QVariantList WimaAreaData::path() const { return _path; }

QGeoCoordinate WimaAreaData::center() const { return _center; }

const QList<QGeoCoordinate> &WimaAreaData::coordinateList() const {
  return _list;
}

bool WimaAreaData::containsCoordinate(const QGeoCoordinate &coordinate) const {
  using namespace PlanimetryCalculus;
  using namespace PolygonCalculus;
  using namespace GeoUtilities;

  if (_path.size() > 2) {
    QPolygonF polygon;
    toCartesianList(this->coordinateList(), coordinate, polygon);
    return PlanimetryCalculus::contains(polygon, QPointF(0, 0));
  } else
    return false;
}

void WimaAreaData::append(const QGeoCoordinate &c) {
  _list.append(c);
  _path.push_back(QVariant::fromValue(c));
}

void WimaAreaData::push_back(const QGeoCoordinate &c) { append(c); }

void WimaAreaData::clear() {
  _list.clear();
  _path.clear();
}

void WimaAreaData::setPath(const QVariantList &coordinateList) {
  _path = coordinateList;
  _list.clear();
  for (auto variant : coordinateList) {
    _list.push_back(variant.value<QGeoCoordinate>());
  }
}

void WimaAreaData::setCenter(const QGeoCoordinate &center) {
  if (_center != center) {
    _center = center;

    emit centerChanged();
  }
}

/*!
 * \fn void WimaAreaData::assign(const WimaAreaData &other)
 *
 * Assigns \a other to the invoking object
 */
void WimaAreaData::assign(const WimaAreaData &other) {
  setPath(other.path());
  setCenter(other.center());
}

void WimaAreaData::assign(const WimaArea &other) {
  setPath(other.path());
  setCenter(other.center());
}

/*!
 * \fn void WimaAreaData::setPath(const QList<QGeoCoordinate> &coordinateList)
 *
 * Sets the path member to \a coordinateList by copying all entries of \a
 * coordinateList. Emits the \c pathChanged() signal.
 */
void WimaAreaData::setPath(const QList<QGeoCoordinate> &coordinateList) {
  _list = coordinateList;

  _path.clear();
  // copy all coordinates to _path
  for (int i = 0; i < coordinateList.size(); i++) {
    _path.append(QVariant::fromValue(coordinateList.value(i)));
  }

  emit pathChanged(_path);
}

bool operator==(const WimaAreaData &m1, const WimaArea &m2) {
  return m1.path() == m2.path() && m1.center() == m2.center();
}
bool operator!=(const WimaAreaData &m1, const WimaArea &m2) {
  return !operator==(m1, m2);
}

/*!
 * \class WimaArea::WimaAreaData
 * \brief Class to store and exchange data of a \c WimaArea Object.
 * Class to store and exchange data of a \c WimaArea Object. In contrast to \c
 * WimaArea this class does not uses the QGC Fact System. It is designed to
 * exchange data between the \c WimaPlaner and the \c WimaController class. And
 * it is the base class for any derived data objects
 *
 * \sa WimaArea
 */
