#include "Spell.h"
#include <QDomElement>

Spell::Spell()
{}

Spell::Spell(QDomElement &e)
{
    parseFromElement(e);
}

QString Spell::name() const
{
    return _name;
}

QString Spell::school() const
{
    return _school;
}

int Spell::level() const
{
    return _level;
}

bool Spell::ritual() const
{
    return _ritual;
}

bool Spell::concentration() const
{
    return _concentration;
}

QStringList Spell::classes() const
{
    return _classes;
}

QString Spell::description() const
{
    return _description;
}

QString Spell::castingtime() const
{
    return _castingtime;
}

QString Spell::range() const
{
    return _range;
}

QString Spell::duration() const
{
    return _duration;
}

Components Spell::components() const
{
    return _components;
}

bool Spell::operator==(const Spell &other) const
{
    // Spell name is assumed to be the primary key, secondary key is description just to make sure.
    return _name == other._name && _description == other._description;
}

void Spell::parseFromElement(QDomElement &e)
{
    _name = e.attribute("name");
    _school = e.attribute("school");
    _level = e.attribute("level").toInt();
    _ritual = e.attribute("ritual") == "true";
    _concentration = e.attribute("concentration") == "true";

    QDomElement castingClass = e.firstChildElement("classes").firstChildElement();
    while(!castingClass.isNull())
    {
        _classes << castingClass.firstChild().toText().nodeValue();
        castingClass = castingClass.nextSiblingElement();
    }
    _description = e.firstChildElement("description").firstChild().toCDATASection().nodeValue();
    _castingtime = e.firstChildElement("castingtime").firstChild().toText().nodeValue();
    _range = e.firstChildElement("range").firstChild().toText().nodeValue();
    _duration = e.firstChildElement("duration").firstChild().toText().nodeValue();
    _components = Components(e.firstChildElement("components"));
}
