#pragma once

#include <QGeoCoordinate>
#include <QObject>
#include <QVariant>

#include <future>
#include <memory>

#include "IDArray.h"
#include "LogicalArray.h"
#include "TileArray.h"
#include "TilePtrArray.h"
#include "geometry/ProgressArray.h"

// Singelton class used to interface measurement devices implementing the nemo
// interface.
class NemoInterface : public QObject {
  Q_OBJECT
  class Impl;
  using PImpl = std::unique_ptr<Impl>;

  NemoInterface();
  NemoInterface(NemoInterface &other) = delete;
  static NemoInterface *createInstance();

public:
  ~NemoInterface() override;
  static NemoInterface *instance();

  enum class STATUS {
    NOT_CONNECTED,
    SYNC,
    READY,
    WEBSOCKET_DETECTED,
    TIMEOUT,
  };
  Q_ENUM(STATUS)

  Q_PROPERTY(STATUS status READ status NOTIFY statusChanged)
  Q_PROPERTY(QString statusString READ statusString NOTIFY statusChanged)
  Q_PROPERTY(QString infoString READ infoString NOTIFY infoStringChanged)
  Q_PROPERTY(
      QString warningString READ warningString NOTIFY warningStringChanged)
  Q_PROPERTY(QString editorQml READ editorQml CONSTANT)
  Q_PROPERTY(bool running READ running NOTIFY runningChanged)

  QString editorQml();

  Q_INVOKABLE void start();
  Q_INVOKABLE void stop();

  // Tile editing.
  //!
  //! \brief addTiles
  //! \param tileArray
  //! \return Returns a QVariant containing a boolean value, indicating if the
  //! requested operation was successfull.
  //!
  std::shared_future<QVariant> addTiles(const TileArray &tileArray);
  std::shared_future<QVariant> addTiles(const TilePtrArray &tileArray);
  //!
  //! \brief removeTiles
  //! \param idArray
  //! \return Returns a QVariant containing a boolean value, indicating if the
  //! requested operation was successfull.
  //!
  std::shared_future<QVariant> removeTiles(const IDArray &idArray);
  //!
  //! \brief clearTiles
  //! \return Returns a QVariant containing a boolean value, indicating if the
  //! requested operation was successfull.
  //!
  std::shared_future<QVariant> clearTiles();

  TileArray getTiles(const IDArray &idArray) const;
  TileArray getAllTiles() const;
  LogicalArray containsTiles(const IDArray &idArray) const;
  std::size_t size() const;
  bool empty() const;

  // Progress.
  ProgressArray getProgress() const;
  ProgressArray getProgress(const IDArray &idArray) const;

  // Status.
  STATUS status() const;
  QString statusString() const;
  QString infoString() const;
  QString warningString() const;
  bool running() const;

signals:
  void statusChanged();
  void infoStringChanged();
  void warningStringChanged();
  void progressChanged(const ProgressArray &progressArray);
  void tilesChanged();
  void runningChanged();

private:
  PImpl pImpl;
};

#define pNemoInterface NemoInterface::instance()
