/**
  * examwidget.cpp
  * Widget testovaciho modu (generuje otazky a vyhodnocuje spravnost odpovedi).
  *
  * @author Michala Capkova
  */

#include "examwidget.h"

#include <QLabel>
#include <QGridLayout>
#include <QPushButton>

#include <stdlib.h>
#include <time.h>
#include <iostream>

/**
  * konstruktor
  */
ExamWidget::ExamWidget(QWidget *parent) : QWidget(parent)
{      
    //inicializace generatoru nahodnych cisel
    srand (time(NULL));

    this->examItemLabel = new QLabel("Exam Item");
    this->testResultLabel = new QLabel("Result");
    this->correctAnswerButton = new QPushButton(trUtf8("Ukaž správnou odpověď"));
    this->nextQuestionButton = new QPushButton(trUtf8("Další otázka"));
    this->endExamButton = new QPushButton(trUtf8("Konec testu"));

    examLayout = new QGridLayout();
    examLayout->addWidget(examItemLabel, 0, 0);
    examLayout->addWidget(testResultLabel, 1, 0);
    examLayout->addWidget(correctAnswerButton, 2, 0);
    examLayout->addWidget(nextQuestionButton, 3, 0);
    examLayout->addWidget(endExamButton, 4, 0);
    examLayout->setRowStretch(5, 1);

    connect(nextQuestionButton, SIGNAL(clicked()), this, SLOT(showQuestionLayout()));
    connect(nextQuestionButton, SIGNAL(clicked()), this, SIGNAL(nextQuestionPressed()));
    connect(correctAnswerButton, SIGNAL(clicked()), this, SIGNAL(correctAnswerPressed()));
    connect(correctAnswerButton, SIGNAL(clicked()), this, SLOT(disableCorrectAnswerButton()));
    connect(endExamButton, SIGNAL(clicked()), this, SIGNAL(endExamPressed()));
    connect(endExamButton, SIGNAL(clicked()), this, SLOT(reset()));

    this->setLayout(examLayout);
}

/**
  * vraci retezec s nazvem objektu, ktery chceme testovat
  */
QString ExamWidget::getExamItem()
{
    return this->examItemLabel->text();
}

/**
  * generuje nazev objektu pro testovani
  */
void ExamWidget::generateExamItem()
{
    //testuje, zda neni seznam prazdy a zda uz nebyly testovany vsechny objekty
    if((examSet.size() > 0) && (blacklist.size() < examSet.size()))
    {
        int examSetCount = this->examSet.size();

        int index = -1;

        //generuje objekty dokud nenajde dosud neotestovany objekt
        do
        {
            index = rand() % examSetCount;
        }
        while (this->blacklist.contains(index));

        this->blacklist << index;
        examItemLabel->setText(examSet.at(index));
    }
}

/**
  * nastavuje testovaci mnozinu objektu
  */
void ExamWidget::setExamSet(QList<QString> examSet)
{
    this->examSet = examSet;
}

/**
  * zobrazi rozlozeni pro otazku
  */
void ExamWidget::showQuestionLayout()
{
    this->examItemLabel->show();
    this->testResultLabel->hide();
    this->correctAnswerButton->hide();
    this->nextQuestionButton->hide();
    this->endExamButton->show();
    this->generateExamItem();
}

/**
  * zobrazi rozlozeni pro vyhodnoceni odpovedi
  */
void ExamWidget::showResultLayout()
{
    this->correctAnswerButton->setEnabled(true);
    this->examItemLabel->show();
    this->testResultLabel->show();
    this->correctAnswerButton->show();

    // pokud nebyly vycerpany vsechny otazky, umozni vygenerovani dalsi otazky
    if (blacklist.size() != examSet.size())
    {
        this->nextQuestionButton->show();
    }
    this->endExamButton->show();
}

/**
  * vyhodnocuje spravnost odpovedi
  */
void ExamWidget::checkAnswer(QString itemName)
{
    if (itemName.compare(this->getExamItem()) == 0)
    {
        this->testResultLabel->setText(trUtf8("Správná odpověď"));
        this->testResultLabel->setStyleSheet("QLabel {color : blue; }");
    }
    else
    {
        this->testResultLabel->setText(trUtf8("Špatná odpověď"));
        this->testResultLabel->setStyleSheet("QLabel {color : red; }");
    }
    this->showResultLayout();
}

/**
  * start noveho testu (vycisteni obou seznamu pouzivanych pro generovani otazek)
  */
void ExamWidget::reset()
{
    blacklist.clear();
    examSet.clear();
}

/**
  * zdeaktivuje tlacitko pro spravnou odpoved
  */
void ExamWidget::disableCorrectAnswerButton()
{
    correctAnswerButton->setDisabled(true);
}
