#ifndef LEGACYAPP_H
#define LEGACYAPP_H

#include <QObject>
#include <QSettings>
#include <QApplication>
#include <QVector>
#include <QVariant>

#include <LotroDat/LotroDat.h>

using LOTRO_DAT::DatStatus;

class LotroDatManagerObserver;

class LotroDatManager : public QObject
{
    Q_OBJECT

public:
    struct Status {
        DatStatus::DAT_STATUS status;
        double percent;
        QString dbg_message;

        Status()
            : status(DatStatus::DAT_STATUS::E_FREE)
            , percent(0)
            , dbg_message("")
        {
        }
    };

    friend bool operator !=(const Status& a, const Status& b) {
        return a.status != b.status || a.percent != b.percent;
    }

    enum Category : int {
        E_TEXTS_COMMON = 100,
        E_TEXTS_ITEMS = 101,
        E_TEXTS_EMOTES = 102,
        E_VIDEOS_PATHS = 103,
        E_MAPS_COMMON = 200,
        E_TEXTURES_COMMON = 201,
        E_AUDIOS_COMMON = 300
    };

public:
    explicit LotroDatManager(QObject *parent = nullptr);

    ~LotroDatManager();

    bool Initialised();

    bool NotPatched();

    Status getStatus();

    LotroDatManagerObserver *getObserver();

public slots:
    void initializeManager();

    void deinitializeManager();

    void startGame(LOTRO_DAT::DatLocaleManager::LOCALE locale);

    void installPatch(QString patch_name, QString database_path);

    void enableCategory(QString patch_name, Category category);

    void disableCategory(QString patch_name, Category category);

    void createBackup();

    void restoreFromBackup();

    void removeBackup();

private:
    bool startLotroLauncherWithParameters(LOTRO_DAT::DatLocaleManager::LOCALE locale);

signals:
    void operationStarted(QString operation_name, QVector<QVariant> args = {});
    void errorOccured(QString operation_name, QVector<QVariant> args = {}, QString message = "No error message provided");
    void operationFinished(QString operation_name, QVector<QVariant> args = {}, bool successful = true);

private:
    LotroDatManagerObserver* observer_;

    LOTRO_DAT::DatFile client_local_file_;
    LOTRO_DAT::DatFile client_general_file_;
};

#endif // LEGACYAPP_H
