#pragma once

#include <QFutureWatcher>
#include <QVector>

#include "SettingsFact.h"
#include "TransectStyleComplexItem.h"

#include "Geometry/WimaJoinedAreaData.h"
#include "Geometry/WimaMeasurementAreaData.h"

class RoutingThread;
class RoutingData;

class CircularSurvey : public TransectStyleComplexItem {
  Q_OBJECT
public:
  using PtrRoutingData = QSharedPointer<RoutingData>;
  enum class Type {
    Circular = 0,
    Linear = 1,
    Count = 2 // Must me last, onyl for counting
  };

  /// @param vehicle Vehicle which this is being contructed for
  /// @param flyView true: Created for use in the Fly View, false: Created for
  /// use in the Plan View
  /// @param kmlOrShpFile Polygon comes from this file, empty for default
  /// polygon
  CircularSurvey(Vehicle *vehicle, bool flyView, const QString &kmlOrShpFile,
                 QObject *parent);
  ~CircularSurvey();

  Q_PROPERTY(QGeoCoordinate refPoint READ refPoint WRITE setRefPoint NOTIFY
                 refPointChanged)
  Q_PROPERTY(Fact *transectDistance READ transectDistance CONSTANT)
  Q_PROPERTY(Fact *alpha READ alpha CONSTANT)
  Q_PROPERTY(Fact *minLength READ minLength CONSTANT)
  Q_PROPERTY(Fact *type READ type CONSTANT)
  Q_PROPERTY(Fact *variant READ variant CONSTANT)
  Q_PROPERTY(Fact *numRuns READ numRuns CONSTANT)
  Q_PROPERTY(Fact *run READ run CONSTANT)
  Q_PROPERTY(int typeCount READ typeCount CONSTANT)
  Q_PROPERTY(bool calculating READ calculating NOTIFY calculatingChanged)
  Q_PROPERTY(bool hidePolygon READ hidePolygon NOTIFY hidePolygonChanged)
  Q_PROPERTY(
      QList<QString> variantNames READ variantNames NOTIFY variantNamesChanged)
  Q_PROPERTY(QList<QString> runNames READ runNames NOTIFY runNamesChanged)

  Q_INVOKABLE void resetReference(void);
  Q_INVOKABLE void reverse(void);

  // Property setters
  void setRefPoint(const QGeoCoordinate &refPt);
  void setHidePolygon(bool hide);
  void setMeasurementArea(const WimaMeasurementAreaData &mArea);
  void setJoinedArea(const WimaJoinedAreaData &jArea);
  void setMeasurementArea(const WimaMeasurementArea &mArea);
  void setJoinedArea(const WimaJoinedArea &jArea);
  void setDepot(const QGeoCoordinate &depot);

  // Property getters
  QGeoCoordinate refPoint() const;
  Fact *transectDistance();
  Fact *alpha();
  Fact *minLength();
  Fact *type();
  Fact *variant();
  Fact *numRuns();
  Fact *run();
  int typeCount() const;
  bool calculating() const;
  bool hidePolygon() const;
  QList<QString> variantNames() const;
  QList<QString> runNames() const;
  QGeoCoordinate depot() const;
  const QList<QList<QGeoCoordinate>> &rawTransects() const;

  // Overrides
  bool load(const QJsonObject &complexObject, int sequenceNumber,
            QString &errorString) override final;
  QString mapVisualQML(void) const override final;
  void save(QJsonArray &planItems) override final;
  bool specifiesCoordinate(void) const override final;
  void appendMissionItems(QList<MissionItem *> &items,
                          QObject *missionItemParent) override final;
  void applyNewAltitude(double newAltitude) override final;
  double timeBetweenShots(void) override final;
  QString commandDescription(void) const override final;
  QString commandName(void) const override final;
  QString abbreviation(void) const override final;
  bool readyForSave(void) const override final;
  double additionalTimeDelay(void) const override final;

  static const char *settingsGroup;
  static const char *transectDistanceName;
  static const char *alphaName;
  static const char *minLengthName;
  static const char *typeName;
  static const char *variantName;
  static const char *numRunsName;
  static const char *runName;
  static const char *CircularSurveyName;
  static const char *refPointLongitudeName;
  static const char *refPointLatitudeName;
  static const char *refPointAltitudeName;

signals:
  void refPointChanged();
  void calculatingChanged();
  void hidePolygonChanged();
  void depotChanged();
  void variantNamesChanged();
  void runNamesChanged();
  void measurementAreaChanged();
  void joinedAreaChanged();

private slots:
  // Overrides from TransectStyleComplexItem
  void _rebuildTransectsPhase1(void) final;
  void _recalcComplexDistance(void) final;
  void _recalcCameraShots(void) final;
  void _setTransects(PtrRoutingData pRoute);

private:
  void _appendLoadedMissionItems(QList<MissionItem *> &items,
                                 QObject *missionItemParent);
  void _buildAndAppendMissionItems(QList<MissionItem *> &items,
                                   QObject *missionItemParent);
  void _changeVariant();
  void _changeRun();

  void _updateWorker();
  void _changeVariantRunWorker();
  void _reverseWorker();
  void _storeWorker();
  void _changeRunWorker();

  // center of the circular lanes, e.g. base station
  QGeoCoordinate _referencePoint;
  QMap<QString, FactMetaData *> _metaDataMap;
  // distance between two neighbour circles
  SettingsFact _transectDistance;
  // angle discretisation of the circles
  SettingsFact _alpha;
  // minimal transect lenght, transects are rejected if they are shorter than
  // this value
  SettingsFact _minLength;
  SettingsFact _type;
  SettingsFact _variant;
  QList<QString> _variantNames;
  SettingsFact _numRuns;
  SettingsFact _run;
  QList<QString> _runNames;

  // Area data
  WimaMeasurementAreaData _mArea;
  WimaJoinedAreaData _jArea;
  QGeoCoordinate _depot;

  // Worker
  using PtrWorker = std::shared_ptr<RoutingThread>;
  PtrWorker _pWorker;
  PtrRoutingData _pRoutingData;

  // Routing data.
  QList<QList<QGeoCoordinate>> _rawTransects;
  using Runs = QVector<Transects>;
  QVector<Runs> _variantVector;

  // State.
  enum class STATE {
    DEFAULT,
    STORE,
    REVERSE,
    VARIANT_CHANGE,
    RUN_CHANGE,
  };
  STATE _state;

  bool _hidePolygon;
};
