#include "dbanswerparser.h"



DBAnswerParser::DBAnswerParser(QObject *parent) : QObject(parent)
{

}

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

QByteArray DBAnswerParser::listInstructors(bool result, QList<Instructor> *listInstructors)
{
    QDomDocument commonDOM;
    if(! dataParser->loadBlankXML(":/resources/blankXML/ListInstructors.xml", &commonDOM))
        return QByteArray();

    QDomNode listNode = commonDOM.namedItem("ListInstructors");

    for(Instructor instructor : *listInstructors)
    {
        //Инструктор
        QDomNode instructorNode = commonDOM.createElement("Instructor");
        listNode.appendChild(instructorNode);
        instructorNode.toElement().setAttribute("instructor_id", QString::number(instructor.getID()));
        instructorNode.toElement().setAttribute("name", instructor.getName());
        instructorNode.toElement().setAttribute("login", instructor.getLogin());
        instructorNode.toElement().setAttribute("password", instructor.getPassword());
        instructorNode.toElement().setAttribute("is_admin", instructor.getIsAdmin());
        instructorNode.toElement().setAttribute("archived", instructor.getArchived());
        instructorNode.toElement().setAttribute("logged_in", instructor.getLoggedIn());
    }

    dataParser->saveDOMtoXML("ListInstructors.xml", &commonDOM);

    return commonDOM.toByteArray();
}

QByteArray DBAnswerParser::listGroups(bool result, QList<Group> *listGroups)
{
    QDomDocument commonDOM;
    if(! dataParser->loadBlankXML(":/resources/blankXML/ListGroups.xml", &commonDOM))
        return QByteArray();

    QDomNode listNode = commonDOM.namedItem("ListGroups");

    for(Group group : *listGroups)
    {
        //Группа
        QDomNode groupNode = commonDOM.createElement("Group");
        listNode.appendChild(groupNode);
        groupNode.toElement().setAttribute("group_id", QString::number(group.getID()));
        groupNode.toElement().setAttribute("name", group.getName());
    }

    dataParser->saveDOMtoXML("ListGroups.xml", &commonDOM);

    return commonDOM.toByteArray();
}

QByteArray DBAnswerParser::listTrainees(bool result, QList<Trainee> *listTrainees)
{
    QDomDocument commonDOM;
    if(! dataParser->loadBlankXML(":/resources/blankXML/ListTrainees.xml", &commonDOM))
        return QByteArray();

    QDomNode listNode = commonDOM.namedItem("ListTrainees");

    for(Trainee trainee : *listTrainees)
    {
        //Обучаемый
        QDomNode traineeNode = commonDOM.createElement("Trainee");
        listNode.appendChild(traineeNode);
        traineeNode.toElement().setAttribute("trainee_id", trainee.getID());
        traineeNode.toElement().setAttribute("name", trainee.getName());
        traineeNode.toElement().setAttribute("login", trainee.getLogin());
        traineeNode.toElement().setAttribute("password", trainee.getPassword());
        traineeNode.toElement().setAttribute("archived", trainee.getArchived());
        traineeNode.toElement().setAttribute("logged_in", trainee.getLoggedIn());
        traineeNode.toElement().setAttribute("group_trainee", trainee.getGroup().getID());
        traineeNode.toElement().setAttribute("computer_trainee", trainee.getComputer().getID());
        //trainee.setTasks()
    }

    dataParser->saveDOMtoXML("ListTrainees.xml", &commonDOM);

    return commonDOM.toByteArray();
}

QByteArray DBAnswerParser::listComputers(bool result, QList<Computer> *listComputers)
{
    //TODO
    return QByteArray();
}

QByteArray DBAnswerParser::listClassrooms(bool result, QList<Classroom> *listClassrooms)
{
    //TODO
    return QByteArray();
}

QByteArray DBAnswerParser::listTasks(bool result, QList<Task> *listTasks)
{
    //TODO
    return QByteArray();
}
