#include "fpgaflashxml.h"
#include <QTXml>
#include <QDebug>
#include <QCryptographicHash>

const int bufferSize = 10240;

QString md5hash (const QString &fileName)
{
    QFile file (fileName);

    if (!file.open (QIODevice::ReadOnly)) {
        return QString ();
    }

    QCryptographicHash hash (QCryptographicHash::Md5);

    while (!file.atEnd()) {
        const QByteArray &buf = file.read (bufferSize);
        hash.addData (buf);
    }

    return hash.result().toHex();
}

/*
 *  check if file has md5
 *  Input parameters: filepath
 *                    md5
 *  Return:           true if MD5 is equal
 *                    false if MD5 is different
 */
bool checkMD5 (const QString &fileName, const QString &MD5)
{
    QString MD5calc = md5hash(fileName);

    if (MD5calc==MD5)
        return true;
    else
        return false;
}

fpagFlashXmlClass::fpagFlashXmlClass()
{

}

fpagFlashXmlClass::~fpagFlashXmlClass()
{

}

void fpagFlashXmlClass::setPrimary(QString _new)
{
    fPrimary = _new;
}

void fpagFlashXmlClass::setSecondary(QString _new)
{
    fSecondary = _new;
}

void fpagFlashXmlClass::setTarget(QString _target)
{
    targetFlash = _target;
}

void fpagFlashXmlClass::setXmlFile(QString _new)
{
    xmlFile = _new;
}

void fpagFlashXmlClass::setArea(unsigned int _area)
{
    area = _area;
}

bool fpagFlashXmlClass::writeXML()
{
    if (xmlFile=="")
    {
        qCritical() << "XML file is not specified!";
        return false;
    }

    if (QFile::exists(xmlFile))
    {
        //file exists, delete previous file
        QFile::remove(xmlFile);
    }

    QFileInfo finfoPri(fPrimary);
    QFileInfo finfoSec(fSecondary);

    //create new file
    QDomDocument document;
    QDomElement root = document.createElement("fpgaflashinfo");
    document.appendChild(root);

    QDomElement model = document.createElement("target");
    model.setAttribute("flash_target", targetFlash);
    model.setAttribute("area", area);
    root.appendChild(model);

    QDomElement primary = document.createElement("primary");
    primary.setAttribute("file", finfoPri.fileName());
    primary.setAttribute("MD5", md5hash(fPrimary));
    root.appendChild(primary);

    QDomElement secondary = document.createElement("secondary");
    secondary.setAttribute("file", finfoSec.fileName());
    secondary.setAttribute("MD5", md5hash(fSecondary));
    root.appendChild(secondary);

    QFile file(xmlFile);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qCritical() << "Open the file for writing failed";
        return false;
    }
    else
    {
        QTextStream stream(&file);
        stream << document.toString();
        file.close();
        qDebug() << "Writing is done";
    }

    return true;

}

bool fpagFlashXmlClass::readXML()
{
    if (xmlFile=="")
    {
        qCritical() << "XML file is not specified!";
        return false;
    }

    if (!QFile::exists(xmlFile))
    {
        //file not exists
        qCritical() << "XML file not exists!";
        return false;
    }

    //read info from file
    QDomDocument xmlBOM;
    // Load xml file as raw data
    QFile f(xmlFile);
    if (!f.open(QIODevice::ReadOnly ))
    {
        // Error while loading file
        qCritical() << "Error while loading file!";
        return false;
    }
    // Set data into the QDomDocument before processing
    xmlBOM.setContent(&f);
    f.close();

    QDomElement root=xmlBOM.documentElement();

    // Get the first child of the root (Markup COMPONENT is expected)
    QDomElement Component=root.firstChild().toElement();

    // Loop while there is a child
    while(!Component.isNull())
    {
        // Check if the child tag name is COMPONENT
        if (Component.tagName()=="target")
        {
            targetFlash=Component.attribute("flash_target","");
            area =Component.attribute("area",0).toInt();
        }
        else
        if (Component.tagName()=="primary")
        {
            fPrimary=Component.attribute("file","");
            fPrimaryMD5=Component.attribute("MD5","");
        }
        else
        if (Component.tagName()=="secondary")
        {
            fSecondary=Component.attribute("file","");
            fSecondaryMD5=Component.attribute("MD5","");
        }

        // Next component
        Component = Component.nextSibling().toElement();
    }

    return true;
}

QString fpagFlashXmlClass::getPrimaryFile()
{
    return fPrimary;
}

QString fpagFlashXmlClass::getPrimaryMD5()
{
    return fPrimaryMD5;
}

QString fpagFlashXmlClass::getSecondaryFile()
{
    return fSecondary;
}

QString fpagFlashXmlClass::getSecondaryMD5()
{
    return fSecondaryMD5;
}

QString fpagFlashXmlClass::getTarget()
{
    return targetFlash;
}

unsigned int fpagFlashXmlClass::getArea()
{
    return area;
}



