#include "WimaMeasurementAreaData.h"
#include "SnakeTile.h"

const char *WimaMeasurementAreaData::typeString = "WimaMeasurementAreaData";

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

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

WimaMeasurementAreaData::WimaMeasurementAreaData(
    const WimaMeasurementArea &other, QObject *parent)
    : WimaAreaData(parent) {
  *this = other;
}

/*!
 * \overload operator=();
 *
 * Assigns \a other to the invoking object.
 */
WimaMeasurementAreaData &WimaMeasurementAreaData::
operator=(const WimaMeasurementAreaData &other) {
  assign(other);

  return *this;
}

/*!
 * \overload operator=();
 *
 * Assigns \a other to the invoking object.
 */
WimaMeasurementAreaData &WimaMeasurementAreaData::
operator=(const WimaMeasurementArea &other) {
  assign(other);

  return *this;
}

QString WimaMeasurementAreaData::type() const { return this->typeString; }

void WimaMeasurementAreaData::assign(const WimaMeasurementAreaData &other) {
  WimaAreaData::assign(other);
  this->tiles.clearAndDeleteContents();
  for (std::size_t i = 0; i < other.tiles.count(); ++i) {
    auto *obj = other.tiles.get(i);
    auto *tile = qobject_cast<SnakeTile *>(obj);
    if (tile != nullptr) {
      this->tiles.append(new SnakeTile(*tile, this));
    } else {
      qWarning() << "WimaMeasurementAreaData::assign(): type cast failed.";
    }
  }
}

void WimaMeasurementAreaData::assign(const WimaMeasurementArea &other) {
  WimaAreaData::assign(other);
  this->tiles.clearAndDeleteContents();
  if (other.ready()) {
    for (std::size_t i = 0; i < other.tiles()->count(); ++i) {
      auto *obj = other.tiles()->get(i);
      auto *tile = qobject_cast<SnakeTile *>(obj);
      if (tile != nullptr) {
        this->tiles.append(new SnakeTile(*tile, this));
      } else {
        qWarning() << "WimaMeasurementAreaData::assign(): type cast failed.";
      }
    }
  } else {
    qWarning()
        << "WimaMeasurementAreaData::assign(): WimaMeasurementArea not ready.";
  }
}
