#include "InteractiveConf.h"

InteractiveConf::InteractiveConf(QObject *parent)
    : QObject{parent}
{}


void InteractiveConf::confSaveElseMaterials(QStringList &elseMaterials)
{
    QDomDocument doc;

    QDomElement root = doc.createElement("Config");
    doc.appendChild(root);

    QDomElement colorsElement = doc.createElement("Rfids");
    root.appendChild(colorsElement);

    for (const QString &rfid : elseMaterials)
    {
        QDomElement item = doc.createElement("Rfid");
        item.appendChild(doc.createTextNode(rfid));
        colorsElement.appendChild(item);
    }

    QFile file(INTERACTIVE_CONF_FILE);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream stream(&file);
        doc.save(stream, 4);
        file.close();
    }
}

QStringList InteractiveConf::confReadElseMaterials(void)
{
    QStringList elseMaterials;

    QFile file(INTERACTIVE_CONF_FILE);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return elseMaterials;
    }

    QDomDocument doc;
    if (!doc.setContent(&file))
    {
        file.close();
        return elseMaterials;
    }
    file.close();

    QDomElement root = doc.documentElement();


    QDomElement colorsElement = root.firstChildElement("Rfids");
    QDomNodeList colorItems = colorsElement.elementsByTagName("Rfid");

    for (int i = 0; i < colorItems.count(); ++i)
    {
        QDomElement item = colorItems.at(i).toElement();
        elseMaterials.append(item.text());
    }

    return elseMaterials;
}
