#pragma once

#include <graph/plugins/plugin.h>
#include <QThread>
#include <QAbstractListModel>
#include <string>

namespace Graph {

class Plugin : public QObject {
  Q_OBJECT

 private:
  plugin_t* plugin;
  const std::string path;
  const void* handle;

 public slots:
  void run();

 signals:
  void exited(int code);

 public:
  Plugin(const std::string& path, update_callback_t callback, int argc, char** argv);
  ~Plugin();

  const std::string& getPath() const { return this->path; }
};

class PluginModel : public QAbstractListModel {
  Q_OBJECT
 public:
  PluginModel(QObject* parent) : QAbstractListModel(parent) {}
  ~PluginModel();

  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
  QVariant data(const QModelIndex& index, int role) const override;

  void add(Plugin* plugin);
  void remove(int index);
  QList<Plugin*>& getPlugins() { return plugins; }

 private:
  QList<Plugin*> plugins;
};
} // namespace Graph
