#include "sendsystem.h"
#include "tools.h"

#include <QFile>
#include <QFileInfo>
#include <QThread>



SendSystem::SendSystem(QObject *)
{

}

void SendSystem::initialize(MainWindow *mainWindow,DataParser *dataParser)
{
    this->mainWindow = mainWindow;
    connect(this,&SendSystem::sigSend,mainWindow,&MainWindow::updateProgress);
    connect(this,&SendSystem::sigGetXmlAnswer,dataParser,&DataParser::xmlAnswer_notify,Qt::DirectConnection);   //МОЖЕТ ДАТУ ПАРСЕР В ВТОРОСТЕПЕННЫЙ ПОТОК?
}

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

void SendSystem::xmlAnswer(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(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(1025*250);
            stream << data;
            socket->waitForBytesWritten();
            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::sendFolderBlock(QString path)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);

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

void SendSystem::sendQTConnect()
{
    QString value = QString::number(PacketType::TYPE_QT);
    socket->write(value.toUtf8());
    socket->waitForBytesWritten();
}

void SendSystem::sendXMLAnswer(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::sendChangeVersion(StreamingVersionData* streamingVersion)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    stream << PacketType::CHANGE_DATA_VERSION;
    stream << streamingVersion->getViewName();

    socket->waitForReadyRead(100);
}

void SendSystem::sendDeleteVersion(StreamingVersionData *streamingVersion)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    stream << PacketType::DELETE_DATA_VERSION;
    stream << streamingVersion->getViewName();

    socket->waitForReadyRead(100);
}

void SendSystem::sendCopyVersion(QString streamingVersion)
{
    QDataStream stream(socket);
    stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    stream << PacketType::COPY_VERSION;

    stream << streamingVersion;
}

SendSystem::~SendSystem()
{

}
