#include "sendsystem.h"

SendSystem::SendSystem(QObject *parent) :
    QObject(parent),
    socket(nullptr),
    readSize(1025*250){}

void SendSystem::setSocket(QTcpSocket *socket)
{
    this->socket = socket;
}

void SendSystem::xmlAnswer(const QString& message)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

    QByteArray data;
    data = emit sigGetXmlAnswer(message);

    stream << PacketType::TYPE_XMLANSWER;
    stream << data;

    qDebug() << "Send answer " + message;
    socket->waitForBytesWritten();
}

void SendSystem::sendClientAutorization()
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

    QFile file(tempName);
    file.open(QIODevice::ReadOnly);

    QByteArray array = file.readAll();

    stream << PacketType::TYPE_XMLANSWER;
    stream << array;
    socket->waitForBytesWritten();

    qDebug() << "Send client auth";
    file.close();
}

void SendSystem::sendFileBlock(const QString& path)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    QString fullPath = Tools::createSendFullPath(path);
    quint64 fileSize = 0;
    int countSend = 0;


    QFile file(fullPath);       //Открываем файл для чтения
    QFileInfo fileInfo(file);

    fileSize = fileInfo.size();

    stream << PacketType::TYPE_FILE; //Отправляем тип блока
    stream << path << fileSize;

    socket->waitForReadyRead(20);
    //socket->waitForBytesWritten();

    if(file.open(QFile::ReadOnly)){
        while(!file.atEnd()){
            QByteArray data = file.read(readSize);
            stream << data;
            socket->waitForBytesWritten(20);
            countSend++;
        }

        qDebug() << Tools::getTime() << "count end Final: " << countSend;
    }

    file.close();

    emit sigSend();
    countSend = 0;
    socket->waitForReadyRead(20);
}

void SendSystem::sendFolderBlock(const QString& path)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

    stream << PacketType::TYPE_FOLDER;
    stream << path;
    emit sigSend();
    socket->waitForReadyRead(100);
}

void SendSystem::sendFileBlockWithVersion(const QString& localPath,const QString& serverPath)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    quint64 fileSize = 0;
    int countSend = 0;


    QFile file(localPath);       //Открываем файл для чтения
    QFileInfo fileInfo(file);

    fileSize = fileInfo.size();

    stream << PacketType::TYPE_FILE; //Отправляем тип блока
    stream << serverPath << fileSize;

    socket->waitForReadyRead(20);

    if(file.open(QFile::ReadOnly)){
        while(!file.atEnd()){
            QByteArray data = file.read(readSize);
            stream << data;
            socket->waitForBytesWritten(10);
            countSend++;
        }

        qDebug() << Tools::getTime() << "count end Final: " << countSend;
    }

    file.close();

    emit sigSend();
    //qDebug() << "Transaction after send file: " << socket->isTransactionStarted();
    countSend = 0;
    //socket->waitForBytesWritten();
    socket->waitForReadyRead(20);
}

void SendSystem::sendQTConnect()
{
    QByteArray container;   //TODO найти более компактное решение
    int numPackage = (int)PacketType::TYPE_QT;
    container.append(numPackage & 0x000000ff);
    container.append((numPackage >> 8) & 0x000000ff);
    container.append((numPackage >> 16) & 0x000000ff);
    container.append((numPackage >> 24) & 0x000000ff);

    socket->write(container);
    socket->waitForBytesWritten();
}

void SendSystem::sendXMLAnswer(const QByteArray& array)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

    stream << PacketType::TYPE_XMLANSWER;
    stream << array;

    socket->waitForBytesWritten();
    socket->waitForReadyRead(100);

    qDebug() << "Send XML answer in byte";
}

void SendSystem::sendFinish()
{
    socket->waitForReadyRead(100);
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

    stream << PacketType::TYPE_FINISH;
    socket->waitForReadyRead(100);
}

void SendSystem::sendPacketType(PacketType packetType)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

    stream << packetType;
}

void SendSystem::sendCheckHash()
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

    sendFileBlock(staticDataFolderName + hashFilename);

    socket->waitForReadyRead(2000);
    stream << PacketType::TYPE_CHECK_VERSION;
}
