#pragma once

#include <QFutureWatcher>
#include <QJsonArray>
#include <QVector>
#include <memory>

#include "ComplexMissionItem.h"
#include "QGCQGeoCoordinate.h"
#include "SettingsFact.h"

#include "AreaData.h"

class RoutingThread;
class RoutingData;

namespace routing {
class GeneratorBase;
}

class MeasurementComplexItem : public ComplexMissionItem {
  Q_OBJECT

  using PtrGenerator = routing::GeneratorBase *;
  using PtrAreaData = AreaData *;
  using PtrRoutingData = std::shared_ptr<RoutingData>;
  using PtrWorker = RoutingThread *;
  using Variant = QVariantList;

  enum class STATE { IDLE, ROUTING, EDITING };

public:
  MeasurementComplexItem(PlanMasterController *masterController, bool flyView,
                         const QString &kmlOrShpFile, QObject *parent);
  ~MeasurementComplexItem();

  Q_PROPERTY(Fact *variant READ variant CONSTANT)
  Q_PROPERTY(Fact *altitude READ variant CONSTANT)
  Q_PROPERTY(
      QStringList variantNames READ variantNames NOTIFY variantNamesChanged)
  Q_PROPERTY(QStringList generatorNameList READ generatorNameList NOTIFY
                 generatorNameListChanged)
  Q_PROPERTY(bool calculating READ calculating NOTIFY calculatingChanged)
  Q_PROPERTY(bool editing READ editing NOTIFY editingChanged)
  Q_PROPERTY(bool idle READ idle NOTIFY idleChanged)
  Q_PROPERTY(
      routing::GeneratorBase *generator READ generator NOTIFY generatorChanged)
  Q_PROPERTY(int generatorIndex READ generatorIndex NOTIFY generatorChanged)
  Q_PROPERTY(AreaData *areaData READ areaData NOTIFY areaDataChanged)
  Q_PROPERTY(QVariantList route READ route NOTIFY routeChanged)

  Q_INVOKABLE void reverseRoute(void);

  // Overrides from ComplexMissionItem
  virtual QString patternName(void) const override final;
  virtual double complexDistance(void) const override final;
  virtual int lastSequenceNumber(void) const final;
  virtual bool load(const QJsonObject &complexObject, int sequenceNumber,
                    QString &errorString) override final;
  virtual double
  greatestDistanceTo(const QGeoCoordinate &other) const override final;
  virtual QString mapVisualQML(void) const override final;

  // Overrides from VisualMissionItem
  virtual bool dirty(void) const override final;
  virtual bool isSimpleItem(void) const override final;
  virtual bool isStandaloneCoordinate(void) const override final;
  virtual bool specifiesCoordinate(void) const override final;
  virtual bool specifiesAltitudeOnly(void) const override final;
  virtual QGeoCoordinate coordinate(void) const override final;
  virtual QGeoCoordinate exitCoordinate(void) const override final;
  virtual int sequenceNumber(void) const override final;
  virtual double specifiedFlightSpeed(void) final override;
  virtual double specifiedGimbalYaw(void) final override;
  virtual double specifiedGimbalPitch(void) final override;
  virtual void appendMissionItems(QList<MissionItem *> &items,
                                  QObject *missionItemParent) final override;
  virtual void setMissionFlightStatus(
      const MissionController::MissionFlightStatus_t &missionFlightStatus)
      final override;
  virtual void applyNewAltitude(double newAltitude) override final;
  virtual double additionalTimeDelay(void) const override final;
  virtual ReadyForSaveState readyForSaveState(void) const override final;
  virtual bool exitCoordinateSameAsEntry(void) const override final;
  virtual void setDirty(bool dirty) override final;
  virtual void setCoordinate(const QGeoCoordinate &coordinate) override final;
  virtual void setSequenceNumber(int sequenceNumber) override final;
  virtual void save(QJsonObject &missionItems) override final;
  virtual double amslEntryAlt(void) const override final;
  virtual double amslExitAlt(void) const override final;
  virtual double minAMSLAltitude(void) const override final;
  virtual double maxAMSLAltitude(void) const override final;
  virtual QString commandDescription(void) const override final;
  virtual QString commandName(void) const override final;
  virtual QString abbreviation(void) const override final;

  // Generator
  bool addGenerator(const QString &name, routing::GeneratorBase *g);
  bool removeGenerator(const QString &name);
  bool removeGenerator(int index);
  Q_INVOKABLE bool switchToGenerator(const QString &name);
  Q_INVOKABLE bool switchToGenerator(int index);
  QStringList generatorNameList();
  routing::GeneratorBase *generator();
  int generatorIndex();

  // Editing.
  //!
  //! \brief startEditing Starts area data editing.
  //!
  //! Starts area data editing. Route will not be updated bewteen a call
  //! sequence of editingStart() and editingStop().
  //!
  Q_INVOKABLE void startEditing();
  //!
  //! \brief stopEditing Stops area editing.
  //!
  //! Stops area editing. Will reset area data to the state before
  //! editingStart() if it is invalid. Triggers a route update.
  //!
  Q_INVOKABLE void stopEditing();
  //!
  //! \brief abortEditing Aborts area editing.
  //!
  //! Will reset area data to the state before
  //! editingStart().
  //!
  Q_INVOKABLE void abortEditing();

  // Property getters
  const AreaData *areaData() const;
  AreaData *areaData();
  QVariantList route();

  Fact *variant();
  Fact *altitude();
  QStringList variantNames() const;

  bool calculating() const;
  bool editing() const;
  bool idle() const;
  bool followTerrain() const;

  static const char *settingsGroup;
  static const char *variantName;
  static const char *altitudeName;
  static const char *jsonComplexItemTypeValue;
  static const QString name;

signals:
  void variantNamesChanged();

  void generatorNameListChanged();
  void generatorChanged();

  void calculatingChanged();
  void editingChanged();
  void idleChanged();

  void areaDataChanged();
  void routeChanged();

private slots:

  // Worker functions.
  void _storeRoutingData(PtrRoutingData pRoute);
  void _updateRoute();
  void _changeVariant();
  void _reverseRoute();

private:
  bool _setGenerator(PtrGenerator newG);
  void _setState(STATE state);
  void _setAreaData(PtrAreaData data);
  static bool _calculating(STATE state);
  static bool _editing(STATE state);
  static bool _idle(STATE state);
  void _updateFlightpathSegments();

  // Hirarcical stuff.
  int _sequenceNumber;
  bool _followTerrain;

  // State.
  STATE _state;

  // Facts
  QMap<QString, FactMetaData *> _metaDataMap;
  SettingsFact _altitude;
  SettingsFact _variant;
  QStringList _variantNames;

  // Area data
  PtrAreaData _pAreaData;
  PtrAreaData _pEditorData;
  PtrAreaData _pCurrentData;

  // Generators
  QList<PtrGenerator> _generatorList;
  QStringList _generatorNameList;
  PtrGenerator _pGenerator;

  // Routing.
  QVector<Variant> _variantVector;
  Variant _route;
  PtrWorker _pWorker;
};
