#pragma once

#include "QScopedPointer"
#include <QObject>

#include "WimaArea.h"

#include "SettingsFact.h"


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

    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 "WimaMeasurementAreaMapVisual.qml";}
    QString         editorQML       (void) const { return "WimaMeasurementAreaEditor.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)          { return &_borderPolygon;}

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

    // Friends
    friend void print(const WimaMeasurementArea& area, QString outputStr);
    friend void print(const WimaMeasurementArea& 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* WimaMeasurementAreaName;

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

public slots:
    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;
};


