#ifndef PATCHLIST_H
#define PATCHLIST_H

#include <QObject>
#include <QTimer>
#include <QThread>
#include <QList>

#include "models/patch/patch.h"
#include "models/patch/textspatch.h"
#include "models/patch/graphicspatch.h"
#include "models/patch/soundspatch.h"
#include "models/patch/videospatch.h"

class PatchList : public QObject
{
    Q_OBJECT
public:
    explicit PatchList(LotroDatManager* mgr, QObject *parent = nullptr);

    QList<Patch *> getPatchList();

signals:
    void patchOperationsFinished();

    void patchOperationsStarted();

    void downloadTotalStatusChanged(Downloader::Status status);

    void installTotalStatusChanged(Patch::InstallationStatus status);

public slots:
    void startAutoUpdate();

    void update();

    void initialize();

private slots:
    void onPatchOperationStarted(QString operation_name, Patch* patch);

    void onPatchOperationFinished(QString operation_name, Patch* patch, bool result);

    void onPatchDownloadStatusChanged(Patch* patch, Downloader::Status status);

    void onPatchInstallStatusChanged(Patch* patch, Patch::InstallationStatus status);

    void onLotroManagerOperationFinished(QString operation_name, QVector<QVariant> args, bool result);

private:
    LotroDatManager *lotro_mgr_ = nullptr;
    bool lotro_mgr_initialised_ = false;

    TextsPatch *texts_patch_ = nullptr;
    GraphicsPatch *graphics_patch_ = nullptr;
    SoundsPatch *sounds_patch_ = nullptr;
    VideosPatch *videos_patch_ = nullptr;

    QThread *texts_patch_thread_;
    QThread *graphics_patch_thread_ = nullptr;
    QThread *sounds_patch_thread_ = nullptr;
    QThread *videos_patch_thread_ = nullptr;

    QMap<Patch*, Downloader::Status> patches_download_status_;
    QMap<Patch*, Patch::InstallationStatus> patches_installation_status_;

    quint32 active_operations_num_ = 0;
    bool auto_updates_enabled_ = false;

    QTimer patch_update_timer;    
};

#endif // PATCHLIST_H
