#pragma once

#include <QTableWidget>
#include <QLibrary>
#include "../TradeCore/abstract_strategy.hh"
#include <memory>

using StrategyPtr = std::unique_ptr<AbstractStrategy>;

/**
 * @brief Used by ModuleManager for storing modules in vector
 * Movable because stategy is unique_ptr.
 */
struct Module {
    Module() = default;
    Module(Module&& module) = default;
    Module& operator=(Module&& module) = default;
    std::unique_ptr<QLibrary> library;
    QString name;
    QString description;
};

/**
 * @brief Loading and management of trade strategies
 * Trade strategy is a module in the form of dynamic library.
 */
class ModuleManager : public QTableWidget
{
    Q_OBJECT

public:
    explicit ModuleManager(QWidget* parent = nullptr);
    StrategyPtr getStrategy(const QString& name);
    void loadModuleList(const QString& fileName);

private:
    void loadModule(const QString& fileName);
    void contextMenuEvent(QContextMenuEvent* event);
    void saveModuleList(const QString& fileName, const QString& content);
    QString infoText(QLibrary& library, const char* symbol, bool& success);

private slots:
    void onAddNewModule();
    void onRemoveModule();

private:
    std::vector<Module> modules_;
    QString moduleListName_;
};
