#include "quiz.h"

Quiz::Quiz( Controller *controller, QWidget *parent ) : QWidget( parent )
{
    qsrand( time( 0 ) );
  setupUi( this );
  answerExplanationWidget->hide();
  this->controller = controller;

  int numberOfQuestions = this->controller->getNumberOfQuestions();
  currentQuestionNumber = 0;
  trueAnsweredQuestions = 0;
  totalTime = 0;
  isAnswerExplanationWidgetShown = true;
//Time
  int timePerQuestion = this->controller->getTimePerQuestion();
  if( timePerQuestion == 0 )//Time is disabled
  {
    countdown->hide();
    line_2->hide();
    timePerQuestion = 100000;
  }
  else
    connect( countdown, SIGNAL( timeout() ), this, SLOT( nextStep() ) );
  countdown->start( timePerQuestion );
  connect( this->controller, SIGNAL( quizQuitted() ), countdown, SLOT( stop() ) );

  connect( pbCancel, SIGNAL( clicked( bool ) ), controller, SLOT( showQuizCancelWindow() ) );
  connect( pbForward, SIGNAL( clicked( bool ) ), this, SLOT( nextStep() ) );

  mainLayout->setParent( 0 );
  mainScrollAreaWidget->setLayout( mainLayout );

//Mögliche QuestionIds holen und zufällig in einer List anordnen
  QList< int > possibleQuestionIds;
  possibleQuestionIds = this->controller->getQuestionIdsToCategoryIds( this->controller->getCurrentCategoryIds() );
  if( possibleQuestionIds.length() > 0 )
  {
    do{//Inhalte aus possibleQuestionIds zufällig in questionIds verteilen
      bool ok = true;
      int random;
      do
      {
        ok = true;
        random = qrand() % possibleQuestionIds.length();
        for( int i = 0; i < questionIds.length(); i++)
        {
          if( possibleQuestionIds[ random ] == questionIds[ i ] )
            ok = false;
        }
      }while( !ok );
      questionIds << possibleQuestionIds[ random ];
    }while( questionIds.length() != possibleQuestionIds.length() && questionIds.length() != numberOfQuestions );
  }else qDebug() << "Es gibt keine Fragen zur Auswahl!";
  nextStep();//Erste Frage anzeigen usw. / Danach Automatisch durch connects
}

Quiz::~Quiz( void )
{
}

void Quiz::nextStep( void )
{
  if( isAnswerExplanationWidgetShown )
  {
    if( currentQuestionNumber >= questionIds.length() )//Wenn letzte Frage erreicht ist
    {
      controller->showQuizEvaluation( trueAnsweredQuestions, questionIds.length(), totalTime );
      return;
    }
    line_4->hide();
    answerExplanationWidget->hide();
    isAnswerExplanationWidgetShown = false;
    countdown->restart();
    nextQuestion();
  }
  else
  {
    line_4->show();
    answerExplanationWidget->show();
    isAnswerExplanationWidgetShown = true;
    checkQuestion();
    countdown->stop();
  }
}

void Quiz::nextQuestion( void )
{
  currentQuestionNumber++;
  tlCurrentQuestionNumber->setText( QString::number( currentQuestionNumber ) + " / " + QString::number( questionIds.length() ) );

  int currentQuestionId = questionIds[ currentQuestionNumber - 1 ];
  //Aktuelle Kategorie anzeigen
  tlCurrentQuestionCategory->setText( controller->getCategoryNameForQuestionId( currentQuestionId ) );
  //Frage und Antworten anzeigen...
  questionAnswerWidget->setValues( controller->getQuestionTextAt( currentQuestionId ),
                                   controller->getTrueAnswerTextsToQuestionId( currentQuestionId ),
                                   controller->getTrueAnswerIdsToQuestionId( currentQuestionId ),
                                   controller->getFalseAnswerTextsToQuestionId( currentQuestionId ),
                                   controller->getFalseAnswerIdsToQuestionId( currentQuestionId ) );
}

void Quiz::checkQuestion( void )
{
  trueAnsweredQuestions += questionAnswerWidget->getResult() ? 1 : 0;
  totalTime += countdown->getUsedTime();
  QList< QString > explanations;
  for( int i = 0; i < questionAnswerWidget->getIsAnswerTrue().length(); i++ )
  {
    if( questionAnswerWidget->getIsAnswerTrue()[ i ] )
      explanations << tr( "Explanation " ) + QString::number( i + 1 ) + ": " +
                      controller->getExplanationTextToAnswerId( questionAnswerWidget->getAnswerIds()[ i ] );
  }

  answerExplanationWidget->setExplanationTexts( explanations );
  questionAnswerWidget->highlightAnswers();
  questionAnswerWidget->setCBsEnabled( false );
  answerExplanationWidget->setResultText( questionAnswerWidget->getResult() );
}
