#include "WimaPlanData.h"

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

WimaPlanData::WimaPlanData(const WimaPlanData &other, QObject *parent)
    : QObject(parent) {
  *this = other;
}

WimaPlanData &WimaPlanData::operator=(const WimaPlanData &other) {
  this->set(other.measurementArea());
  this->set(other.serviceArea());
  this->set(other.joinedArea());
  this->set(other.corridor());

  return *this;
}

void WimaPlanData::set(const WimaJoinedAreaData &areaData) {
  if (_joinedArea != areaData) {
    _joinedArea = areaData;
    emit joinedAreaChanged();
  }
}

void WimaPlanData::set(const WimaServiceAreaData &areaData) {
  if (_serviceArea != areaData) {
    _serviceArea = areaData;
    emit serviceAreaChanged();
  }
}

void WimaPlanData::set(const WimaCorridorData &areaData) {
  if (_corridor != areaData) {
    _corridor = areaData;
    emit corridorChanged();
  }
}

void WimaPlanData::set(const WimaMeasurementAreaData &areaData) {
  if (_measurementArea != areaData) {
    _measurementArea = areaData;
    emit measurementAreaChanged();

    if (_measurementArea.coordinateList().size() > 0) {
      setOrigin(_measurementArea.coordinateList().first());
    } else {
      setOrigin(QGeoCoordinate());
    }
  }
}

void WimaPlanData::clear() { *this = WimaPlanData(); }

QGeoCoordinate WimaPlanData::origin() { return _origin; }

bool WimaPlanData::isValid() {
  return _measurementArea.coordinateList().size() >= 3 &&
         _serviceArea.coordinateList().size() >= 3 && _origin.isValid();
}

const WimaJoinedAreaData &WimaPlanData::joinedArea() const {
  return this->_joinedArea;
}

const WimaServiceAreaData &WimaPlanData::serviceArea() const {
  return this->_serviceArea;
}

const WimaCorridorData &WimaPlanData::corridor() const {
  return this->_corridor;
}

const WimaMeasurementAreaData &WimaPlanData::measurementArea() const {
  return this->_measurementArea;
}

WimaJoinedAreaData &WimaPlanData::joinedArea() { return this->_joinedArea; }

WimaServiceAreaData &WimaPlanData::serviceArea() { return this->_serviceArea; }

WimaCorridorData &WimaPlanData::corridor() { return this->_corridor; }

WimaMeasurementAreaData &WimaPlanData::measurementArea() {
  return this->_measurementArea;
}

bool WimaPlanData::operator==(const WimaPlanData &other) const {
  return this->_joinedArea == other._joinedArea &&
         this->_measurementArea == other._measurementArea &&
         this->_corridor == other._corridor &&
         this->_serviceArea == other._serviceArea;
}
bool WimaPlanData::operator!=(const WimaPlanData &other) const {
  return !(*this == other);
}

void WimaPlanData::setOrigin(const QGeoCoordinate &origin) {
  if (this->_origin != origin) {
    this->_origin = origin;
    emit originChanged();
  }
}
