#include "clientanswerparser.h"

ClientAnswerParser::ClientAnswerParser(QObject *parent) : QObject(parent)
{

}

void ClientAnswerParser::initialize(DataParser *dataParser)
{
    this->dataParser = dataParser;
}

QByteArray ClientAnswerParser::authorization(bool result, QString instructorName,QString clientName, QString accessType, QString login, int id)
{

    QList<SXmlAnswerTag> listTag;

    SAttribute attribute1 = {"Result", result? "true" : "false"};
    SAttribute attribute2 = {"InstructorName", instructorName};
    SAttribute attribute3 = {"ClientName", clientName};
    SAttribute attribute4 = {"AccessType", accessType};
    SAttribute attribute5 = {"Login", login};
    SAttribute attribute6 = {"id_client", QString::number(id)};
    QList<SAttribute> listAttr = {attribute1, attribute2, attribute3, attribute4, attribute5, attribute6};
    SXmlAnswerTag tag = {"ServerAuthorization", listAttr};

    listTag.append(tag);

    return dataParser->xmlAnswer(listTag);
}

QByteArray ClientAnswerParser::deAuthorization(bool result, QString login)
{
    QList<SXmlAnswerTag> listTag;

    SAttribute attribute1 = {"Result", result? "true" : "false"};
    SAttribute attribute2 = {"Login", login};
    QList<SAttribute> listAttr = {attribute1, attribute2};
    SXmlAnswerTag tag = {"ServerDeAuthorization", listAttr};

    listTag.append(tag);

    return dataParser->xmlAnswer(listTag);
}

QByteArray ClientAnswerParser::message(QString loginFrom,QString loginTo,QString text)
{
    QList<SXmlAnswerTag> listTag;
    QList<SAttribute> listAttr;
    SAttribute attribute1;
    attribute1 = {"From",loginFrom};
    listAttr.append(attribute1);
    attribute1 = {"To",loginTo};
    listAttr.append(attribute1);
    attribute1 = {"Text",text};
    listAttr.append(attribute1);

    SXmlAnswerTag tag = {"ClientMessage", listAttr};

    listTag.append(tag);

    return dataParser->xmlAnswer(listTag);
}

QByteArray ClientAnswerParser::task(QString text)
{
    QList<SXmlAnswerTag> listTag;

    SAttribute attribute1 = {"Text", text};
    QList<SAttribute> listAttr = {attribute1};
    SXmlAnswerTag tag = {"ServerTask", listAttr};

    listTag.append(tag);

    return dataParser->xmlAnswer(listTag);
}

QByteArray ClientAnswerParser::notify(QString code)
{
    QList<SXmlAnswerTag> listTag;

    SAttribute attribute1 = {"Code", code};
    QList<SAttribute> listAttr = {attribute1};
    SXmlAnswerTag tag = {"ServerNotify", listAttr};

    listTag.append(tag);

    return dataParser->xmlAnswer(listTag);
}

QByteArray ClientAnswerParser::tasks(QStringList listTasks)
{
    QList<SXmlAnswerTag> listTag;

    foreach(QString task, listTasks)
    {
        QList<SAttribute> listAttr;

        SAttribute attribute1 = {"Head", task};
        SAttribute attribute2 = {"IsComplete", "false"};
        listAttr.append(attribute1);
        listAttr.append(attribute2);

        SXmlAnswerTag tag = {"ServerTask", listAttr};
        listTag.append(tag);
    }

    return dataParser->xmlAnswer(listTag, "TaskArray", "Tasks");
}

QByteArray ClientAnswerParser::currentVersion()
{
    QByteArray array;
    QFile fileR(version);
    if (!fileR.open(QFile::ReadOnly | QFile::Text))
    {
        QString str = "Не удалось открыть файл";
        qDebug() << "xmlAnswer: " << str;
    }
    else
    {
        array = fileR.readAll();
        fileR.close();   // Закрываем файл
    }

    return array;
}
