#include "SpellsTableModel.h"
#include <QDebug>

namespace
{
static const int COLUMNS = 7;
static const int SPELL_NAME = 0;
static const int SPELL_LEVEL = 1;
static const int SPELL_SCHOOL = 2;
static const int SPELL_CASTTIME = 3;
static const int SPELL_RANGE = 4;
static const int SPELL_DURATION = 5;
static const int SPELL_COMPONENTS = 6;
}


SpellsTableModel::SpellsTableModel(Spells spells, QObject *parent)
    : QAbstractTableModel(parent)
    , _spells(spells)
{}

SpellsTableModel::~SpellsTableModel()
{}

int SpellsTableModel::rowCount(const QModelIndex&) const
{
    return _spells.size();
}

int SpellsTableModel::columnCount(const QModelIndex&) const
{
    return COLUMNS;
}

QVariant SpellsTableModel::data(const QModelIndex &index, int role) const
{
    int row = index.row();
    int column = index.column();

    Spell spell = _spells.at(row);

    if(role != Qt::DisplayRole)
    {
        return QVariant();
    }
    else
    {
        switch(column)
        {
        case SPELL_NAME:
            return spell.name();
        case SPELL_SCHOOL:
            return spell.school();
        case SPELL_LEVEL:
            return spell.level();
        case SPELL_CASTTIME:
            return spell.castingtime().split(",").at(0);
        case SPELL_DURATION:
            return spell.duration();
        case SPELL_RANGE:
            return spell.range();
        case SPELL_COMPONENTS:
            return spell.components().humanReadable().split("(").at(0);
        default:
            return QVariant();
        }
    }
}

QVariant SpellsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
    {
        switch(section)
        {
        case SPELL_NAME:
            return QString("Name");
        case SPELL_SCHOOL:
            return QString("School");
        case SPELL_LEVEL:
            return QString("Level");
        case SPELL_CASTTIME:
            return QString("Casting Time");
        case SPELL_DURATION:
            return QString("Duration");
        case SPELL_RANGE:
            return QString("Range");
        case SPELL_COMPONENTS:
            return QString("Components");
        default:
            return QVariant();
        }
    }
    else if(role == Qt::DisplayRole && orientation == Qt::Vertical)
    {
        return section + 1;
    }
    return QVariant();
}

void SpellsTableModel::sort(int column, Qt::SortOrder order)
{
    emit layoutAboutToBeChanged();
    switch(column)
    {
    case SPELL_NAME:
        _spells.sort("name", order);
        break;
    case SPELL_SCHOOL:
        _spells.sort("school", order);
        break;
    case SPELL_LEVEL:
        _spells.sort("level", order);
        break;
    case SPELL_CASTTIME:
        _spells.sort("casttime", order);
        break;
    case SPELL_DURATION:
        _spells.sort("duration", order);
        break;
    case SPELL_RANGE:
        _spells.sort("range", order);
        break;
    case SPELL_COMPONENTS:
        _spells.sort("components", order);
        break;
    default:
        break;
    }
    emit layoutChanged();
}

Spell SpellsTableModel::spell(int index)
{
    return _spells.at(index);
}

void SpellsTableModel::setSpellFilter(SpellFilter *filter)
{
    emit layoutAboutToBeChanged();
    _spells.setSpellFilter(filter);
    emit layoutChanged();
}

int SpellsTableModel::rowOf(const Spell &spell) const
{
    return _spells.indexOf(spell);
}
