#include "tcpclient.h"

#include <QDir>
#include <QThread>

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

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

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

void TCPClient::setConnect(ServerSettings *serverSettings)
{
    socket = new QTcpSocket();
    qDebug() << "TCPCLient thread: " << QThread::currentThreadId();
    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(5))
    {
        connect(socket,&QTcpSocket::readyRead,this,&TCPClient::slotReadyRead,Qt::DirectConnection);
        connect(socket,&QTcpSocket::disconnected,this,&TCPClient::setDisconnect);
        //connect(socket,&QTcpSocket::connected,this,&TCPClient::slotConnectNotify);

        connect(this,&TCPClient::sigRecognize,recognizeSystem,&RecognizeSystem::recognize,Qt::DirectConnection);
        connect(this,&TCPClient::sigSetSocket,sendSystem,&SendSystem::setSocket);

        emit sigSetSocket(socket);
        slotConnectNotify();

        emit signal_ConnectedToServer(true);
    }
    else
    {
        isConnected = false;
        emit signal_ConnectedToServer(false);
        emit sigServerDisconnect();
    }
}

void TCPClient::setDisconnect()
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    stream << PacketType::TYPE_DISABLE;

    socket->disconnect();

    isConnected = false;
    emit sigServerDisconnect();
    emit sigSendDebugLog("Server disabled");

    emit signal_ConnectedToServer(false);
}


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("/" + 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;
    }

    emit sigRecognize(socket);
}

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

TCPClient::~TCPClient()
{

}
