# pragma once

#include "TransectStyleComplexItem.h"

#include "PolygonCalculus.h"
#include "PlanimetryCalculus.h"
#include "GeoUtilities.h"
#include "QVector"
#include "Circle.h"
#include "SettingsFact.h"

class CircularSurveyComplexItem :public TransectStyleComplexItem
{
    Q_OBJECT
public:
    /// @param vehicle Vehicle which this is being contructed for
    /// @param flyView true: Created for use in the Fly View, false: Created for use in the Plan View
    /// @param kmlOrShpFile Polygon comes from this file, empty for default polygon
    CircularSurveyComplexItem(Vehicle* vehicle, bool flyView, const QString& kmlOrShpFile, QObject* parent);

    Q_PROPERTY(QGeoCoordinate refPoint  READ refPoint           WRITE setRefPoint       NOTIFY refPointChanged)
    Q_PROPERTY(Fact* deltaR             READ deltaR                                     CONSTANT)
    Q_PROPERTY(Fact* deltaAlpha         READ deltaAlpha                                 CONSTANT)
    Q_PROPERTY(Fact* transectMinLength  READ transectMinLength                          CONSTANT)
    Q_PROPERTY(Fact* isSnakePath        READ isSnakePath                                CONSTANT)
    Q_PROPERTY(Fact* reverse            READ reverse                                    CONSTANT)
    Q_PROPERTY(Fact* maxWaypoints       READ maxWaypoints                                    CONSTANT)
    Q_PROPERTY(bool  isInitialized      READ isInitialized      WRITE setIsInitialized  NOTIFY isInitializedChanged)

    Q_INVOKABLE void resetReference(void);
    Q_INVOKABLE void setReferencePointBeingChanged(bool changeing); // used by gui to indicate a changeing reference point (dagging by user)

    // Property setters
    void setRefPoint(const QGeoCoordinate &refPt);
    // Set this to true if survey was automatically generated, prevents initialisation from gui.
    void setIsInitialized(bool isInitialized);

    // Property getters
    QGeoCoordinate  refPoint()      const;
    Fact            *deltaR();
    Fact            *deltaAlpha();    
    Fact            *transectMinLength();
    Fact            *isSnakePath();
    Fact            *reverse();
    Fact            *maxWaypoints();
    // Is true if survey was automatically generated, prevents initialisation from gui.
    bool            isInitialized();
    bool            referencePointBeingChanged(); // returns true if the referencepoint is being changed (dragged by user)

    // Overrides from ComplexMissionItem
    bool    load                (const QJsonObject& complexObject, int sequenceNumber, QString& errorString) final;
    QString mapVisualQML        (void) const final { return QStringLiteral("CircularSurveyMapVisual.qml"); }

    // Overrides from TransectStyleComplexItem
    void    save                (QJsonArray&  planItems) final;
    bool    specifiesCoordinate (void) const final { return true; }
    void    appendMissionItems  (QList<MissionItem*>& items, QObject* missionItemParent) final;
    void    applyNewAltitude    (double newAltitude) final;
    double  timeBetweenShots    (void) final;

    // Overrides from VisualMissionionItem
    QString commandDescription  (void) const final { return tr("Circular Survey"); }
    QString commandName         (void) const final { return tr("Circular Survey"); }
    QString abbreviation        (void) const final { return tr("C.S."); }
    bool    readyForSave        (void) const final;
    double  additionalTimeDelay (void) const final;

    static const char* settingsGroup;    
    static const char* deltaRName;
    static const char* deltaAlphaName;
    static const char* transectMinLengthName;
    static const char* isSnakePathName;
    static const char* reverseName;
    static const char* maxWaypointsName;

    static const char* jsonComplexItemTypeValue;
    static const char* jsonDeltaRKey;
    static const char* jsonDeltaAlphaKey;
    static const char* jsonTransectMinLengthKey;
    static const char* jsonIsSnakePathKey;
    static const char* jsonReverseKey;
    static const char* jsonReferencePointLongKey;
    static const char* jsonReferencePointLatKey;
    static const char* jsonReferencePointAltKey;

signals:
    void refPointChanged();
    void isInitializedChanged();

private slots:
    // Overrides from TransectStyleComplexItem
    void _rebuildTransectsPhase1    (void) final; // do not call this function, it is called by TransectStyleComplexItem::_rebuildTransects()
    void _recalcComplexDistance     (void) final;
    void _recalcCameraShots         (void) final;
    void _reverseTransects          (void);

signals:


private:
    void _appendLoadedMissionItems(QList<MissionItem*>& items, QObject* missionItemParent);
    void _buildAndAppendMissionItems(QList<MissionItem*>& items, QObject* missionItemParent);



    QGeoCoordinate _referencePoint; // center of the circular lanes, e.g. base station

    QMap<QString, FactMetaData*> _metaDataMap;

    SettingsFact            _deltaR; // distance between two neighbour circles
    SettingsFact            _deltaAlpha; // angle discretisation of the circles
    SettingsFact            _transectMinLength; // minimal transect lenght, transects are rejected if they are shorter than this value
    SettingsFact            _isSnakePath; // bool value, determining if transects are connected in a snake like or zig zag like manner
    SettingsFact            _reverse; // reverses the _transects path
    SettingsFact            _maxWaypoints; // the maximum number of waypoints _transects (TransectStyleComplexItem) can contain (to avoid performance hits)

    QTimer _updateTimer;

    bool _isInitialized; // indicates if the polygon and refpoint etc. are initialized, prevents reinitialisation from gui and execution of _rebuildTransectsPhase1 during init from gui
    bool _reverseOnly; // if this is true _rebuildTransectsPhase1() will reverse the path only, _rebuildTransectsPhase1() resets _reverseOnly
    bool _referencePointBeingChanged; // is set to true by gui, if user is changeing the reference point

    int _updateCounter;


};



