#pragma once

#include "GenericWaypointManager.h"
#include "Settings.h"
#include "AreaInterface.h"

namespace WaypointManager {

typedef GenericWaypointManager<QGeoCoordinate,
                               QVector,
                               QmlObjectListModel,
                               Settings>
        ManagerBase;
//!
//! \brief The RTLManager class is used to manage the return to launch of the vehicle.
//!
//! @note The \class QmlObjectList returned by \fn missionItems doesn't contain a MissionSettingsItem.
//! This list is supposed to be used for display purposes only.
class RTLManager : public ManagerBase
{
public:
    RTLManager() = delete;
    RTLManager(Settings &settings,
               AreaInterface &interface);

    //!
    //! \brief Does nothing.
    //!
    virtual void            setWaypoints(const QVector<QGeoCoordinate> &waypoints) override;
    //!
    //! \brief Does nothing.
    //!
    virtual void            push_back   (const QGeoCoordinate &wp) override;
    //!
    //! \brief Does nothing.
    //!
    virtual void            push_front  (const QGeoCoordinate &wp) override;
    //!
    //! \brief Clears the waypoint manager.
    virtual void            clear       () override;
    //!
    //! \brief Does nothing.
    //!
    virtual void            insert      (std::size_t i, const QGeoCoordinate &wp) override;
    //!
    //! \brief Returns zero.
    //! \return Returns zero.
    virtual std::size_t     size        () const override;
    //!
    //! \brief Returns an empty coordinate.
    virtual void            remove      (std::size_t i) override;


    //!
    //! \brief Updates the waypoint manager.
    //!
    //! After updateing a call to currentMissionItems or currentWaypoints returns
    //! a path from the vehicles current position to the home position.
    //!
    //! \return Returns true on success, false either.
    //!
    virtual bool update() override;
    //!
    //! \brief Does nothing.
    //! \return Returns true.
    //!
    virtual bool next() override;
    //!
    //! \brief Does nothing.
    //! \return Returns true.
    //!
    virtual bool previous() override;
    //!
    //! \brief Does nothing.
    //! \return Returns true.
    //!
    virtual bool reset() override;

    bool checkPrecondition(QString &errorString);

protected:
    bool _insertMissionItem(const QGeoCoordinate &c,
                            size_t index,
                            QmlObjectListModel &list,
                            bool doUpdate);
    bool _insertMissionItem(const QGeoCoordinate &c,
                            size_t index,
                            bool doUpdate);
    bool _calcShortestPath(const QGeoCoordinate &start,
                           const QGeoCoordinate &destination,
                           QVector<QGeoCoordinate> &path);

    AreaInterface *_areaInterface;
private:
    bool _worker();

};

} // namespace WaypointManager
