#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#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 "Datas.h"
#include "commonbuttongroupwidget.h"
#include "entrywidget.h"
#include "instructorbuttongroupwidget.h"
#include "mywinheader.h"
#include "updatenotifywidget.h"
#include "versionselectwidget.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class TCPClient;
class UpdateController;
class RecognizeSystem;
class ExternalExecuter;
class UpdateNotifyWidget;
class HashComparer;
class CommonButtonGroupWidget;
class InstructorButtonGroupWidget;
class VersionSelectWidget;
class EntryWidget;
class ScreenChecker;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    void callUpdateList();
    void checkUpdate();
    ~MainWindow();

    void bindConnection();

signals:
    void sigInitializeClient(MainWindow* mainWindow,
                             RecognizeSystem *recognizeSystem,
                            ExternalExecuter *externalExecuter,
                            SendSystem *sendSystem,
                            QThread *thread);
    void sigRecognize(UpdateController* updateController,
                      DataParser* dataParser,
                      MainWindow* mainWindow,
                      HashComparer* hashComparer,
                      TCPClient *tcpClient);

    void sigSendCommand(QString command);
    void sigSendXMLAnswer(QString answer);
    void sigUpdateFilesOnServer(QList<FileData> *fileSendList);
    void sigSetConnect(ServerSettings* serverSettings,QThread *thread);
    void sigCalculateHash();
    void sigSendAutorization();
    bool sigGetConnected();


public slots:
    void showUpdateInfo();
    void showCompleteDialogBox();
    void slotConnectionState(bool flag);
    void slotServerDisconnect();
    void updateProgress();
    void loadComplete();
    void lostConnection();
    void serverBlocked();
    void checkLoginResult(ServerAuthorization * serverAuth);
    void setNeedUpdate(bool flag,quint64 size,quint64 fileCount);
    void showServerListWidget(QList<StreamingVersionData*> *serverData);


private slots:
    void on_settingsButton_clicked();
    void on_languageComboBox_activated(const QString &arg1);
    void slotDisableNotify();
    void on_updateListGuideLabel_linkActivated(const QString &link);
    void on_exitButton_clicked();
    void on_offlineStartButton_clicked();


private:
    Ui::MainWindow *ui;
    MyWinHeader *header;
    UpdateNotifyWidget *updateWidget;
    CommonButtonGroupWidget *commonButtonGroupWidget;
    InstructorButtonGroupWidget *instructorButtonGroupWidget;
    EntryWidget *entryWidget;
    VersionSelectWidget *versionSelectWidget;

    QTranslator translator;
    TCPClient *client;
    DataParser *dataParser;
    UpdateController *updateController;
    RecognizeSystem *recognizeSystem;
    ScreenChecker *screenChecker;
    ExternalExecuter *externalExecuter;
    SendSystem *sendSystem;
    HashComparer *hashComparer;
    QThread *workerThread;
    QThread *animationThread;
    QTimer *timer;
    QMovie *movie;
    QLabel *movieLabel;
    QList<FileData> *updateList;

    int fileCountForUpdate;
    int filesLoaded;
    bool isRecovery;


    void painting();
    void checkAppAvailable();
    void checkLanguage(QString language);
    void autoStart();
    void loadStaticData();
    void bindClient();
    void createObjects();
    void startLoadingAnim();
    void stopLoadingMovie();
    void showConnectionEmpty();

public:
    void initialize();
    void login();
    void saveServerSettingsWithConnect();
    void loadToServer();
    void undoCurrentChanges();
    void startUnityClient();
    void setCurrentVersionName(QString versionName);

protected:
    virtual void keyPressEvent(QKeyEvent *event);
};
#endif // MAINWINDOW_H
