#ifndef COREMANAGER_H
#define COREMANAGER_H

#include <QObject>
#include <QTranslator>

#include <QThread>
#include <QTranslator>
#include <Core/dataparser.h>
#include <Core/tcpclient.h>
#include <Core/screenchecker.h>
#include <Core/UpdateController.h>
#include <Core/sendsystem.h>
#include <Core/hashcomparer.h>
#include <Core/versioncontainer.h>
#include <widgetmanager.h>

#include <UI/resourcemanager.h>

class CoreManager : public QObject
{
    Q_OBJECT
public:
    explicit CoreManager(QObject *parent = nullptr);
    ~CoreManager(){}
    void initialize(WidgetManager *widgetManager,QThread *workerThread);
    void start();
    void checkUpdateInfo();
    void checkAutoStart();
    void setLanguage(const QString &language);

    VersionContainer *getVersionContainer() const;
    SendSystem *getSendSystem() const;
    ResourceManager *getResourceManager() const;
    RecognizeSystem *getRecognizeSystem() const;

    void loadToServer();
    void undoCurrentChanges();
    void startUnityClient();
    void tryLogin();
    void startOffline();

    void saveServerSettingsWithConnect();


    void checkAppAvailable();
    void exit();
    void initializeWidgets();

    void recalculateState();
signals:
    void sigInitializeClient(RecognizeSystem *recognize,SendSystem *sendSystem);
    void sigSendCommand(PacketType packetType);
    void sigSendXMLAnswer(QString answer);
    void sigSetConnect(ServerSettings* serverSettings);
    void sigCalculateHash();
    void sigSendAutorization();
    void sigSendCheckUpdate();
    bool sigGetConnected();
    void sigSendUpdateToServer();

    void sigSetLoadSettings(ServerSettings *serverSettings);

private:
    QTranslator translator;
    TCPClient *client;
    DataParser *dataParser;
    DataParserOutput *dataParserOutput;
    UpdateController *updateController;
    RecognizeSystem *recognizeSystem;
    ScreenChecker *screenChecker;
    ExternalExecuter *externalExecuter;
    SendSystem *sendSystem;
    HashComparer *hashComparer;
    VersionContainer *versionContainer;
    ResourceManager *resourceManager;
    PostProcessorSystem *postProcessorSystem;

    WidgetManager *widgetManager;

    QThread *workerThread;

    quint16 fileCountForUpdate;
    quint16 filesLoaded;
    bool isRecovery;
    bool autostart;


    void loadStaticData();
    void binding();
    void initializeSystems();
    void callUpdateList();
    void lostConnection();
    void serverBlocked();
    void checkLoginResult(ServerAuthorization *auth);
    void checkAccessType(const QString &accessType);
    void setServerVersion(StreamingVersionData *version);
    void calcUpdateProgress();
    void loadComplete();
    void setLocalVersion();
    void checkNeedUpdate(bool flag, quint64 size, quint64 fileCount, quint64 deleteCount);
    void checkUpdate();

};

#endif // COREMANAGER_H
