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

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

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


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

    // static Methodes
    static QGCMapPolygon    toQGCPolygon        (const WimaArea& area);
    static bool             join                (WimaArea &area1, WimaArea &area2, WimaArea& joinedArea, QString &errorString);
    static bool             join                (WimaArea &area1, WimaArea &area2, WimaArea& joinedArea);
    static bool            intersects           (const QGCMapPolyline& line1, const QGCMapPolyline& line2,
                                                 QGeoCoordinate& intersectionPt);
    static bool            intersects           (const QGCMapPolyline& line, const WimaArea& area,
                                                 QList<QGeoCoordinate>& intersectionList,
                                                 QList<QPair<int, int>>& neighbourList);
    static double          distInsidePoly       (const QGeoCoordinate& c1, const QGeoCoordinate& c2, WimaArea area);
    static bool            dijkstraPath         (const QGeoCoordinate& c1, const QGeoCoordinate& c2,
                                                 const WimaArea& area, QList<QGeoCoordinate>& dijkstraPath, QString &errorstring);
    static bool            isSelfIntersecting   (const WimaArea& area);
           bool            isSelfIntersecting   ();

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

signals:
    void    maxAltitudeChanged      (void);

public slots:
    void        setMaxAltitude     (double altitude);

private:
    double                  _maxAltitude;

private:
    void init();
};





