#include "CircularSurvey.h"
#include "RoutingThread.h"
// QGC
#include "JsonHelper.h"
#include "QGCApplication.h"
#include "QGCLoggingCategory.h"
// Wima
#include "snake.h"
#define CLIPPER_SCALE 1000000
#include "clipper/clipper.hpp"

#include "Geometry/GenericCircle.h"
#include "Snake/SnakeTile.h"

// boost
#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>

#include "CircularGenerator.h"
#include "LinearGenerator.h"

QGC_LOGGING_CATEGORY(CircularSurveyLog, "CircularSurveyLog")

template <typename T>
constexpr typename std::underlying_type<T>::type integral(T value) {
  return static_cast<typename std::underlying_type<T>::type>(value);
}

const char *CircularSurvey::settingsGroup = "CircularSurvey";
const char *CircularSurvey::CircularSurveyName = "CircularSurvey";
const char *CircularSurvey::variantName = "Variant";

CircularSurvey::CircularSurvey(Vehicle *vehicle, bool flyView,
                               const QString &kmlOrShpFile, QObject *parent)
    : TransectStyleComplexItem(vehicle, flyView, settingsGroup, parent),
      _state(STATE::IDLE),
      _metaDataMap(FactMetaData::createMapFromJsonFile(
          QStringLiteral(":/json/CircularSurvey.SettingsGroup.json"), this)),
      _variant(settingsGroup, _metaDataMap[variantName]),
      _pAreaData(std::make_shared<WimaPlanData>()),
      _pWorker(std::make_unique<RoutingThread>()) {

  Q_UNUSED(kmlOrShpFile)
  _editorQml = "qrc:/qml/CircularSurveyItemEditor.qml";

  // Connect facts.
  connect(&this->_variant, &Fact::rawValueChanged, this,
          &CircularSurvey::_changeVariant);

  // Connect worker.
  connect(this->_pWorker.get(), &RoutingThread::result, this,
          &CircularSurvey::_setTransects);
  connect(this->_pWorker.get(), &RoutingThread::calculatingChanged, this,
          &CircularSurvey::calculatingChanged);
  this->_transectsDirty = true;

  // Altitude
  connect(&_cameraCalc, &CameraCalc::distanceToSurfaceRelativeChanged, this,
          &CircularSurvey::coordinateHasRelativeAltitudeChanged);
  connect(&_cameraCalc, &CameraCalc::distanceToSurfaceRelativeChanged, this,
          &CircularSurvey::exitCoordinateHasRelativeAltitudeChanged);

  // Register Generators.
  auto lg = std::make_shared<routing::LinearGenerator>(this->_pAreaData);
  registerGenerator(lg->name(), lg);
  auto cg = std::make_shared<routing::CircularGenerator>(this->_pAreaData);
  registerGenerator(cg->name(), cg);
}

CircularSurvey::~CircularSurvey() {}

void CircularSurvey::reverse() {
  this->_state = STATE::REVERSE;
  this->_rebuildTransects();
}

void CircularSurvey::setPlanData(const WimaPlanData &d) {
  *this->_pAreaData = d;
}

const WimaPlanData &CircularSurvey::planData() const {
  return *this->_pAreaData;
}

WimaPlanData &CircularSurvey::planData() { return *this->_pAreaData; }

QStringList CircularSurvey::variantNames() const { return _variantNames; }

bool CircularSurvey::load(const QJsonObject &complexObject, int sequenceNumber,
                          QString &errorString) {
  // We need to pull version first to determine what validation/conversion
  // needs to be performed
  QList<JsonHelper::KeyValidateInfo> versionKeyInfoList = {
      {JsonHelper::jsonVersionKey, QJsonValue::Double, true},
  };
  if (!JsonHelper::validateKeys(complexObject, versionKeyInfoList,
                                errorString)) {
    return false;
  }

  int version = complexObject[JsonHelper::jsonVersionKey].toInt();
  if (version != 1) {
    errorString = tr("Survey items do not support version %1").arg(version);
    return false;
  }

  QList<JsonHelper::KeyValidateInfo> keyInfoList = {
      {VisualMissionItem::jsonTypeKey, QJsonValue::String, true},
      {ComplexMissionItem::jsonComplexItemTypeKey, QJsonValue::String, true},
      {variantName, QJsonValue::Double, false},
  };

  if (!JsonHelper::validateKeys(complexObject, keyInfoList, errorString)) {
    return false;
  }

  QString itemType = complexObject[VisualMissionItem::jsonTypeKey].toString();
  QString complexType =
      complexObject[ComplexMissionItem::jsonComplexItemTypeKey].toString();
  if (itemType != VisualMissionItem::jsonTypeComplexItemValue ||
      complexType != CircularSurveyName) {
    errorString = tr("%1 does not support loading this complex mission item "
                     "type: %2:%3")
                      .arg(qgcApp()->applicationName())
                      .arg(itemType)
                      .arg(complexType);
    return false;
  }

  _ignoreRecalc = true;

  setSequenceNumber(sequenceNumber);

  if (!_surveyAreaPolygon.loadFromJson(complexObject, true /* required */,
                                       errorString)) {
    _surveyAreaPolygon.clear();
    return false;
  }

  if (!_load(complexObject, errorString)) {
    _ignoreRecalc = false;
    return false;
  }

  _variant.setRawValue(complexObject[variantName].toInt());

  _ignoreRecalc = false;

  _recalcComplexDistance();
  if (_cameraShots == 0) {
    // Shot count was possibly not available from plan file
    _recalcCameraShots();
  }

  return true;
}

QString CircularSurvey::mapVisualQML() const {
  return QStringLiteral("CircularSurveyMapVisual.qml");
}

void CircularSurvey::save(QJsonArray &planItems) {
  QJsonObject saveObject;

  _save(saveObject);

  saveObject[JsonHelper::jsonVersionKey] = 1;
  saveObject[VisualMissionItem::jsonTypeKey] =
      VisualMissionItem::jsonTypeComplexItemValue;
  saveObject[ComplexMissionItem::jsonComplexItemTypeKey] = CircularSurveyName;

  saveObject[variantName] = double(_variant.rawValue().toUInt());

  // Polygon shape
  _surveyAreaPolygon.saveToJson(saveObject);

  planItems.append(saveObject);
}

bool CircularSurvey::specifiesCoordinate() const { return true; }

void CircularSurvey::appendMissionItems(QList<MissionItem *> &items,
                                        QObject *missionItemParent) {
  if (_transectsDirty)
    return;
  if (_loadedMissionItems.count()) {
    // We have mission items from the loaded plan, use those
    _appendLoadedMissionItems(items, missionItemParent);
  } else {
    // Build the mission items on the fly
    _buildAndAppendMissionItems(items, missionItemParent);
  }
}

void CircularSurvey::_appendLoadedMissionItems(QList<MissionItem *> &items,
                                               QObject *missionItemParent) {
  if (_transectsDirty)
    return;
  int seqNum = _sequenceNumber;

  for (const MissionItem *loadedMissionItem : _loadedMissionItems) {
    MissionItem *item = new MissionItem(*loadedMissionItem, missionItemParent);
    item->setSequenceNumber(seqNum++);
    items.append(item);
  }
}

void CircularSurvey::_buildAndAppendMissionItems(QList<MissionItem *> &items,
                                                 QObject *missionItemParent) {
  if (_transectsDirty || _transects.count() == 0)
    return;

  MissionItem *item;
  int seqNum = _sequenceNumber;

  MAV_FRAME mavFrame =
      followTerrain() || !_cameraCalc.distanceToSurfaceRelative()
          ? MAV_FRAME_GLOBAL
          : MAV_FRAME_GLOBAL_RELATIVE_ALT;

  for (const QList<TransectStyleComplexItem::CoordInfo_t> &transect :
       _transects) {
    // bool transectEntry = true;

    for (const CoordInfo_t &transectCoordInfo : transect) {
      item = new MissionItem(
          seqNum++, MAV_CMD_NAV_WAYPOINT, mavFrame,
          0,   // Hold time (delay for hover and capture to settle vehicle
               // before image is taken)
          0.0, // No acceptance radius specified
          0.0, // Pass through waypoint
          std::numeric_limits<double>::quiet_NaN(), // Yaw unchanged
          transectCoordInfo.coord.latitude(),
          transectCoordInfo.coord.longitude(),
          transectCoordInfo.coord.altitude(),
          true,  // autoContinue
          false, // isCurrentItem
          missionItemParent);
      items.append(item);
    }
  }
}

bool CircularSurvey::_switchToGenerator(
    const CircularSurvey::PtrGenerator &newG) {
  if (this->_pGenerator != newG) {
    if (this->_pGenerator != nullptr) {
      disconnect(this->_pGenerator.get(),
                 &routing::GeneratorBase::generatorChanged, this,
                 &CircularSurvey::_rebuildTransects);
    }

    this->_pGenerator = newG;
    connect(this->_pGenerator.get(), &routing::GeneratorBase::generatorChanged,
            this, &CircularSurvey::_rebuildTransects);
    emit generatorChanged();

    this->_state = STATE::IDLE;
    _rebuildTransects();

    return true;
  } else {
    return false;
  }
}

void CircularSurvey::_changeVariant() {
  this->_state = STATE::VARIANT_CHANGE;
  this->_rebuildTransects();
}

void CircularSurvey::_updateWorker() {
  // Mark transects as dirty.
  this->_transectsDirty = true;
  // Reset data.
  this->_transects.clear();
  this->_variantVector.clear();
  this->_variantNames.clear();
  emit variantNamesChanged();

  if (this->_pAreaData->isValid()) {

    // Prepare data.
    auto origin = this->_pAreaData->origin();
    origin.setAltitude(0);
    if (!origin.isValid()) {
      qCDebug(CircularSurveyLog)
          << "_updateWorker(): origin invalid." << origin;
      return;
    }

    // Convert safe area.
    auto geoSafeArea = this->_pAreaData->joinedArea().coordinateList();
    if (!(geoSafeArea.size() >= 3)) {
      qCDebug(CircularSurveyLog)
          << "_updateWorker(): safe area invalid." << geoSafeArea;
      return;
    }
    for (auto &v : geoSafeArea) {
      if (v.isValid()) {
        v.setAltitude(0);
      } else {
        qCDebug(CircularSurveyLog)
            << "_updateWorker(): safe area contains invalid coordinate."
            << geoSafeArea;
        return;
      }
    }

    // Routing par.
    RoutingParameter par;
    par.numSolutions = 5;
    auto &safeAreaENU = par.safeArea;
    snake::areaToEnu(origin, geoSafeArea, safeAreaENU);

    // Create generator.
    if (this->_pGenerator) {
      routing::GeneratorBase::Generator g; // Transect generator.
      if (this->_pGenerator->get(g)) {
        // Start/Restart routing worker.
        this->_pWorker->route(par, g);
      } else {
        qCDebug(CircularSurveyLog)
            << "_updateWorker(): generator creation failed.";
      }
    } else {
      qCDebug(CircularSurveyLog)
          << "_updateWorker(): pGenerator == nullptr, number of registered "
             "generators: "
          << this->_generatorList.size();
    }
  } else {
    qCDebug(CircularSurveyLog) << "_updateWorker(): plan data invalid.";
  }
}

void CircularSurvey::_changeVariantWorker() {
  auto variant = this->_variant.rawValue().toUInt();

  // Find old variant and run. Old run corresponts with empty list.
  std::size_t old_variant = std::numeric_limits<std::size_t>::max();
  for (std::size_t i = 0; i < std::size_t(this->_variantVector.size()); ++i) {
    const auto &variantCoordinates = this->_variantVector.at(i);
    if (variantCoordinates.isEmpty()) {
      old_variant = i;
      break;
    }
  }

  // Swap route.
  if (variant != old_variant) {
    // Swap in new variant.
    if (variant < std::size_t(this->_variantVector.size())) {
      if (old_variant != std::numeric_limits<std::size_t>::max()) {
        // this->_transects containes a route, swap it back to
        // this->_solutionVector
        auto &oldVariantCoordinates = this->_variantVector[old_variant];
        oldVariantCoordinates.swap(this->_transects);
      }
      auto &newVariantCoordinates = this->_variantVector[variant];
      this->_transects.swap(newVariantCoordinates);

    } else { // error
      qCDebug(CircularSurveyLog)
          << "Variant out of bounds (variant =" << variant << ").";
      qCDebug(CircularSurveyLog) << "Resetting variant to zero.";

      disconnect(&this->_variant, &Fact::rawValueChanged, this,
                 &CircularSurvey::_changeVariant);
      this->_variant.setCookedValue(QVariant(0));
      connect(&this->_variant, &Fact::rawValueChanged, this,
              &CircularSurvey::_changeVariant);

      if (this->_variantVector.size() > 0) {
        this->_changeVariantWorker();
      }
    }
  }
}

void CircularSurvey::_reverseWorker() {
  if (this->_transects.size() > 0) {
    auto &t = this->_transects.front();
    std::reverse(t.begin(), t.end());
  }
}

void CircularSurvey::applyNewAltitude(double newAltitude) {
  _cameraCalc.valueSetIsDistance()->setRawValue(true);
  _cameraCalc.distanceToSurface()->setRawValue(newAltitude);
  _cameraCalc.setDistanceToSurfaceRelative(true);
}

double CircularSurvey::timeBetweenShots() { return 1; }

QString CircularSurvey::commandDescription() const {
  return tr("Circular Survey");
}

QString CircularSurvey::commandName() const { return tr("Circular Survey"); }

QString CircularSurvey::abbreviation() const { return tr("C.S."); }

bool CircularSurvey::readyForSave() const {
  return TransectStyleComplexItem::readyForSave() && !_transectsDirty &&
         this->_state == STATE::IDLE;
}

double CircularSurvey::additionalTimeDelay() const { return 0; }

bool CircularSurvey::registerGenerator(
    const QString &name, std::shared_ptr<routing::GeneratorBase> g) {
  if (name.isEmpty()) {
    qCDebug(CircularSurveyLog) << "registerGenerator(): empty name string.";
    return false;
  }

  if (!g) {
    qCDebug(CircularSurveyLog) << "registerGenerator(): empty generator.";
    return false;
  }

  if (this->_generatorNameList.contains(name)) {
    qCDebug(CircularSurveyLog) << "registerGenerator(): generator "
                                  "already registered.";
    return false;
  } else {
    this->_generatorNameList.push_back(name);
    this->_generatorList.push_back(g);
    if (this->_generatorList.size() == 1) {
      _switchToGenerator(g);
    }

    emit generatorNameListChanged();
    return true;
  }
}

bool CircularSurvey::unregisterGenerator(const QString &name) {
  auto index = this->_generatorNameList.indexOf(name);
  if (index >= 0) {
    // Is this the current generator?
    const auto &g = this->_generatorList.at(index);
    if (g == this->_pGenerator) {
      if (index > 0) {
        _switchToGenerator(this->_generatorList.at(index - 1));
      } else {
        _switchToGenerator(nullptr);
        qCDebug(CircularSurveyLog)
            << "unregisterGenerator(): last generator unregistered.";
      }
    }

    this->_generatorNameList.removeAt(index);
    this->_generatorList.removeAt(index);

    emit generatorNameListChanged();
    return true;
  } else {
    qCDebug(CircularSurveyLog)
        << "unregisterGenerator(): generator " << name << " not registered.";
    return false;
  }
}

bool CircularSurvey::unregisterGenerator(int index) {
  if (index > 0 && index < this->_generatorNameList.size()) {
    return unregisterGenerator(this->_generatorNameList.at(index));
  } else {
    qCDebug(CircularSurveyLog) << "unregisterGenerator(): index (" << index
                               << ") out"
                                  "of bounds ( "
                               << this->_generatorList.size() << " ).";
    return false;
  }
}

bool CircularSurvey::switchToGenerator(const QString &name) {
  auto index = this->_generatorNameList.indexOf(name);
  if (index >= 0) {
    _switchToGenerator(this->_generatorList.at(index));
    return true;
  } else {
    qCDebug(CircularSurveyLog)
        << "switchToGenerator(): generator " << name << " not registered.";
    return false;
  }
}

bool CircularSurvey::switchToGenerator(int index) {
  if (index >= 0) {
    _switchToGenerator(this->_generatorList.at(index));
    return true;
  } else {
    qCDebug(CircularSurveyLog) << "unregisterGenerator(): index (" << index
                               << ") out"
                                  "of bounds ( "
                               << this->_generatorNameList.size() << " ).";
    return false;
  }
}

QStringList CircularSurvey::generatorNameList() {
  return this->_generatorNameList;
}

routing::GeneratorBase *CircularSurvey::generator() {
  return _pGenerator.get();
}

int CircularSurvey::generatorIndex() {
  return this->_generatorList.indexOf(this->_pGenerator);
}

void CircularSurvey::_rebuildTransectsPhase1(void) {
  auto start = std::chrono::high_resolution_clock::now();

  switch (this->_state) {
  case STATE::SKIPP:
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: skipp.";
    break;
  case STATE::VARIANT_CHANGE:
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: variant change.";
    this->_changeVariantWorker();
    break;
  case STATE::RUN_CHANGE:
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: run change.";
    this->_changeVariantWorker();
    break;
  case STATE::REVERSE:
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: reverse.";
    this->_reverseWorker();
    break;
  case STATE::IDLE:
    qCDebug(CircularSurveyLog) << "rebuildTransectsPhase1: update.";
    this->_updateWorker();
    break;
  }
  this->_state = STATE::IDLE;

  qCDebug(CircularSurveyLog)
      << "rebuildTransectsPhase1(): "
      << std::chrono::duration_cast<std::chrono::milliseconds>(
             std::chrono::high_resolution_clock::now() - start)
             .count()
      << " ms";
}

void CircularSurvey::_recalcComplexDistance() {
  _complexDistance = 0;
  if (_transectsDirty)
    return;
  for (int i = 0; i < _visualTransectPoints.count() - 1; i++) {
    _complexDistance +=
        _visualTransectPoints[i].value<QGeoCoordinate>().distanceTo(
            _visualTransectPoints[i + 1].value<QGeoCoordinate>());
  }
  emit complexDistanceChanged();
}

// no cameraShots in Circular Survey, add if desired
void CircularSurvey::_recalcCameraShots() { _cameraShots = 0; }

void CircularSurvey::_setTransects(CircularSurvey::PtrRoutingData pRoute) {
  // Store solutions.
  auto ori = this->_pAreaData->origin();
  ori.setAltitude(0);
  const auto &transectsENU = pRoute->transects;
  QVector<Variant> variantVector;
  const auto nSolutions = pRoute->solutionVector.size();

  for (std::size_t j = 0; j < nSolutions; ++j) {
    Variant var{QList<CoordInfo_t>()};
    const auto &solution = pRoute->solutionVector.at(j);
    if (solution.size() > 0) {
      const auto &route = solution.at(0);
      const auto &path = route.path;
      const auto &info = route.info;
      if (info.size() > 1) {
        // Find index of first waypoint.
        std::size_t idxFirst = 0;
        const auto &infoFirst = info.at(1);
        const auto &firstTransect = transectsENU[infoFirst.index];
        if (firstTransect.size() > 0) {
          const auto &firstWaypoint =
              infoFirst.reversed ? firstTransect.back() : firstTransect.front();
          double th = 0.01;
          for (std::size_t i = 0; i < path.size(); ++i) {
            auto dist = bg::distance(path[i], firstWaypoint);
            if (dist < th) {
              idxFirst = i;
              break;
            }
          }

          // Find index of last waypoint.
          std::size_t idxLast = path.size() - 1;
          const auto &infoLast = info.at(info.size() - 2);
          const auto &lastTransect = transectsENU[infoLast.index];
          if (lastTransect.size() > 0) {
            const auto &lastWaypoint =
                infoLast.reversed ? lastTransect.front() : lastTransect.back();
            for (long i = path.size() - 1; i >= 0; --i) {
              auto dist = bg::distance(path[i], lastWaypoint);
              if (dist < th) {
                idxLast = i;
                break;
              }
            }

            // Convert to geo coordinates.
            auto &list = var.front();
            for (std::size_t i = idxFirst; i <= idxLast; ++i) {
              auto &vertex = path[i];
              QGeoCoordinate c;
              snake::fromENU(ori, vertex, c);
              list.append(CoordInfo_t{c, CoordTypeInterior});
            }

          } else {
            qCDebug(CircularSurveyLog)
                << "_setTransects(): lastTransect.size() == 0";
          }
        } else {
          qCDebug(CircularSurveyLog)
              << "_setTransects(): firstTransect.size() == 0";
        }
      } else {
        qCDebug(CircularSurveyLog)
            << "_setTransects(): transectsInfo.size() <= 1";
      }
    } else {
      qCDebug(CircularSurveyLog) << "_setTransects(): solution.size() == 0";
    }

    if (var.size() > 0 && var.front().size() > 0) {
      variantVector.push_back(std::move(var));
    }
  }

  // Assign routes if no error occured.
  if (variantVector.size() > 0) {
    // Swap first route to _transects.
    this->_variantVector.swap(variantVector);

    // If the transects are getting rebuilt then any previously loaded
    // mission items are now invalid.
    if (_loadedMissionItemsParent) {
      _loadedMissionItems.clear();
      _loadedMissionItemsParent->deleteLater();
      _loadedMissionItemsParent = nullptr;
    }

    // Add route variant names.
    this->_variantNames.clear();
    for (std::size_t i = 1; i <= std::size_t(this->_variantVector.size());
         ++i) {
      this->_variantNames.append(QString::number(i));
    }
    emit variantNamesChanged();

    disconnect(&this->_variant, &Fact::rawValueChanged, this,
               &CircularSurvey::_changeVariant);
    this->_variant.setCookedValue(QVariant(0));
    connect(&this->_variant, &Fact::rawValueChanged, this,
            &CircularSurvey::_changeVariant);
    this->_changeVariantWorker();

    // Mark transect ready.
    this->_transectsDirty = false;

    this->_state = STATE::SKIPP;
    this->_rebuildTransects();
  } else {
    qCDebug(CircularSurveyLog)
        << "_setTransects(): failed, variantVector empty.";
    this->_state = STATE::IDLE;
  }
}

Fact *CircularSurvey::variant() { return &_variant; }

bool CircularSurvey::calculating() const {
  return this->_pWorker->calculating();
}

/*!
    \class CircularSurveyComplexItem
    \inmodule Wima

    \brief The \c CircularSurveyComplexItem class provides a survey mission
   item with circular transects around a point of interest.

    CircularSurveyComplexItem class provides a survey mission item with
   circular transects around a point of interest. Within the \c Wima module
   it's used to scan a defined area with constant angle (circular transects)
   to the base station (point of interest).

    \sa WimaArea
*/
