#include "SpellViewer.h"
#include <QFormLayout>

SpellViewer::SpellViewer(QWidget *parent)
    : QWidget(parent)
{
    setupUi();
    connect(this, &SpellViewer::spellChanged, this, &SpellViewer::onSpellChanged);
}

void SpellViewer::setSpell(Spell spell)
{
    _spell = spell;
    emit spellChanged();
}

const Spell& SpellViewer::spell() const
{
    return _spell;
}

void SpellViewer::onSpellChanged()
{
    _name->setText(_spell.name());
    _school->setText(_spell.school());
    _level->setText(QString::number(_spell.level()));
    _classes->setText(_spell.classes().join(", "));
    _castingtime->setText(_spell.castingtime());
    _range->setText(_spell.range());
    _duration->setText(_spell.duration());
    _components->setText(_spell.components().humanReadable());
    _ritual->setText(_spell.ritual() ? "Yes" : "No");
    _concentration->setText(_spell.concentration() ? "Yes" : "No");
    _description->setHtml(_spell.description());
    auto cursor = _description->textCursor();
    _description->selectAll();
    _description->setFontPointSize(12);
    _description->setTextCursor(cursor);
}

void SpellViewer::setupUi()
{
    QFormLayout *layout = new QFormLayout();
    QFont font;
    font.setBold(true);

    _name = new QLabel(this);
    _name->setFont(font);
    layout->addRow("Name:", _name);

    _school = new QLabel(this);
    _school->setFont(font);
    layout->addRow("School:", _school);

    _level = new QLabel(this);
    _level->setFont(font);
    layout->addRow("Level:", _level);

    _classes = new QLabel(this);
    _classes->setFont(font);
    layout->addRow("Classes:", _classes);

    _castingtime = new QLabel(this);
    _castingtime->setFont(font);
    layout->addRow("Casting time:", _castingtime);

    _range = new QLabel(this);
    _range->setFont(font);
    layout->addRow("Range:", _range);

    _duration = new QLabel(this);
    _duration->setFont(font);
    layout->addRow("Duration:", _duration);

    _components = new QLabel(this);
    _components->setFont(font);
    _components->setWordWrap(true);
    layout->addRow("Components:", _components);

    _ritual = new QLabel(this);
    _ritual->setFont(font);
    layout->addRow("Ritual:", _ritual);

    _concentration = new QLabel(this);
    _concentration->setFont(font);
    layout->addRow("Concentration:", _concentration);

    _description = new QTextEdit(this);
    _description->setReadOnly(true);
    auto policy = _description->sizePolicy();
    policy.setVerticalStretch(1);
    _description->setSizePolicy(policy);
    layout->addRow("Description:", _description);

    this->setLayout(layout);
    connect(this, &SpellViewer::spellChanged, this, &SpellViewer::onSpellChanged);
}
