#include "Core/dataparser.h"

#include "FileData.h"
#include "tools.h"

DataParser::DataParser(QObject *parent) :
    QObject(parent)
{

}

DataParser::~DataParser()
{

}

void DataParser::CreateXML(QList<FileData> fileDataList)
{

    QFile file(hashFilename);
    file.open(QIODevice::WriteOnly);
    QXmlStreamWriter xmlWriter(&file);

    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
    xmlWriter.writeStartElement("FileDataList");

    foreach (FileData data,fileDataList)
    {
        xmlWriter.writeStartElement("FileData");

        xmlWriter.writeAttribute("Path",data.path);
        xmlWriter.writeAttribute("Hash",data.hash);

        xmlWriter.writeEndElement();
    }

    xmlWriter.writeEndElement();
    xmlWriter.writeEndDocument();

    file.close();
}

void DataParser::CreateAuthMessage(ClientAutorization *auth)
{
    authPassCache = auth; //кэширование даных авторизации, для сохранения при успешном заходе

    QFile file(tempName);
    file.open(QIODevice::WriteOnly);
    QXmlStreamWriter xmlWriter(&file);

    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
    xmlWriter.writeStartElement("ClientAutorization");

    xmlWriter.writeAttribute("Login",auth->Login);
    xmlWriter.writeAttribute("Password",auth->Password);

    xmlWriter.writeEndElement();
    xmlWriter.writeEndElement();
    xmlWriter.writeEndDocument();

    file.close();
}



void DataParser::CreateServerSettings(QString address, QString port)
{
    QFile file(settingsName);

    file.open(QIODevice::WriteOnly);

    QXmlStreamWriter xmlWriter(&file);

    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();

    xmlWriter.writeStartElement("ServerSettingsContainer");
    xmlWriter.writeStartElement("ServerSettings");

    xmlWriter.writeAttribute("Address",address);
    xmlWriter.writeAttribute("Port",port);

    xmlWriter.writeEndElement();
    xmlWriter.writeEndElement();

    xmlWriter.writeEndDocument();

    file.close();

}

void DataParser::CreateAuthData(ServerAuthorization *serverAuth)
{
    QFile file(authTempName);
    file.open(QIODevice::WriteOnly);

    QXmlStreamWriter xmlWriter(&file);

    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
    xmlWriter.writeStartElement("AuthData");
    xmlWriter.writeAttribute("Login",authPassCache->Login);
    xmlWriter.writeAttribute("Password",authPassCache->Password);
    xmlWriter.writeAttribute("InstructorName",serverAuth->InstructorName);
    xmlWriter.writeAttribute("ClientName",serverAuth->ClientName);
    xmlWriter.writeAttribute("AccessType",serverAuth->AccessType);

    xmlWriter.writeEndElement();
    file.close();

}

ServerSettings *DataParser::GetServerSettings()
{
    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;
                    }

                }
            }
        }

        xmlReader.readNext();
    }

    return settings;
}

