#include "PresenterModel.h"

PresenterModel::PresenterModel(QuestionRepo& repo, QObject* parent) : repo{ repo }, QAbstractTableModel{ parent }
{

}

int PresenterModel::rowCount(const QModelIndex& parent) const
{
    return this->repo.size();
}

int PresenterModel::columnCount(const QModelIndex& parent) const
{
    return 4;
}

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

    std::vector<Question> questions = this->repo.get_all();
    Question q = questions[row];

    if (role == Qt::DisplayRole)
    {
        switch (col)
        {
        case 0:
            return QString::number(q.id());
        case 1:
            return QString::fromStdString(q.text());
        case 2:
            return QString::fromStdString(q.correct_answer());
        case 3:
            return QString::number(q.score());
        default:
            break;
        }
    }

    return QVariant();
}

QVariant PresenterModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
    {
        switch (section)
        {
        case 0:
            return QString{ "ID" };
        case 1:
            return QString{ "Text" };
        case 2:
            return QString{ "Correct Answer" };
        case 3:
            return QString{ "Score" };
        default:
            break;
        }
    }
    if(role == Qt::DisplayRole && orientation == Qt::Vertical)
    {
        return QString::number(section+1);
    }

    return QVariant();
}

void PresenterModel::addQuestion(const Question& q)
{
    beginInsertRows(QModelIndex(), this->rowCount(), this->rowCount());
    this->repo.add(q);
    endInsertRows();
}
