#pragma once

#include <QObject>
#include "QGCMapPolygon.h"
#include "QmlObjectListModel.h"

#include "WimaArea.h"
#include "WimaAreaData.h"
#include "WimaServiceArea.h"
#include "WimaServiceAreaData.h"
#include "WimaMeasurementArea.h"
#include "WimaMeasurementAreaData.h"
#include "WimaCorridor.h"
#include "WimaCorridorData.h"
#include "WimaJoinedArea.h"
#include "WimaJoinedAreaData.h"
#include "WimaPlanData.h"
#include "WimaDataContainer.h"

#include "PlanMasterController.h"
#include "MissionController.h"
#include "SurveyComplexItem.h"
#include "SimpleMissionItem.h"
#include "MissionSettingsItem.h"
#include "JsonHelper.h"
#include "QGCApplication.h"

#include "OptimisationTools.h"
#include "PlanimetryCalculus.h"
#include "GeoUtilities.h"

#define TEST_FUN 0

#if TEST_FUN
    #include "TestPolygonCalculus.h"
    #include "testplanimetrycalculus.h"
#endif

class WimaPlaner : public QObject
{
    Q_OBJECT

    enum FileType {WimaFile, PlanFile};

public:
    WimaPlaner(QObject *parent = nullptr);
    template<class T>
    WimaPlaner(T t, QObject *parent = nullptr) = delete;
    template<class T>
    WimaPlaner(T t) = delete;

    Q_PROPERTY(PlanMasterController*    masterController    READ masterController       WRITE setMasterController       NOTIFY masterControllerChanged)
    Q_PROPERTY(MissionController*       missionController   READ missionController      WRITE setMissionController      NOTIFY missionControllerChanged)
    Q_PROPERTY(QmlObjectListModel*      visualItems         READ visualItems                                            NOTIFY visualItemsChanged)
    Q_PROPERTY(int                      currentPolygonIndex READ currentPolygonIndex    WRITE setCurrentPolygonIndex    NOTIFY currentPolygonIndexChanged)
    Q_PROPERTY(QString                  currentFile         READ currentFile                                            NOTIFY currentFileChanged)
    Q_PROPERTY(QStringList              loadNameFilters     READ loadNameFilters                                        CONSTANT)
    Q_PROPERTY(QStringList              saveNameFilters     READ saveNameFilters                                        CONSTANT)
    Q_PROPERTY(QString                  fileExtension       READ fileExtension                                          CONSTANT)
    Q_PROPERTY(QGeoCoordinate           joinedAreaCenter    READ joinedAreaCenter                                       CONSTANT)
    Q_PROPERTY(WimaDataContainer*       dataContainer       READ dataContainer          WRITE setDataContainer          NOTIFY dataContainerChanged)
    Q_PROPERTY(bool                     missionReady        READ missionReady                                           NOTIFY missionReadyChanged)


    // Property accessors
    PlanMasterController*       masterController       (void)          { return _masterController; }
    MissionController*          missionController      (void)          { return _missionController; }
    QmlObjectListModel*         visualItems            (void) ;
    int                         currentPolygonIndex    (void) const    { return _currentAreaIndex; }
    QString                     currentFile            (void) const    { return _currentFile; }
    QStringList                 loadNameFilters        (void) const;
    QStringList                 saveNameFilters        (void) const;
    QString                     fileExtension          (void) const    { return wimaFileExtension; }
    QGeoCoordinate              joinedAreaCenter       (void) const;
    bool                        missionReady           (void) const    { return _missionReady; }
    WimaDataContainer*          dataContainer          (void)          { return _container;}

    // Property setters
    void setMasterController        (PlanMasterController* masterController);
    void setMissionController       (MissionController* missionController);
    /// Sets the integer index pointing to the current polygon. Current polygon is set interactive.
    void setCurrentPolygonIndex     (int index);
    void setDataContainer           (WimaDataContainer* container);

    // Member Methodes
    Q_INVOKABLE bool addMeasurementArea();
    /// Removes an area from _visualItems
    /// @param index Index of the area to be removed
    Q_INVOKABLE void removeArea(int index);
    Q_INVOKABLE bool addServiceArea();
    Q_INVOKABLE bool addCorridor();
    /// Remove all areas from WimaPlaner and all mission items from MissionController
    Q_INVOKABLE void removeAll();

    Q_INVOKABLE void startMission();
    Q_INVOKABLE void abortMission();
    Q_INVOKABLE void pauseMission();
    Q_INVOKABLE void resumeMission();
    /// Recalculates vehicle corridor, flight path, etc.
    Q_INVOKABLE bool updateMission();

    Q_INVOKABLE void saveToCurrent();
    Q_INVOKABLE void saveToFile(const QString& filename);
    Q_INVOKABLE bool loadFromCurrent();
    Q_INVOKABLE bool loadFromFile(const QString& filename);

    Q_INVOKABLE void resetAllInteractive(void);
    Q_INVOKABLE void setInteractive(void);

    QJsonDocument saveToJson(FileType fileType);

    // static Members
    static const char* wimaFileExtension;
    static const char* areaItemsName;
    static const char* missionItemsName;

signals:
    void masterControllerChanged        (void);
    void missionControllerChanged       (void);
    void visualItemsChanged             (void);
    void currentPolygonIndexChanged     (int index);
    void currentFileChanged             ();
    void dataContainerChanged           ();
    void missionReadyChanged            (bool ready);

private slots:
    void recalcPolygonInteractivity (int index);
    bool recalcJoinedArea           (QString &errorString);
    void pushToContainer            ();
    bool calcShortestPath      (const QGeoCoordinate &start, const QGeoCoordinate &destination, QList<QGeoCoordinate> &path);

private:
    // Member Functions
    WimaPlanData toPlanData();
    void         setMissionReady(bool ready);

    // Member Variables
    bool                    _missionReady; // basically true if updateMission() was sucessful
    PlanMasterController   *_masterController;
    MissionController      *_missionController;
    int                     _currentAreaIndex;
    QString                 _currentFile; // file for saveing
    WimaDataContainer      *_container; // container for data exchange with WimaController
    QmlObjectListModel      _visualItems; // contains all visible areas
    WimaJoinedArea          _joinedArea; // joined area fromed by _measurementArea, _serviceArea, _corridor
    WimaMeasurementArea     _measurementArea; // measurement area
    WimaServiceArea         _serviceArea; // area for supplying
    WimaCorridor            _corridor; // corridor connecting _measurementArea and _serviceArea
};
