#ifndef GAMERES_H
#define GAMERES_H

#include <QObject>
#include <QThread>
#include <LotroDat.h>
using namespace LOTRO_DAT;

class GameRes : public QObject {
Q_OBJECT

    // Singleton realisation of class
private:
    GameRes();
public:
    static GameRes* getInstance() {
        static GameRes instance;
        return &instance;
    }

    // Basic public functions
public slots:
    void openDatFile(int id);               // Opens .dat file by its id (0-local_English.dat)

    void closeDatFile(int id);              // Closes .dat file by its id

    void applyPatch(QString name);          // Applies patch by its name (sounds/texts/images/videos/textures/loadscreens)

    void setGameLocale(QString locale);     // Sets locale by its name (Original/RU)

    void startGame();                       // Starts detatched game application process

    void installMicroPatch();               // Installs latest micro-patch

    void applyAll();                        // Installs all patches (applyGlobal)

    bool isBusy() {return false;}                        // !!!!TO BE DONE Returns true if operations with game res are working now

    void checkDatFile();                    // Checks and returns information if dat file is clear/updated

    QStringList getLotroPath();             // Returns path to LotRO files

    void saveAllDatFiles();                 // Saves and closes all .dat files

signals:
    // Signals for every public function finish. Gives result int value.
    void openDatFileFinished(int result);
    void closeDatFileFinished(int result);
    void applyPatchFinished(int result);
    void setGameLocaleFinished(int result);
    void startGameFinished(int result);
    void installMicroPatchFinished(int result);
    void applyAllFinished(int result);
    void checkDatFileFinished(int result);

    // Signals for gui updating - describe process of patching.
    void startedPatching(QString patchname);
    void updatedPatchPercent(int percent);
    void finishedPatching(QString patchname, QString result);

private:
    int dateToTimestamp(QString customDateString, QString format);
    void processFile();
    void applyLoadscreens();
    bool isDatReady(); // Checks if file is ready to write data

    std::string getDatPath(int id);

private slots:
    void applyMicroPatch(QString filename);


private:
    QThread *thread_;
    bool busy_;
    Database db_;
    DatFile datfiles_[5];
    LOCALE locale_;
};

#endif // GAMERES_H
