#include "connectortoserver.h"
#include <QThread>

ConnectorToServer::ConnectorToServer(QWidget* parentWidget, QObject *parent) :
    QObject(parent),
    parentWidget(parentWidget),
    connectionThread(nullptr),
    client(nullptr),
    dataParser(nullptr),
    sendSystem(nullptr),
    recognizeSystem(nullptr),
    versionSelectWidget(nullptr),
    versionContainer(nullptr),
    notifyController(nullptr),
    fl_GetedOfflineMessages(false)
{
    initialize();
}

ConnectorToServer::~ConnectorToServer()
{
    delete client;
    delete versionSelectWidget;
    delete notifyController;
    delete versionContainer;
    delete recognizeSystem;
    delete sendSystem;
    delete dataParser;

    connectionThread->quit();
    connectionThread->wait();
    delete connectionThread;
}

ServerSettings ConnectorToServer::getServerSettings()
{
    return  serverSettings;
}

bool ConnectorToServer::getIsConnected()
{
    return client->getIsConnected();
}

void ConnectorToServer::SetConnectToServer()
{
    qDebug() << "connectorToServer::SetConnectToServer() thread ID " << QThread::currentThreadId();
    serverSettings = *dataParser->getServerSettings();
    emit sigSetConnect(dataParser->getServerSettings(),connectionThread);
}

void ConnectorToServer::StopConnectToServer()
{
    emit sigStopConnect();
    clearListModelDB();
}

void ConnectorToServer::setLoginName(QString name)
{
    versionSelectWidget->setAuthor(name);
}

void ConnectorToServer::slot_sendMessage(ClientMessage clientMessage)
{
     sendMessage(clientMessage);
}

void ConnectorToServer::slot_showServerList(QList<StreamingVersionData *> *serverList)
{
    versionSelectWidget->fillView(serverList);
}

void ConnectorToServer::slot_HashReady()
{

}

void ConnectorToServer::slot_Auth(ServerAuthorization *serverAuth)
{
    this->serverAuth = *serverAuth;
    emit sigLoginResult(serverAuth);
}

void ConnectorToServer::initialize()
{
    createObjects();

    bindConnection();

    emit sigInitializeClient(recognizeSystem,sendSystem,connectionThread);

    //SetConnectToServer();
}

void ConnectorToServer::bindConnection()
{
    connect(this,&ConnectorToServer::sigInitializeClient,client,&TCPClient::initialize,Qt::AutoConnection);
    connect(this,&ConnectorToServer::sigSetConnect,client,&TCPClient::setConnect,Qt::AutoConnection);
    connect(this,&ConnectorToServer::sigStopConnect,client,&TCPClient::setDisconnect,Qt::AutoConnection);

    connect(this,&ConnectorToServer::sigSendAnswerToServer,sendSystem,&SendSystem::sendXMLAnswer,Qt::AutoConnection);

    connect(recognizeSystem,&RecognizeSystem::sigAuth,this,&ConnectorToServer::slot_Auth);
    connect(recognizeSystem,&RecognizeSystem::sigDeAuth,this,&ConnectorToServer::sigDeLoginResult);
    connect(recognizeSystem,&RecognizeSystem::signal_ReceiveMessage,this,&ConnectorToServer::signal_receiveMessage,Qt::AutoConnection);
    connect(recognizeSystem,&RecognizeSystem::sigShowServerDataList,this,&ConnectorToServer::slot_showServerList);
    connect (recognizeSystem,&RecognizeSystem::sigSetVersion,versionContainer,&VersionContainer::setServerVersionData);

    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryToDB_ListInstructors,this,&ConnectorToServer::slot_AnswerQueryToDB_ListInstructors);
    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryToDB_ListGroups,this,&ConnectorToServer::slot_AnswerQueryToDB_ListGroups);
    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryToDB_ListTrainees,this,&ConnectorToServer::slot_AnswerQueryToDB_ListTrainees);
    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryToDB_ListComputers,this,&ConnectorToServer::slot_AnswerQueryToDB_ListComputers);
    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryToDB_ListClassrooms,this,&ConnectorToServer::slot_AnswerQueryToDB_ListClassrooms);

    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryToDB_ListTasksAMMforTrainee,this,&ConnectorToServer::slot_AnswerQueryToDB_ListTasksAMMforTrainee);
    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryToDB_ListTasksFIMforTrainee,this,&ConnectorToServer::slot_AnswerQueryToDB_ListTasksFIMforTrainee);

    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryTasksXML_FIM,this,&ConnectorToServer::slot_AnswerQueryTasksXML_FIM);
    connect(recognizeSystem,&RecognizeSystem::sigAnswerQueryTasksXML_AMM,this,&ConnectorToServer::slot_AnswerQueryTasksXML_AMM);

    connect(recognizeSystem,&RecognizeSystem::sigAnswerQuerySubProc,this,&ConnectorToServer::slot_AnswerQuerySubProc);

    connect(client,&TCPClient::signal_ConnectedToServer,this,&ConnectorToServer::signal_ConnectedToServer,Qt::AutoConnection);

    connect(recognizeSystem,&RecognizeSystem::sigNotify,notifyController,&NotifyController::showWarning,Qt::AutoConnection);

    connect(recognizeSystem,&RecognizeSystem::sigHashReady,this,&ConnectorToServer::slot_HashReady);

    connect(recognizeSystem,&RecognizeSystem::signal_AnswerDocsChanged,this,&ConnectorToServer::slot_AnswerDocsChanged);
}

void ConnectorToServer::createObjects()
{
    connectionThread = new QThread;

    client = new TCPClient;
    client->moveToThread(connectionThread);

    dataParser = new DataParser;    

    sendSystem = new SendSystem;
    sendSystem->moveToThread(connectionThread);

    recognizeSystem = new RecognizeSystem;
    recognizeSystem->moveToThread(connectionThread);

    notifyController = new NotifyController;
    versionContainer = new VersionContainer;
    versionSelectWidget = new VersionSelectWidget(parentWidget);
    versionSelectWidget->initialize(sendSystem,versionContainer,notifyController, recognizeSystem);

    connectionThread->start();
    connectionThread->setPriority(QThread::HighestPriority);
}

SendSystem *ConnectorToServer::getSendSystem() const
{
    return sendSystem;
}
