#pragma once

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

#include "WimaArea.h"
#include "WimaMeasurementArea.h"
#include "WimaServiceArea.h"
#include "WimaCorridor.h"
#include "WimaDataContainer.h"
#include "WimaMeasurementAreaData.h"
#include "WimaCorridorData.h"
#include "WimaServiceAreaData.h"

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


class WimaController : public QObject
{
    Q_OBJECT

    enum FileType {WimaFile, PlanFile};

public:
    WimaController(QObject *parent = nullptr);


    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(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(WimaDataContainer*       dataContainer       READ dataContainer           WRITE setDataContainer         NOTIFY dataContainerChanged)
    Q_PROPERTY(QmlObjectListModel*      missionItems        READ missionItems                                           NOTIFY missionItemsChanged)


    // Property accessors
    PlanMasterController*       masterController       (void)          { return _masterController; }
    MissionController*          missionController      (void)          { return _missionController; }
    QmlObjectListModel*         visualItems            (void) ;
    QString                     currentFile            (void) const    { return _currentFile; }
    QStringList                 loadNameFilters        (void) const;
    QStringList                 saveNameFilters        (void) const;
    QString                     fileExtension          (void) const    { return wimaFileExtension; }
    QGCMapPolygon               joinedArea             (void) const;
    WimaDataContainer*          dataContainer          (void) const;
    QmlObjectListModel*         missionItems           (void);


    // Property setters
    void setMasterController        (PlanMasterController* masterController);
    void setMissionController       (MissionController* missionController);
    void setDataContainer           (WimaDataContainer* container);

    // Member Methodes
    Q_INVOKABLE void startMission();
    Q_INVOKABLE void abortMission();
    Q_INVOKABLE void pauseMission();
    Q_INVOKABLE void resumeMission();
    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);


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

    // Member Methodes
    QJsonDocument saveToJson(FileType fileType);

signals:
    void masterControllerChanged        (void);
    void missionControllerChanged       (void);
    void visualItemsChanged             (void);
    void currentFileChanged             ();
    void dataContainerChanged           ();
    void readyForSaveSendChanged        (bool ready);
    void missionItemsChanged            (void);

private slots:
    void containerDataValidChanged          (bool valid);

private:
    PlanMasterController   *_masterController;
    MissionController      *_missionController;
    QString                 _currentFile; // file for saveing
    WimaDataContainer      *_container; // container for data exchange with WimaController
    QmlObjectListModel      _visualItems; // contains all visible areas
    WimaJoinedAreaData      _joinedArea; // joined area fromed by opArea, serArea, _corridor
    WimaMeasurementAreaData _measurementArea; // measurement area
    WimaServiceAreaData     _serviceArea; // area for supplying
    WimaCorridorData        _corridor; // corridor connecting opArea and serArea
    bool                    _localPlanDataValid;
    QmlObjectListModel      _missionItems; // all mission itmes generaded by wimaPlaner, displayed in flightView
};

