#ifndef INTERNALLOGICMANAGER_H
#define INTERNALLOGICMANAGER_H

#include <QObject>
#include <QSettings>
#include <QThread>

#include "models/patchdownloader.h"
#include "models/lotrodatmanager.h"
#include "models/filesystem.h"

/*
Stage 1 - check if user opened application for the first time - then we need to show
          hello messages.

Stage 2 - check if lotro folder is correct (there's file LotroLauncher.exe in it).
          If no - then show him settings (ask to find lotro folder) where user can set it.

Stage 3 - check if there are client_local_English.dat and client_general.dat files.
          If no - then remove file and show message that we need to download them.

Stage 4 - try to open client_local_English.dat and client_general.dat. If at least one of them
          fails to open at all - show that there may be other processes taken ownage.

Stage 5 - if .dat files are opened, but there was an error telling that file has incorrect version -
          remove file and show message that we need to download them.

--- After this assuming that .dat files are correct and opened.

Stage 6 - if user runs for the first time, then ask to create backup

Stage 7 - check that user has chosen patches options. Otherwise move user to settings with enabled
          position of choosing (this skips stage 8 and goes to st.9)

Stage 8 - if user runs app for the first time - ask to download micropatches

Stage 9 - if there are flags that patches already applied but this is not true with .dat - ask to
          apply them once more

Stage 10 - All settings are checked and set. If user is for the first time - show message "ready to begin"
          Start check for updates. Start check for updates timer.
*/

class MainWindow;

class InternalLogicManager : public QObject
{
    Q_OBJECT
public:
    explicit InternalLogicManager(MainWindow *gui_, QSettings *settings_, QObject *parent = nullptr);
    ~InternalLogicManager();

signals:
    void initPipelineStarted();
    void initPipelineFinished();

    // Semi-critical sections signals. Example: patch files downloading
    void semiCriticalSectionEntered();
    void semiCriticalSectionLeaved();

    // Critical sections. Example: patch applying, game updating, prelauncher files moving
    void criticalSectionEntered();
    void criticalSectionLeaved();

//public slots:
//    void startInitPipeline();

//    // Actions from UI -> general window
//    void startGame(QString locale = "Original"); // Original/RU

//    // Actions from UI -> settings window
//    void lotroSettingsUpdated();
//    void createLotroFilesBackup();
//    void removeLotroFilesBackup();
//    void applySelectedPatches();
//    void updateSettingsChanged();
//    void terminateThreads();
//    void stopThreads();
//private slots:
//    void checkFirstTimeOpening();
//    void checkGameFolderCorrectness();
//    void checkDatFilesExist();
//    void processLotroManagerInit();
//    void processPatchDownloaderInit(); // There begins also checkForUpdates cycle;
//    void checkBackupExist();
//    void checkUserHasEnteredPatchOptions();
//    void checkDatFileWasPatched();

private:
    PatchDownloader *patch_downloader;
    QThread *patch_downloader_thread;

    LotroDatManager *lotro_manager;
    QThread *lotro_manager_thread;

    QSettings *settings;
    MainWindow *gui;
};

#endif // INTERNALLOGICMANAGER_H
