# 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* fixedDirection     READ fixedDirection                                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 comprehensiveUpdate(void); // triggers a slow recalculation of the transects

    // 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            *fixedDirection();
    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* fixedDirectionName;
    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* jsonfixedDirectionKey;
    static const char* jsonReverseKey;
    static const char* jsonReferencePointLongKey;
    static const char* jsonReferencePointLatKey;
    static const char* jsonReferencePointAltKey;

    static const long triggerTime = 50; // trigger time (ms) for _triggerSlowRecalcTimer

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

private slots:
    // Overrides from TransectStyleComplexItem
    void _rebuildTransectsPhase1    (void) final; // calls _rebuildTransectsFast or _rebuildTransectsSlow depending on _fastRecalc
    void _rebuildTransectsFast      (void);
    void _rebuildTransectsSlow      (void); // the slow version of _rebuildTransectsFast which properly connects the _transects
    void _triggerSlowRecalc         (void);
    bool _generateTransectPath      (QVector<QVector<QPointF>> &transectPath, const QPolygonF &surveyPolygon);
    bool _rebuildTransectsInputCheck(QPolygonF &poly);
    void _rebuildTransectsToGeo     (const QVector<QPointF> &path, const QGeoCoordinate &reference);
    void _recalcComplexDistance     (void) final;
    void _recalcCameraShots         (void) final;
    void _reverseTransects          (void);
    bool _shortestPath              (const QGeoCoordinate &start, const QGeoCoordinate &destination, QVector<QGeoCoordinate> &shortestPath);


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            _fixedDirection; // bool value, determining if transects have fixed direction or not
    SettingsFact            _reverse; // reverses the _transects path
    SettingsFact            _maxWaypoints; // the maximum number of waypoints _transects (TransectStyleComplexItem) can contain (to avoid performance hits)

    QTimer _triggerSlowRecalcTimer;

    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 _doFastRecalc; // fast recalc of transects if set, see _rebuildTransectsPhase1 for furhter explanation
};





