#include "Core/dataparser.h"

DataParser::DataParser(QObject *parent) :
    QObject(parent),
    postProcessSystem(nullptr)
{
    if(!QDir(fullStaticDataFolderName).exists()){
        QDir().mkdir(fullStaticDataFolderName);
    }
}

void DataParser::initialize(PostProcessorSystem *postProcessSystem)
{
    this->postProcessSystem = postProcessSystem;
}


ServerSettings *DataParser::getClientSettings()
{
    ServerSettings *settings = new ServerSettings;
    QFile file(settingsName);
    file.open(QIODevice::ReadOnly);
    QXmlStreamReader xmlReader(&file);

    while (!xmlReader.atEnd())
    {

        if(xmlReader.isStartElement()){

            if(xmlReader.name() == "ServerSettings")
            {

                foreach(const QXmlStreamAttribute &attr, xmlReader.attributes()){
                    QString name = attr.name().toString();
                    QString value = attr.value().toString();

                    if(name == "Address"){
                        settings->Address = value;
                    }

                    if(name == "Port"){
                        settings->Port = value;
                    }

                    if(name == "Language"){
                        settings->Language = value;
                    }

                    if(name == "AutoStart"){
                        settings->isAutoStart = value.toInt();
                    }

                    if(name == "UseMathModel"){
                        settings->mathModelUse = value.toInt();
                    }
                }
            }

            if (xmlReader.name() == "VersionData")
            {
                StreamingVersionData *data = new StreamingVersionData;

                foreach(const QXmlStreamAttribute &attr, xmlReader.attributes())
                {
                    QString name = attr.name().toString();
                    QString value = attr.value().toString();

                    if (name == "Version")
                    {
                        data->setName(value);
                        settings->LocalVersionName = value;
                    }

                    if (name == "isChangable")
                    {
                        data->setIsChangeable(value.toInt());
                    }

                    if(name == "Created")
                    {
                        data->setCreateData(QDateTime::fromString(value));
                    }


                    settings->versionData = *data;

                }
            }
        }

        xmlReader.readNext();
    }

    file.close();
    return settings;
}

bool DataParser::xmlParser(const QByteArray& array)
{
    QXmlStreamReader xmlReader(array);
    bool result = false;

    xmlReader.readNext();

    while(!xmlReader.atEnd())
    {
        if(!xmlReader.isStartElement()) {
            xmlReader.readNext();
            continue;
        }

        if(xmlReader.name() == "ServerNotify")
        {
            foreach(const QXmlStreamAttribute &attr, xmlReader.attributes())
            {
                QString name = attr.name().toString();
                QString value = attr.value().toString();

                if(name == "Code")
                {
                    if (value == "END")
                    {
                        postProcessSystem->socketDisable();
                    }

                    if (value == "BLOCKED")
                    {
                        postProcessSystem->serverBlocked();
                    }

                    if (value == "HASHSENDCOMPLETE")
                    {
                        postProcessSystem->startCompare();
                    }

                    if (value == "UNCHANGEABLE")
                    {
                        //версию нельзя изменять
                        result = true;
                    }

                    if (value == "CHANGEABLE")
                    {
                        postProcessSystem->compareFiles();
                        //emit sigUpdateFilesOnServer(hashComparer->getFilesForUpdate());
                    }
                }
            }
        }

        if(xmlReader.name() == "ServerAuthorization"){

            ServerAuthorization *serverAuth = new ServerAuthorization;

            foreach(const QXmlStreamAttribute &attr, xmlReader.attributes())
            {
                QString name = attr.name().toString();
                QString value = attr.value().toString();

                if (name == "Result")
                {
                    serverAuth->Result = value == "true" ? true : false;
                }

                if (name == "InstructorName")
                {
                    serverAuth->InstructorName = value;
                }

                if (name == "ClientName")
                {
                    serverAuth->ClientName = value;
                }

                if (name == "AccessType")
                {
                    serverAuth->AccessType = value;
                    postProcessSystem->checkAccessType(value);
                    //recognizeSystem->checkAccessType(value);
                }
            }

            postProcessSystem->saveLoginData(serverAuth);
            //emit recognizeSystem->sigSaveLoginData(serverAuth);
        }

        if(xmlReader.name() == "VersionData")
        {
            StreamingVersionData *serverVersion = new StreamingVersionData;
            foreach(const QXmlStreamAttribute &attr,xmlReader.attributes())
            {
                QString name = attr.name().toString();
                QString value = attr.value().toString();

                if (name == "Version")
                {
                    serverVersion->setName(value);
                }

                if (name == "Created")
                {
                    serverVersion->setCreateData(QDateTime::fromString(value));
                }

                if (name == "isChangeable")
                {
                    serverVersion->setIsChangeable(value.toInt());
                }

            }
            postProcessSystem->setServerVersion(serverVersion);
        }

        if(xmlReader.name() == "VersionList")
        {
            QList<StreamingVersionData*> *serverStreamingVersionDataList = new QList<StreamingVersionData*>;
            xmlReader.readNext();

            while (!xmlReader.atEnd())
            {
                if(xmlReader.isStartElement())
                {
                    if(xmlReader.name() == "VersionData")
                    {
                        StreamingVersionData *data = new StreamingVersionData;

                        foreach(const QXmlStreamAttribute &attr,xmlReader.attributes())
                        {
                            QString name = attr.name().toString();
                            QString value = attr.value().toString();

                            if (name == "Version")
                                data->setName(value);
                            else if(name == "Created")
                                data->setCreateData(QDateTime::fromString(value));
                            else if(name == "isChangeable")
                            {
                                data->setIsChangeable(value.toInt());
                            }
                        }

                        serverStreamingVersionDataList->append(data);
                    }
                }

                xmlReader.readNext();
            }
        }

        xmlReader.readNext();
    }
    return result;
}
