#ifndef SERVERLMSWIDGET_H
#define SERVERLMSWIDGET_H

#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QXmlStreamWriter>
#include <QMap>
#include <QTranslator>
#include <Systems/dataparser.h>
#include <Systems/sendsystem.h>
#include <Systems/processingsystem.h>
#include <Systems/updatecontroller.h>
#include <Systems/recognizesystem.h>
#include <Systems/logger.h>
#include <QMutex>

#include "ServerLMS_global.h"
#include "typesDataServerClient.h"
#include "Client.h"
//#include "instructorsAndTrainees.h"
#include "instructorsandtraineeswidget.h"
#include "messangerwidget.h"
#include "multithreadserver.h"

namespace Ui {
class ServerLMSWidget;
}

class DataParser;
class SendSystem;
class ProcessingSystem;
class Logger;
class UpdateController;
class RecognizeSystem;
class ClientHandler;
class MultiThreadServer;

class SERVERLMS_EXPORT ServerLMSWidget : public QWidget
{
    Q_OBJECT

public:
    explicit ServerLMSWidget(InstructorsAndTraineesWidget* instructorsAndTrainees, MessangerWidget* messangerWidget, QWidget *parent = nullptr);
    void autorizationHandler(QString login);
    ~ServerLMSWidget();

protected:
    // Метод получения событий
    // В нём будет производиться проверка события смены перевода приложения
    void changeEvent(QEvent * event) override;

signals:
    //void signal_authorizationChanged();
    void sigRecognize();
    QTcpSocket* sigGetSocket();
    void sigLog(QString log);


public slots:
    void slot_LanguageChanged(QString language);
    void addClient(qintptr descriptor, ClientHandler *client);
    void slotUpdateListClients();
    void slot_BlockAutorization(bool block);

private slots:
    //слот обработки сигнала о готовности нового сообщения на отправку клиенту от мессенджера
    void slot_msgToClientReady(QString login, QString text);
    void slotAddToLog(QString msg);
    void slotAddToMessanger(QString login, QString text);

public:

    void removeClient(int socketId);
    void blockAutorization()
    {
        stateBlockAutorization = blocked;
    }
    void unBlockAutorization()
    {
        stateBlockAutorization = unblocked;
    }

    EStateBlockAutorization getStateBlockAutorization() const
    {
        return stateBlockAutorization;
    }

    QMap<int, ClientHandler *> getClientsMap() const;

private slots:
    void on_btnStartServer_clicked();
    void on_btnStopServer_clicked();
    void on_btnTaskSet_clicked();


private:
    bool startServer();
    bool stopServer();

private:
    MultiThreadServer * tcpServer;
    qint16 hostPort;
    QMap<int, ClientHandler*> clientsMap;
    EStateServer stateServer;
    EStateBlockAutorization stateBlockAutorization;

    InstructorsAndTraineesWidget* pInstructorsAndTrainees;
    MessangerWidget* pMessangerWidget;

    QThread *updateThread;
    QThread *loggerThread;

    DataParser *dataParser;
    ProcessingSystem *processingSystem;
    UpdateController *updateController;
    Logger *logger;

    bool first = true; // для тестов Unity

private:
    Ui::ServerLMSWidget *ui;
    QTranslator qtLanguageTranslator;

};

#endif // SERVERLMSWIDGET_H
