#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"

class NemointerfaceFactory;

class Nemointerface : public QObject {
  Q_OBJECT
  class Impl;
  using PImpl = std::unique_ptr<Impl>;

  Nemointerface(const QString &connectionString);
  Nemointerface(Nemointerface &other) = delete;

  friend class NemointerfaceFactory;

public:
  ~Nemointerface() override;

  enum class STATUS {
    NOT_CONNECTED,
    ERROR,
    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;
  QString connectionString();

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

private:
  PImpl pImpl;
};

// Singelton class used for nemo interface creation.
class NemointerfaceFactory {
  NemointerfaceFactory();
  NemointerfaceFactory(const NemointerfaceFactory &other) = delete;
  static NemointerfaceFactory *createInstance();

public:
  ~NemointerfaceFactory();
  static NemointerfaceFactory *instance();

  // Creates a interface with a connection string connectionString.
  // Two different interfaces can't share the same connection string.
  std::shared_ptr<Nemointerface> create(const QString &connectionString);
  // Returns true if an interface with connection string connectionString can
  // be created (i.e. doesn't exist yet), returns false either.
  bool isCreatable(const QString &connectionString);

private:
  std::map<QString, std::weak_ptr<Nemointerface>> _map;
};

#define pNemoInterfaceFactory NemointerfaceFactory::instance()
