#pragma once

#include <QFutureWatcher>
#include <QVector>

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

class RoutingWorker;
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(int typeCount READ typeCount CONSTANT)
  Q_PROPERTY(bool calculating READ calculating NOTIFY calculatingChanged)
  Q_PROPERTY(bool hidePolygon READ hidePolygon NOTIFY hidePolygonChanged)

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

  // Property setters
  void setRefPoint(const QGeoCoordinate &refPt);
  void setHidePolygon(bool hide);
  void setDepot(const QGeoCoordinate &depot);
  void setSafeArea(const QList<QGeoCoordinate> &safeArea);

  // Property getters
  QGeoCoordinate refPoint() const;
  Fact *transectDistance();
  Fact *alpha();
  Fact *minLength();
  Fact *type();
  int typeCount() const;
  bool calculating() const;
  bool hidePolygon() const;
  QGeoCoordinate depot() const;
  QList<QGeoCoordinate> safeArea() 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 *CircularSurveyName;
  static const char *refPointLongitudeName;
  static const char *refPointLatitudeName;
  static const char *refPointAltitudeName;

signals:
  void refPointChanged();
  void calculatingChanged();
  void hidePolygonChanged();
  void depotChanged();
  void safeAreaChanged();

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);

  // 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;

  // Worker
  using PtrWorker = std::shared_ptr<RoutingWorker>;
  PtrWorker _pWorker;
  PtrRoutingData _workerOutput;
  QList<QList<QGeoCoordinate>> _rawTransects;
  bool _needsStoring;
  bool _needsReversal;
  bool _hidePolygon;

  QGeoCoordinate _depot;
  QList<QGeoCoordinate> _safeArea;
};
