#pragma once

#include "QGCGeo.h"
#include "QGCMapPolygon.h"
#include "QGCMapPolyline.h"
#include "Vehicle.h"
#include "qobject.h"
#include <QLineF>
#include <QPair>
#include <QPointF>

#include "GeoUtilities.h"
#include "PlanimetryCalculus.h"
#include "PolygonCalculus.h"

class WimaArea : public QGCMapPolygon // abstract base class for all WimaAreas
{
  Q_OBJECT
public:
  WimaArea(QObject *parent = nullptr);
  WimaArea(const WimaArea &other, QObject *parent = nullptr);
  WimaArea &operator=(const WimaArea &other);

  Q_PROPERTY(double maxAltitude READ maxAltitude WRITE setMaxAltitude NOTIFY
                 maxAltitudeChanged)
  Q_PROPERTY(QString mapVisualQML READ mapVisualQML CONSTANT)
  Q_PROPERTY(QString editorQML READ editorQML CONSTANT)
  Q_PROPERTY(Fact *borderPolygonOffset READ borderPolygonOffsetFact CONSTANT)
  Q_PROPERTY(QGCMapPolygon *borderPolygon READ borderPolygon NOTIFY
                 borderPolygonChanged)
  Q_PROPERTY(Fact *showBorderPolygon READ showBorderPolygon CONSTANT)
  Q_PROPERTY(bool wimaAreaInteractive READ wimaAreaInteractive WRITE
                 setWimaAreaInteractive NOTIFY wimaAreaInteractiveChanged)

  // Property accessors
  double maxAltitude(void) const { return _maxAltitude; }
  Fact *borderPolygonOffsetFact(void) { return &_borderPolygonOffset; }
  Fact *showBorderPolygon(void) { return &_showBorderPolygon; }
  double borderPolygonOffset(void) const {
    return _borderPolygonOffset.rawValue().toDouble();
  }
  QGCMapPolygon *borderPolygon(void) { return &_borderPolygon; }
  bool wimaAreaInteractive(void) const { return _wimaAreaInteractive; }

  void setWimaAreaInteractive(bool interactive);

  // overrides from WimaArea
  virtual QString mapVisualQML(void) const { return ""; }
  virtual QString editorQML(void) const { return ""; }

  // Member Methodes
  int getClosestVertexIndex(const QGeoCoordinate &coordinate) const;
  QGeoCoordinate getClosestVertex(const QGeoCoordinate &coordinate) const;
  QGCMapPolygon toQGCPolygon() const;
  bool join(WimaArea &area);
  bool join(WimaArea &area, QString &errorString);
  int nextVertexIndex(int index) const;
  int previousVertexIndex(int index) const;
  bool isSimplePolygon() const;
  bool containsCoordinate(const QGeoCoordinate &coordinate) const;

  void saveToJson(QJsonObject &jsonObject);
  bool loadFromJson(const QJsonObject &jsonObject, QString &errorString);

  // static Methodes
  static QGCMapPolygon toQGCPolygon(const WimaArea &area);
  static bool join(const WimaArea &area1, const WimaArea &area2,
                   WimaArea &joinedArea, QString &errorString);
  static bool join(const WimaArea &area1, const WimaArea &area2,
                   WimaArea &joinedArea);

  // Friends
  friend void print(const WimaArea &area, QString &outputString);
  friend void print(const WimaArea &area);

  // static Members
  // Accurracy used to compute isDisjunct
  static const double epsilonMeter;
  static const char *maxAltitudeName;
  static const char *wimaAreaName;
  static const char *areaTypeName;
  static const char *borderPolygonOffsetName;
  static const char *showBorderPolygonName;
  static const char *settingsGroup;

signals:
  void maxAltitudeChanged(void);
  void borderPolygonChanged(void);
  void borderPolygonOffsetChanged(void);
  void wimaAreaInteractiveChanged(void);

public slots:
  void setMaxAltitude(double altitude);
  void setShowBorderPolygon(bool showBorderPolygon);
  void setBorderPolygonOffset(double offset);

private slots:
  void recalcPolygons(void);
  void updatePolygonConnections(QVariant value);
  void recalcInteractivity(void);

private:
  void init();

  double _maxAltitude;

  QMap<QString, FactMetaData *> _metaDataMap;
  SettingsFact _borderPolygonOffset;
  SettingsFact _showBorderPolygon;

  QGCMapPolygon _borderPolygon;

  bool _wimaAreaInteractive;
};
