#pragma once

#include <QFutureWatcher>
#include <QVector>

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

class CSWorker;

class CircularSurvey : public TransectStyleComplexItem {
  Q_OBJECT
public:
  using Route = QList<QGeoCoordinate>;
  using PtrRoute = QSharedPointer<Route>;

  /// @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 *deltaR READ deltaR CONSTANT)
  Q_PROPERTY(Fact *deltaAlpha READ deltaAlpha CONSTANT)
  Q_PROPERTY(Fact *transectMinLength READ transectMinLength 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 *deltaR();
  Fact *deltaAlpha();
  Fact *transectMinLength();
  bool calculating() const;
  bool hidePolygon() const;
  QGeoCoordinate depot() const;
  QList<QGeoCoordinate> safeArea() 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 *deltaRName;
  static const char *deltaAlphaName;
  static const char *transectMinLengthName;
  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(PtrRoute 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 _deltaR;
  // angle discretisation of the circles
  SettingsFact _deltaAlpha;
  // minimal transect lenght, transects are rejected if they are shorter than
  // this value
  SettingsFact _minLength;

  using PtrWorker = std::shared_ptr<CSWorker>;
  PtrWorker _pWorker;
  PtrRoute _pRoute;
  bool _needsStoring;
  bool _needsReversal;
  bool _hidePolygon;

  QGeoCoordinate _depot;
  QList<QGeoCoordinate> _safeArea;
};
