#pragma once

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

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


    Q_PROPERTY(double   maxAltitude     READ maxAltitude    WRITE setMaxAltitude    NOTIFY maxAltitudeChanged)
    Q_PROPERTY(QString  mapVisualQML    READ mapVisualQML                           CONSTANT)
    Q_PROPERTY(QString  editorQML       READ editorQML                              CONSTANT)

    //Property accessors
    double      maxAltitude         (void) const    { return _maxAltitude;}

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

    // Member Methodes
    //iterates over all vertices in _polygon and returns the index of that one closest to coordinate
    int             getClosestVertexIndex   (const QGeoCoordinate& coordinate) const;
    //iterates over all vertices in _polygon and returns that one closest to coordinate
    QGeoCoordinate  getClosestVertex        (const QGeoCoordinate& coordinate) const;
    QGCMapPolygon   toQGCPolygon            () const;
    void            join                    (QList<WimaArea*>* polyList,  WimaArea* joinedPoly);// change to & notation
    bool            join                    (WimaArea &poly);
    bool            isDisjunct              (QList<WimaArea*>* polyList);// change to & notation, if necessary
    bool            isDisjunct              (WimaArea* poly1, WimaArea* poly2);// change to & notation, if necessary
    /// calculates the next polygon vertex index
    /// @return index + 1 if index < poly->count()-1 && index >= 0, or 0 if index == poly->count()-1, -1 else
    int             nextVertexIndex         (int index) const;
    /// calculates the previous polygon vertex index
    /// @return index - 1 if index < poly->count() && index > 0, or poly->count()-1 if index == 0, -1 else
    int             previousVertexIndex     (int index) const;


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

    // static Methodes
    static QGCMapPolygon    toQGCPolygon        (const WimaArea& poly);
    /// joins the poly1 and poly2 if possible, joins the polygons to form a simple polygon (no holes)
    /// see https://en.wikipedia.org/wiki/Simple_polygon
    /// @return true if polygons have been joined, false else
    static bool             join                (WimaArea &poly1, WimaArea &poly2, WimaArea& joinedPoly);
    /// checks if line1 and line2 intersect with each other, takes latitude and longitute into account only (height neglected)
    /// @param line1 line containing two coordinates, height not taken into account
    /// @param line2 line containing two coordinates, height not taken into account
    /// @param intersectionPt Coordinate item to store intersection pt. in.
    /// @return false on error or no intersection, true else
    static bool            intersects           (const QGCMapPolyline& line1, const QGCMapPolyline& line2,
                                                 QGeoCoordinate& intersectionPt);
    /// checks if line1 and poly intersect with each other, takes latitude and longitute into account only (height neglected)
    /// @param line line containing two coordinates, height not taken into account
    /// @param intersectionList Empty list to store intersection points in.
    /// @param neighbourList Empty list to store the indices of the neighbours (the two Vertices of poly with the smallest distance to the intersection pt.)
    /// @return false on error or no intersection, true else
    static bool            intersects           (const QGCMapPolyline& line, const WimaArea& poly,
                                                 QList<QGeoCoordinate>& intersectionList,
                                                 QList<QPair<int, int>>& neighbourList);
    /// calculates the distance between to geo coordinates, returns the distance if the path lies within the polygon and inf. else.
    /// @return the distance if the path lies within the polygon and inf. else.
    static double          distInsidePoly       (const QGeoCoordinate& c1, const QGeoCoordinate& c2, WimaArea poly);
    /// calculates the shortes path between two geo coordinates inside a polygon using the Dijkstra Algorithm
    /// @return true if path was found, false else
    static bool            dijkstraPath         (const QGeoCoordinate& c1, const QGeoCoordinate& c2,
                                                 const WimaArea& poly, QList<QGeoCoordinate>& dijkstraPath);
    /// @return true if the polygon is self intersecting
    static bool            isSelfIntersecting   (const WimaArea& poly);
           bool            isSelfIntersecting   ();

    // Friends
    /// prints the member values of area to the outputString
    friend void print(const WimaArea& area, QString& outputString);
    /// prints the member values of area to the console
    friend void print(const WimaArea& area);

    // static Members
    // Accurracy used to compute isDisjunct
    static const double numericalAccuracy;
    static const char*  maxAltitudeName;
    static const char*  wimaAreaName;
    static const char*  areaTypeName;

signals:
    void    maxAltitudeChanged      (void);
    void    vehicleChanged          (void);

public slots:
    void        setMaxAltitude     (double alt);
    /// Updates this with data from area
    void update(const WimaArea& area);

private:
    double                  _maxAltitude;

private:
    void init();
};


