/*
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 "kqtiresponsedeclaration.h"
#include "kqtimapping.h"

KQtiResponseDeclaration::KQtiResponseDeclaration(const QString& identifier, const QString& cardinality, const QString& baseType) :
        mIdentifier(identifier),
        mCardinality(cardinality),
        mBaseType(baseType),
        usingMapping(false),
        mMapping(0)
{
}

KQtiResponseDeclaration::KQtiResponseDeclaration(const QDomElement& element) : usingMapping(false), mMapping(0)
{
    mIdentifier = element.attribute("identifier");
    mCardinality = element.attribute("cardinality");
    mBaseType = element.attribute("baseType");
    
    QDomElement mappingElement = element.firstChildElement("mapping");
    if (mappingElement.childNodes().count() != 0)
    {
        usingMapping = true;
        mMapping = new KQtiMapping(mappingElement);
    }
    
    QDomElement correctResponse = element.firstChildElement("correctResponse");
    for(int i=0; i<correctResponse.elementsByTagName("value").size(); i++)
    {
        QDomNode valueNode = correctResponse.elementsByTagName("value").item(i);
        addCorrectResponse(valueNode.firstChild().nodeValue());
    }
}

KQtiResponseDeclaration::~KQtiResponseDeclaration()
{
    if (usingMapping)
    {
        delete mMapping;
    }
}

void KQtiResponseDeclaration::addCorrectResponse(const QString& value)
{
    mCorrectResponse.append(value);
}

/*void KQtiResponseDeclaration::setScoreBounds(int lower, int upper, int defaultValue)
{
    usingMapping = true;
    
    mMappingLowerBound = lower;
    mMappingUpperBound = upper;
    mMappingDefaultValue = defaultValue;
}

void KQtiResponseDeclaration::addScoreMap(QString key, int value)
{
    mapEntries.insert(key, value);
}*/

void KQtiResponseDeclaration::setMapping(KQtiMapping* mapping)
{
    usingMapping = true;
    mMapping = mapping;
}

QDomElement KQtiResponseDeclaration::element() const
{
    QDomDocument doc;
    
    QDomElement responseDeclarationElement = doc.createElement("responseDeclaration");
    responseDeclarationElement.setAttribute("identifier", mIdentifier);
    responseDeclarationElement.setAttribute("cardinality", mCardinality);
    responseDeclarationElement.setAttribute("baseType", mBaseType);
    
    QDomElement correctResponseElement = doc.createElement("correctResponse");
    responseDeclarationElement.appendChild(correctResponseElement);
    
    if(usingMapping)
    {
        responseDeclarationElement.appendChild(mMapping->element());
    }
    
    foreach(QString response, mCorrectResponse)
    {
        QDomElement valueElement = doc.createElement("value");
        QDomText choiceElementText = doc.createTextNode(response);
        valueElement.appendChild(choiceElementText);
        correctResponseElement.appendChild(valueElement);
    }
    
    return responseDeclarationElement;
}
