#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(! Tools::loadXMLtoDOM(":/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());
    }

    Tools::saveDOMtoXML("ListInstructors.xml", &commonDOM);

    return commonDOM.toByteArray();
}

QByteArray DBAnswerParser::listGroups(bool result, QList<Group> *listGroups)
{
    QDomDocument commonDOM;
    if(! Tools::loadXMLtoDOM(":/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());
    }

    Tools::saveDOMtoXML("ListGroups.xml", &commonDOM);

    return commonDOM.toByteArray();
}

QByteArray DBAnswerParser::listTrainees(bool result, QList<Trainee> *listTrainees)
{
    QDomDocument commonDOM;
    if(! Tools::loadXMLtoDOM(":/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()

        //Тайминг
        QDomNode timingNode = commonDOM.createElement("timing");
        traineeNode.appendChild(timingNode);

        TimingOfTrainee timing = trainee.getTiming();

        timingNode.toElement().setAttribute("entryTime", timing.getEntryTimeS());
        timingNode.toElement().setAttribute("exitTime", timing.getExitTimeS());
        timingNode.toElement().setAttribute("operatingTime", timing.getOperatingTimeS());
    }

    Tools::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::listContacts(bool result, QList<ContactModel> *listContacts)
{
    QDomDocument doc;
    QDomProcessingInstruction xmlDecl = doc.createProcessingInstruction("xml", "version='1.0' encoding='utf-8'");
    doc.insertBefore(xmlDecl,doc.firstChild());
    QDomElement root = doc.createElement("ContactArray");

    for(ContactModel entity : *listContacts)
    {
        QDomElement contact = doc.createElement("ContactData");
        contact.toElement().setAttribute("name",entity.getName());
        contact.toElement().setAttribute("id",entity.getID());
        QString isLogged = entity.getLoggedIn() ? "1" : "0";
        contact.toElement().setAttribute("isOnline",isLogged);
        contact.toElement().setAttribute("UserType",entity.getType());
        contact.toElement().setAttribute("Login",entity.getLogin());

        root.appendChild(contact);
    }

    doc.appendChild(root);

    qDebug() << doc.toString();
    return doc.toByteArray();

}

QByteArray DBAnswerParser::listTasksAMMofTrainee(bool result, QList<TaskAmmFim> *listTasks, int trainee_id, bool full_list)
{
    QDomDocument commonDOM;
    if(! Tools::loadXMLtoDOM(":/resources/blankXML/ListTasksAMM.xml", &commonDOM))
        return QByteArray();

    QDomNode listNode = commonDOM.namedItem("ListTasksAMM");
    listNode.toElement().setAttribute("trainee_id", QString::number(trainee_id));
    if(full_list)
        listNode.toElement().setAttribute("full_list", "true");
    else
        listNode.toElement().setAttribute("full_list", "false");

    for(TaskAmmFim task : *listTasks)
    {
        //Задача
        QDomNode taskNode = commonDOM.createElement("taskAMM");
        listNode.appendChild(taskNode);
        taskNode.toElement().setAttribute("task_id", QString::number(task.getID()));
        taskNode.toElement().setAttribute("title", task.ammProcedure.title);
        taskNode.toElement().setAttribute("dmCode", task.ammProcedure.dmCode);
        taskNode.toElement().setAttribute("status", task.status);

        for(SubProc subProc : task.listSubProc)
        {//Подпроцедура
            QDomNode subProcNode = commonDOM.createElement("SubProc");
            taskNode.appendChild(subProcNode);

            subProcNode.toElement().setAttribute("dmCode", subProc.getDmCode());
            subProcNode.toElement().setAttribute("title", subProc.getTitle());
            subProcNode.toElement().setAttribute("canplay", subProc.getModeListStr());
        }
    }

    Tools::saveDOMtoXML("ListTasksAMM.xml", &commonDOM);

    return commonDOM.toByteArray();
}

QByteArray DBAnswerParser::listTasksFIMofTrainee(bool result, QList<TaskAmmFim> *listTasks, int trainee_id, bool full_list)
{
    QDomDocument commonDOM;
    if(! Tools::loadXMLtoDOM(":/resources/blankXML/ListTasksFIM.xml", &commonDOM))
        return QByteArray();

    QDomNode listNode = commonDOM.namedItem("ListTasksFIM");
    listNode.toElement().setAttribute("trainee_id", QString::number(trainee_id));
    if(full_list)
        listNode.toElement().setAttribute("full_list", "true");
    else
        listNode.toElement().setAttribute("full_list", "false");

    for(TaskAmmFim task : *listTasks)
    {
        //Задача
        QDomNode taskNode = commonDOM.createElement("taskFIM");
        listNode.appendChild(taskNode);
        taskNode.toElement().setAttribute("task_id", QString::number(task.getID()));
        taskNode.toElement().setAttribute("title", task.title);
        taskNode.toElement().setAttribute("status", task.status);

        for(Malfunction malfunction : task.malfunctionList)
        {//Неисправность
            QDomNode malfunctionNode = commonDOM.createElement("malfunction");
            taskNode.appendChild(malfunctionNode);
            malfunctionNode.toElement().setAttribute("dmCode", malfunction.dmCode);
            malfunctionNode.toElement().setAttribute("num", malfunction.num);
            malfunctionNode.toElement().setAttribute("description", malfunction.description);

            for(MalfunctionSign sign : malfunction.malfunctionSigns)
            {//Сигналы
                QDomNode signNode = commonDOM.createElement("malfunctionSign");
                malfunctionNode.appendChild(signNode);
                signNode.toElement().setAttribute("type", sign.type);
                signNode.toElement().setAttribute("description", sign.description);
            }
        }


        {//FIMReport
            FIMReport report = task.report;
            QDomNode reportNode = commonDOM.createElement("report");
            taskNode.appendChild(reportNode);
            reportNode.toElement().setAttribute("report_id", report.id);

            for(FIMReportItem reportItem : task.report.itemList)
            {//FIMReportItem
                QDomNode reportItemNode = commonDOM.createElement("reportItem");
                reportNode.appendChild(reportItemNode);
                reportItemNode.toElement().setAttribute("item_id", reportItem.id);
                reportItemNode.toElement().setAttribute("text", reportItem.text);

                //ProcedureID
                QDomNode procedureIDNode = commonDOM.createElement("procedureID");
                reportItemNode.appendChild(procedureIDNode);

                procedureIDNode.toElement().setAttribute("doc", reportItem.procedure.doc);
                procedureIDNode.toElement().setAttribute("title", reportItem.procedure.title);
                procedureIDNode.toElement().setAttribute("dmCode", reportItem.procedure.dmCode);
                procedureIDNode.toElement().setAttribute("result", reportItem.procedure.result);
            }
        }
    }

    Tools::saveDOMtoXML("ListTasksFIM.xml", &commonDOM);

    return commonDOM.toByteArray();
}
