#include <QThread>
#include <QDebug>
#include "tasktreepreparation.h"


TaskTreePreparation::TaskTreePreparation(QObject *parent) :
    QObject(parent),
    listAllModules(nullptr)
{
    qDebug() << "TaskTreePreparation init thread ID " << QThread::currentThreadId();
}

TaskTreePreparation::~TaskTreePreparation()
{
    deleteAllModuls();
}

QTreeWidgetItem *TaskTreePreparation::addModuleToTreeWidget(Module *module, QTreeWidgetItem *parentItem)
{
    QTreeWidgetItem* itemModule = nullptr;

    QString text = "";
    QString ID = QString::number(module->getID());
    QString code = "";

    itemModule = new QTreeWidgetItem();

    if(parentItem)
        parentItem->addChild(itemModule);

    if(module->getType() == ModuleType::TYPE_PM)
    {
        PM* PMmodul = static_cast<PM*>(module);
        text = PMmodul->getLangStructRus().title;
        code = PMmodul->pmCode();

        for(Module* module : PMmodul->getListChildModules())
        {
            addModuleToTreeWidget(module, itemModule);
        }
    }
    else
    {
        DM* DMmodul = static_cast<DM*>(module);
        text = DMmodul->getLangStructRus().techName;
        code = DMmodul->dmCode();

        //itemModule->setFlags(itemModule->flags() | Qt::ItemIsUserCheckable);
        //itemModule->setCheckState(0, Qt::Checked);
        itemModule->setIcon(0, QIcon(":/resources/icons/procedure.png"));
    }

    itemModule->setText(ColumnsTree::clmn_PMorDM, text);
    itemModule->setText(ColumnsTree::clmn_code, code);
    itemModule->setText(ColumnsTree::clmn_ID, ID);

    return itemModule;
}

void TaskTreePreparation::loadAMMtasksFromXML(QByteArray array)
{
    deleteAllModuls();

    QDomDocument docTasksDOM;

    docTasksDOM.setContent(array);

    QDomElement manifestElement = docTasksDOM.firstChildElement("manifest");
    if(manifestElement.isNull())
        return;

    domElementParser(manifestElement, nullptr);
}

void TaskTreePreparation::deleteAllModuls()
{
    if(listAllModules == nullptr)
        return;

    for(Module* module: *listAllModules)
    {
        if(module->getType() == ModuleType::TYPE_PM)
            delete static_cast<PM*>(module);
        else
            delete static_cast<DM*>(module);
    }
    listAllModules->clear();
}

void TaskTreePreparation::domElementParser(QDomElement element, Module* moduleParent)
{
    QString name;

    QDomElement childElement = element.firstChildElement();
    if(childElement.isNull())
        return;

    Module* module = nullptr;

    do
    {
        name = childElement.nodeName();

       QDomNamedNodeMap nodeMap = childElement.attributes();

       if(name == "pm")
       {
           module = new PM();
           PM* PMmodul = static_cast<PM*>(module);

           PMmodul->initialize(nodeMap.namedItem("modelIdentCode").nodeValue(),
                               nodeMap.namedItem("pmIssuer").nodeValue(),
                               nodeMap.namedItem("pmNumber").nodeValue(),
                               nodeMap.namedItem("pmVolume").nodeValue());

           if(moduleParent)
           {
               PMmodul->setParentModule(moduleParent);

               PM* PMmodulParent = static_cast<PM*>(moduleParent);
               PMmodulParent->addChildModule(module);
           }
       }
       else if(name == "dm")
       {
           module = new DM();
           DM* DMmodul = static_cast<DM*>(module);

           DMmodul->initialize(nodeMap.namedItem("modelIdentCode").nodeValue(),
                               nodeMap.namedItem("systemDiffCode").nodeValue(),
                               nodeMap.namedItem("systemCode").nodeValue(),
                               nodeMap.namedItem("subSystemCode").nodeValue(),
                               nodeMap.namedItem("subSubSystemCode").nodeValue(),
                               nodeMap.namedItem("assyCode").nodeValue(),
                               nodeMap.namedItem("disassyCode").nodeValue(),
                               nodeMap.namedItem("disassyCodeVariant").nodeValue(),
                               nodeMap.namedItem("infoCode").nodeValue(),
                               nodeMap.namedItem("infoCodeVariant").nodeValue(),
                               nodeMap.namedItem("itemLocationCode").nodeValue());

           if(moduleParent)
           {
               DMmodul->setParentModule(moduleParent);

               PM* PMmodulParent = static_cast<PM*>(moduleParent);
               PMmodulParent->addChildModule(module);
           }
       }
       else if(name == "rus" || name == "eng")
       {
           if(moduleParent)
           {
               if(moduleParent->getType() == ModuleType::TYPE_PM)
               {//PM
                   PM* PMmodulParent = static_cast<PM*>(moduleParent);

                   if(name == "rus")
                        PMmodulParent->setLangStructRus(nodeMap.namedItem("title").nodeValue());
                   else
                        PMmodulParent->setLangStructEng(nodeMap.namedItem("title").nodeValue());
               }
               else
               {//DM
                   DM* DMmodulParent = static_cast<DM*>(moduleParent);

                   if(name == "rus")
                        DMmodulParent->setLangStructRus(nodeMap.namedItem("techName").nodeValue(),
                                                        nodeMap.namedItem("infoName").nodeValue(),
                                                        nodeMap.namedItem("pdf").nodeValue(),
                                                        nodeMap.namedItem("bookmark").nodeValue(),
                                                        nodeMap.namedItem("xml").nodeValue());
                   else
                       DMmodulParent->setLangStructEng(nodeMap.namedItem("techName").nodeValue(),
                                                       nodeMap.namedItem("infoName").nodeValue(),
                                                       nodeMap.namedItem("pdf").nodeValue(),
                                                       nodeMap.namedItem("bookmark").nodeValue(),
                                                       nodeMap.namedItem("xml").nodeValue());
               }
           }

       }

        domElementParser(childElement, module);

        if(moduleParent == nullptr)
            listAllModules->append(module);

    }while (! (childElement = childElement.nextSiblingElement()).isNull());
}

void TaskTreePreparation::slot_prepareListItems(QByteArray array, QList<Module*>* listAllModules)
{
    qDebug() << "TaskTreePreparation::slot_prepareListItems thread ID " << QThread::currentThreadId();

    this->listAllModules = listAllModules;

    loadAMMtasksFromXML(array);

    listItems.clear();

    for(Module* module : *this->listAllModules)
    {
        QTreeWidgetItem*  item = addModuleToTreeWidget(module);
        listItems.append(item);
    }

    Q_EMIT signal_listItemsReady(listItems);
}
