#pragma once

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

#include "WimaArea.h"
#include "WimaGOperationArea.h"
#include "WimaServiceArea.h"
#include "WimaVCorridor.h"

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


class WimaController : public QObject
{
    Q_OBJECT
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(int                      currentPolygonIndex     READ currentPolygonIndex        WRITE setCurrentPolygonIndex        NOTIFY currentPolygonIndexChanged)


    // Property accessors
    PlanMasterController*       masterController       (void) const    { return _masterController;}
    MissionController*          missionController      (void) const    { return _missionController;}
    QmlObjectListModel*         visualItems            (void)          { return _visualItems; }
    int                         currentPolygonIndex    (void) const    { return _currentPolygonIndex; }




    // 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);

    Q_INVOKABLE void addGOperationArea();
    /// Removes an area from _visualItems
    /// @param index Index of the area to be removed
    Q_INVOKABLE void removeArea(int index);
    Q_INVOKABLE void addServiceArea();
    /// @return true if a vehicle corridor was added sucessfully and false otherwise.
                bool addVehicleCorridor(WimaGOperationArea* opArea, WimaServiceArea* serviceArea);

    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 saveMission();
    Q_INVOKABLE void loadMission();

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



signals:
    void masterControllerChanged        (void);
    void missionControllerChanged       (void);
    void visualItemsChanged             (void);
    void currentPolygonIndexChanged     (int index);

private slots:
    void recalcVehicleCorridor();
    void recalcVehicleMeasurementAreas();
    void recalcAll();
    void recalcPolygonInteractivity(int index);


private:
    bool                        _planView;
    QmlObjectListModel*         _visualItems;
    PlanMasterController*       _masterController;
    MissionController*          _missionController;
    int                         _currentPolygonIndex;

};
