#pragma once

#include <QObject>
#include "WimaArea.h"
#include "SettingsFact.h"
#include "WimaTrackerPolyline.h"
#include "WimaGOperationArea.h"

#include "QScopedPointer"

class WimaGOperationArea : public WimaArea
{
    Q_OBJECT
public:
    WimaGOperationArea(QObject* parent = nullptr);
    WimaGOperationArea(const WimaGOperationArea &other, QObject *parent = nullptr);

    Q_PROPERTY(Fact*            bottomLayerAltitude     READ bottomLayerAltitudeFact    CONSTANT)
    Q_PROPERTY(Fact*            numberOfLayers          READ numberOfLayersFact         CONSTANT)
    Q_PROPERTY(Fact*            layerDistance           READ layerDistanceFact          CONSTANT)
    Q_PROPERTY(Fact*            borderPolygonOffset     READ borderPolygonOffsetFact    CONSTANT)
    Q_PROPERTY(QGCMapPolygon    borderPolygon           READ borderPolygon              NOTIFY borderPolygonChanged)

    // Overrides from WimaPolygon
    QString         mapVisualQML    (void) const { return "WimaGOperationAreaMapVisual.qml";}
    QString         editorQML       (void) const { return "WimaGOperationAreaEditor.qml";}

    // Property accessors
    Fact*           bottomLayerAltitudeFact (void)          { return &_bottomLayerAltitude;}
    Fact*           numberOfLayersFact      (void)          { return &_numberOfLayers;}
    Fact*           layerDistanceFact       (void)          { return &_layerDistance;}
    Fact*           borderPolygonOffsetFact (void)          { return &_borderPolygonOffset;}
    double          bottomLayerAltitude     (void) const    { return _bottomLayerAltitude.rawValue().toDouble();}
    int             numberOfLayers          (void) const    { return _numberOfLayers.rawValue().toInt();}
    double          layerDistance           (void) const    { return _layerDistance.rawValue().toDouble();}
    double          borderPolygonOffset     (void) const    { return _borderPolygonOffset.rawValue().toDouble();}
    QGCMapPolygon   borderPolygon           (void) const    { return _borderPolygon;}

    // Member Methodes
    void saveToJson(QJsonObject& json);
    bool loadFromJson(const QJsonObject& json, QString &errorString);

    // Friends
    friend void print(const WimaGOperationArea& area, QString outputStr);
    friend void print(const WimaGOperationArea& area);

    // Static Variables
    static const char* settingsGroup;
    static const char* bottomLayerAltitudeName;
    static const char* numberOfLayersName;
    static const char* layerDistanceName;
    static const char* borderPolygonOffsetName;
    static const char* wimaGOperationAreaName;

signals:
    void bottomLayerAltitudeChanged     (void);
    void numberOfLayersChanged          (void);
    void layerDistanceChanged           (void);
    void polylineChanged                (void);
    void vehicleCorridorChanged         (WimaVCorridor* corridor);
    void borderPolygonChanged           (void);

public slots:
    /// Updates this with data from area
    void update(const WimaGOperationArea &area);
    void setBottomLayerAltitude (double altitude);
    void setNumberOfLayers      (double numLayers);
    void setLayerDistance       (double layerDistance);
    void setBorderPolygonOffset (double offset);
private slots:
    void recalcBorderPolygon            (void);
private:
    // Member Methodes
    void init();

    // Members
    QMap<QString, FactMetaData*> _metaDataMap;

    SettingsFact            _bottomLayerAltitude;
    SettingsFact            _numberOfLayers;
    SettingsFact            _layerDistance;
    SettingsFact            _borderPolygonOffset;

    QGCMapPolygon           _borderPolygon;
};


