#include "ParticipantView.h"
#include <QMessageBox>
#include <QVBoxLayout>


ParticipantView::ParticipantView(ParticipantModel* model, QWidget* parent) : QWidget{ parent }, model{ model }
{
    this->initGUI();
    this->view->setModel(model);
    this->connectSignalsAndSlots();
}

ParticipantView::~ParticipantView()
{

}

void ParticipantView::initGUI()
{
    this->view = new QTableView{};
    this->answerEdit = new QLineEdit{};
    this->addButton = new QPushButton{ "Add" };

    QVBoxLayout* layout = new QVBoxLayout{};
    layout->addWidget(this->view);
    layout->addWidget(this->answerEdit);
    layout->addWidget(this->addButton);

    this->setLayout(layout);
}

void ParticipantView::connectSignalsAndSlots()
{
    QObject::connect(this->addButton, &QPushButton::clicked, this, [this]() {
        QModelIndexList selectedIndexes = this->view->selectionModel()->selectedIndexes();
        if (selectedIndexes.size() != 1 || selectedIndexes.at(0).column()!=3)
        {
            QMessageBox::critical(this, "Error", "Please select one question!");
            return;
        }
        int index = selectedIndexes.at(0).row();

        std::string answer = this->answerEdit->text().toStdString();
        this->model->answerQuestion(index, answer);
        });
}

