#include "tcpclient.h"
#include "UpdateController.h"
#include "externalexecuter.h"

#include <QDir>

TCPClient::TCPClient(QObject *parent) :
    QObject(parent)
{
}

void TCPClient::initialize(MainWindow *mainWindow,RecognizeSystem *recognize,ExternalExecuter *externalExecuter,SendSystem *sendSystem)
{
    this->recognizeSystem = recognize;
    this->externalExecuter = externalExecuter;
    this->sendSystem = sendSystem;
    isConnected = false;

    connect(this,&TCPClient::sigConnectionState,mainWindow,&MainWindow::slotConnectionState,Qt::AutoConnection);
    connect(this,&TCPClient::sigServerDisconnect,mainWindow,&MainWindow::slotServerDisconnect);

    emit sigSendDebugLog(Tools::getTime() + " Client started");
}

void TCPClient::setConnect(ServerSettings *serverSettings)
{
    socket = new QTcpSocket();
    qDebug() << "TCPCLient thread: " << thread();
    if (socket != NULL && socket->state() == QTcpSocket::ConnectedState)
    {
        emit sigSendDebugLog("already connected");
        return;
    }


    socket->connectToHost(serverSettings->Address,serverSettings->Port.toShort());
    emit sigSendDebugLog("Try connect...");
    isConnected = true;

    if (socket->waitForConnected(2000))
    {
        connect(socket,&QTcpSocket::readyRead,this,&TCPClient::slotReadyRead,Qt::AutoConnection);
        connect(socket,&QTcpSocket::disconnected,this,&TCPClient::setDisconnect);

        sendSystem->setSocket(socket);
        slotConnectNotify();
    }
    else
    {
        isConnected = false;
        emit sigServerDisconnect();
    }
}


void TCPClient::setDisconnect()
{
    socket->disconnect();
    isConnected = false;
    emit sigServerDisconnect();
    emit sigSendDebugLog("Server disabled");
}


void TCPClient:: waitRead(int time)
{
    socket->waitForReadyRead(time);
}

QTcpSocket *TCPClient::getSocket()
{
    return socket;
}

void TCPClient::slotSendCommand(QString command)
{
    QDataStream stream(socket);
    QByteArray data;
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

    if(!command.isEmpty() && socket->state() == QTcpSocket::ConnectedState){

        if(command == "check")
        {
            stream << PacketType::TYPE_COMMAND;
            stream << command;
            socket->waitForBytesWritten();

            sendSystem->sendFileBlock(staticDataFolderName + hashFilename);
            emit sigSendDebugLog(Tools::getTime() + " Local checkFile sended");

            socket->waitForReadyRead(1000);
        }
        else if(command == "update"){
            emit sigSendDebugLog("Update started");
            stream << PacketType::TYPE_COMMAND;
            stream << command;
            socket->waitForReadyRead(1000);
        }
        else if(command == "run"){
            externalExecuter->callApp();
        }
    }else{
        emit sigSendDebugLog("WRONG SOCKET AFTER ENTERED");
    }
}

void TCPClient::slotConnectNotify()
{
    if(socket->state() != QTcpSocket::ConnectedState)
    {
        isConnected = false;
        emit sigSendDebugLog("Connect invalid");
        emit sigConnectionState(false);
        return;
    }
    else
    {
        isConnected = true;
        emit sigSendDebugLog("Connect complete");
        emit sigConnectionState(true);
        sendSystem->sendQTConnect();
    }
}

void TCPClient::slotReadyRead()
{
    if(!socket){
        emit sigSendDebugLog("WRONG SOCKET");
        return;
    }

    recognizeSystem->recognize(socket);
}

bool TCPClient::getIsConnected() const
{
    return isConnected;
}

TCPClient::~TCPClient()
{

}
