#include "postprocessorsystem.h"

PostProcessorSystem::PostProcessorSystem(QObject *parent) :
    QObject(parent),
    hashComparer(nullptr),
    versionContainer(nullptr),
    updateController(nullptr),
    dataParserOutput(nullptr){}

void PostProcessorSystem::initialize(DataParserOutput *dataParserOutput,
                                     HashComparer *hashComparer,
                                     VersionContainer* versionContainer,
                                     UpdateController* updateController)
{
    this->hashComparer = hashComparer;
    this->versionContainer = versionContainer;
    this->updateController = updateController;
    this->dataParserOutput = dataParserOutput;

}

void PostProcessorSystem::socketDisable()
{
    emit sigSocketDisabled();
}

void PostProcessorSystem::serverBlocked()
{
    emit sigServerBlocked();
}

void PostProcessorSystem::startCompare()
{
    QList<FileData> *serverStreamingHash = new QList<FileData>;
    QList<FileData> *localStreamingHash = new QList<FileData>;

    QFile file(serverHash);
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    serverStreamingHash = dataParserOutput->xmlFileDataParse(file.readAll(),"StreamingAssets");
    file.close();

    QFile file2(streamingHashFilename);
    file2.open(QIODevice::ReadOnly | QIODevice::Text);

    localStreamingHash = dataParserOutput->xmlFileDataParse(file2.readAll(),"StreamingAssets");
    file2.close();

    emit sigStartCompare(serverStreamingHash,*localStreamingHash);
}

void PostProcessorSystem::compareFiles()
{
    updateController->updateFilesOnServer(hashComparer->getFilesForUpdate());
}

void PostProcessorSystem::checkAccessType(const QString& type)
{
    if(type == "instructor")
    {
        emit sigCallUpdateList();
    }
}

void PostProcessorSystem::saveLoginData(ServerAuthorization *auth)
{
    emit sigSaveLoginData(auth);
    dataParserOutput->createAuthData(auth);
}

void PostProcessorSystem::setServerVersion(StreamingVersionData *serverVersion)
{
    versionContainer->setServerVersionData(serverVersion);
}

void PostProcessorSystem::calculateCommonHash()
{
    updateController->calculateCommonHash();
}

void UpdateController::checkCanUpdate()
{
    QByteArray checkUpdate = dataParserOut->xmlAnswer_notify("CANCHANGE");  //TODO: ПЕРЕВЕСТИ НА PACKAGETYPE
    sendSystem->sendXMLAnswer(checkUpdate);
}
