#pragma once

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

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

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

class RoutingThread;
class RoutingData;

namespace routing {
class GeneratorBase;
}

class CircularSurvey : public TransectStyleComplexItem {
  Q_OBJECT

  using PtrGenerator = std::shared_ptr<routing::GeneratorBase>;
  using PtrRoutingData = std::shared_ptr<RoutingData>;
  using PtrWorker = std::shared_ptr<RoutingThread>;

public:
  /// @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(Fact *variant 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(
      routing::GeneratorBase *generator READ generator NOTIFY generatorChanged)
  Q_PROPERTY(int generatorIndex READ generatorIndex NOTIFY generatorChanged)

  Q_INVOKABLE void reverse(void);

  // Property setters
  void setPlanData(const WimaPlanData &d);

  // Property getters
  Fact *variant();
  QStringList variantNames() const;
  bool calculating() 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;

  // Generator
  bool registerGenerator(const QString &name,
                         std::shared_ptr<routing::GeneratorBase> g);
  bool unregisterGenerator(const QString &name);
  bool unregisterGenerator(int index);
  Q_INVOKABLE bool switchToGenerator(const QString &name);
  Q_INVOKABLE bool switchToGenerator(int index);
  QStringList generatorNameList();
  routing::GeneratorBase *generator();
  int generatorIndex();

  static const char *settingsGroup;
  static const char *variantName;
  static const char *CircularSurveyName;

signals:
  void calculatingChanged();
  void variantNamesChanged();
  void generatorNameListChanged();
  void generatorChanged();

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

  // Worker functions.
  void _setTransects(PtrRoutingData pRoute);
  void _changeVariant();
  void _updateWorker();
  void _changeVariantWorker();
  void _reverseWorker();

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

  bool _switchToGenerator(const PtrGenerator &newG);

  // State.
  enum class STATE {
    IDLE,
    SKIPP,
    REVERSE,
    VARIANT_CHANGE,
    RUN_CHANGE,
  };
  STATE _state;

  // center of the circular lanes, e.g. base station
  QMap<QString, FactMetaData *> _metaDataMap;
  SettingsFact _variant;
  QStringList _variantNames;

  // Area data
  std::shared_ptr<WimaPlanData> _pAreaData;

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

  // Routing.
  using Variant = Transects;
  QVector<Variant> _variantVector;
  PtrWorker _pWorker;
};
