#ifndef LOTROMANAGER_H
#define LOTROMANAGER_H

#include <QObject>
#include <QPushButton>

#include <queue>
#include <functional>

#include <LotroDat.h>

class LotroManager : public QObject
{
    Q_OBJECT
private:
    explicit LotroManager(QObject *parent = nullptr);

public:
    static LotroManager& getInstance() {
       static LotroManager lotro_mgr_instance;
       return lotro_mgr_instance;
    }

    bool execute(QString command, QString args, QString name);

    bool isBusy();

    QString patchTitleFromName(QString name);

private:

    bool startGame();

    bool openDatFile(int id);

    void applyPatch(QString name);

    void applyPatch(QStringList patches_list);

    bool correctPatchName(QString patch_name);

    void applyGlobal();

    bool isDatReady();

    int checkDatFile();

    void applyMicroPatch();

    void applyLoadscreens();

    bool setGameLocale(QString locale);

    void changeBaseLocale(QString locale);

    void saveDatFiles();

    void processFile();

    void installMicroPatch();

    void prepareMicroPatch();

private:
    bool busy_;

    qint64 last_second_files_count;
    qint64 last_second_time;
    qint64 start_time;
    QString elapsed_time;

    LOTRO_DAT::LOCALE dat_locale_;
    std::vector<LOTRO_DAT::DatFile> dat_files_;
    LOTRO_DAT::Database database_;

    const std::vector<QString> available_operations_ = {
        "StartGame",
        "SetLocale",
        "ChangeBaseLocale",

        "ApplyGlobal",
        "ApplyPatch",
        "ApplyPatchList",
        "ApplyMicroPatch",
        "ApplyLoadScreens",

        "CheckDatFile",
        "SaveDatFiles"
    };

signals:
    void dat_operation_started(QString command, QString args, QString name);
    void dat_operation_finished(QString command, QString args, QString name, QString result = "");
    void changePatchStatus(QString patch_name, QString new_status);
    void changeHint(QString head, QString hint);

private slots:
    void on_dat_operation_started(QString command, QString args, QString name);
    void on_dat_operation_finished(QString command, QString args, QString name, QString result = "");
};

#endif // LOTROMANAGER_H
