/*
Copyright (c) 2007 Matt Williams matt@milliams.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License or (at
your option) version 3 or, at the discretion of KDE e.V. (which shall
act as a proxy as in section 14 of the GPLv3), any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "kqtimapping.h"

#include <kdebug.h>

KQtiMapping::KQtiMapping(int lower, int upper, int defaultValue)
{
    mMappingLowerBound = lower;
    mMappingUpperBound = upper;
    mMappingDefaultValue = defaultValue;
}

KQtiMapping::KQtiMapping(const QDomElement& element)
{
    mMappingLowerBound = element.attribute("lowerBound").toInt();
    mMappingUpperBound = element.attribute("upperBound").toInt();
    mMappingDefaultValue = element.attribute("defaultValue").toInt();
    
    kDebug() << "number of maps " << element.elementsByTagName("mapEntry").size();
    for(int i=0; i<element.elementsByTagName("mapEntry").size(); i++)
    {
        QDomNamedNodeMap mapEntryElementAttributeList = element.elementsByTagName("mapEntry").item(i).attributes();
        add(mapEntryElementAttributeList.namedItem("mapKey").nodeValue(), mapEntryElementAttributeList.namedItem("mappedValue").nodeValue().toInt());
    }
}

KQtiMapping::~KQtiMapping()
{
}

void KQtiMapping::add(const QString& key, int value)
{
    mMapEntries.insert(key, value);
}

QDomElement KQtiMapping::element() const
{
    QDomDocument doc;
    
    QDomElement mappingElement = doc.createElement("mapping");
    mappingElement.setAttribute("lowerBound", mMappingLowerBound);
    mappingElement.setAttribute("upperBound", mMappingUpperBound);
    mappingElement.setAttribute("defaultValue", mMappingDefaultValue);
    
    foreach(QString key, mMapEntries.keys())
    {
        QDomElement mapElement = doc.createElement("mapEntry");
        mapElement.setAttribute("mapKey", key);
        mapElement.setAttribute("mappedValue", mMapEntries[key]);
        mappingElement.appendChild(mapElement);
    }
    
    return mappingElement;
}
