#include "questionanswerwidget.h"

QuestionAnswerWidget::QuestionAnswerWidget( QWidget *parent ) : QWidget( parent )
{
  qsrand( time( 0 ) );

}

void QuestionAnswerWidget::setValues( QString question, QList< QString > trueAnswers, QList< int > trueAnswerIds,
                                      QList< QString > falseAnswers, QList< int > falseAnswerIds )
{
  setWidget( trueAnswers.length() + falseAnswers.length() );

  //Fragetext setzen
  tlQuestion->setText( question );

  QList< int > isAnswerTrueUsedIndex;//hier wird gespeichert welche Felder bzw. Checkboxen schon verwendet werden
  //Richtige Antworten zufällig verteilen
  int trueAnswersLength = trueAnswers.length();
  for( int i = 0; i < trueAnswersLength; i++ )
  {
    bool ok = true;
    int randomIsAnswerTrue, randomTrueAnswer;
    do
    {
      ok = true;
      randomIsAnswerTrue = qrand() % isAnswerTrue.length();
      for( int i = 0; i < isAnswerTrueUsedIndex.length(); i++ )
        if( isAnswerTrueUsedIndex[ i ] == randomIsAnswerTrue )
          ok = false;
      if( isAnswerTrue[ randomIsAnswerTrue ] == true && ok )
        ok = false;
      else
      {
        isAnswerTrue[ randomIsAnswerTrue ] = true;
        isAnswerTrueUsedIndex << randomIsAnswerTrue;
      }
    }while( !ok );
    randomTrueAnswer = qrand() % trueAnswers.length();
    cbAnswers[ randomIsAnswerTrue ]->setText( trueAnswers[ randomTrueAnswer ] );
    trueAnswers.removeAt( randomTrueAnswer );
    answerIds[ randomIsAnswerTrue ] = trueAnswerIds[ randomTrueAnswer ];
  }

  //Falsche Antworten zufällig verteilen
  int falseAnswersLength = falseAnswers.length();
  for( int i = 0; i < falseAnswersLength; i++ )
  {
    bool ok = true;
    int randomIsAnswerTrue, randomFalseAnswer;
    do
    {
      ok = true;
      randomIsAnswerTrue = qrand() % isAnswerTrue.length();
      for( int i = 0; i < isAnswerTrueUsedIndex.length(); i++ )
        if( isAnswerTrueUsedIndex[ i ] == randomIsAnswerTrue )
          ok = false;
      if( isAnswerTrue[ randomIsAnswerTrue ] != false && ok )
        ok = false;
      else
        isAnswerTrueUsedIndex << randomIsAnswerTrue;
    }while( !ok );
    randomFalseAnswer = qrand() % falseAnswers.length();
    cbAnswers[ randomIsAnswerTrue ]->setText( falseAnswers[ randomFalseAnswer ] );
    falseAnswers.removeAt( randomFalseAnswer );
    answerIds[ randomIsAnswerTrue ] = falseAnswerIds[ randomFalseAnswer ];
  }
}

void QuestionAnswerWidget::setWidget( int cbCount )
{
  //Layout leeren, Variablen / Listen leeren
  cbAnswers.clear();
  isAnswerTrue.clear();
  answerIds.clear();
  //Layout zusammenbauen ( Cb, Variablen,...)
  if( layout() != NULL )
  {
    while( layout()->count() )
      delete layout()->itemAt( layout()->count() - 1 )->widget();
    delete layout();
  }
  QGridLayout *gLayout = new QGridLayout( this );

  tlQuestion = new QLabel();
  tlQuestion->setWordWrap(true);
  gLayout->addWidget( tlQuestion, 0, 0, 1, 2 );

  for( int i = 0; i < cbCount; i++ )
  {
    cbAnswers << new CheckBox();
    isAnswerTrue << false;
    answerIds << -1;
    QLabel *label = new QLabel( QString::number( i + 1 ) + ":" );
    gLayout->addWidget( label, i + 1, 0, 1, 1 );
    gLayout->addWidget( cbAnswers[ i ], i + 1, 1, 1, 1 );
  }
}

bool QuestionAnswerWidget::getResult( void )
{
  for( int i = 0; i < cbAnswers.length(); i++ )
    if( cbAnswers[ i ]->isChecked() != isAnswerTrue[ i ] )//Sobald eine Antwort nicht richtig ausgewählt wurde, ist die ganze Aufgabe falsch
      return false;
  return true;
}

void QuestionAnswerWidget::highlightAnswers( void )
{
  for( int i = 0; i < isAnswerTrue.length(); i++ )
  {
    QPalette palette = cbAnswers[ i ]->palette();
    if( isAnswerTrue[ i ] )
    {
      palette.setColor( QPalette::WindowText, Qt::darkGreen );
      palette.setColor( QPalette::Text, Qt::darkGreen );
    }
    else
    {
      palette.setColor( QPalette::WindowText, Qt::red );
      palette.setColor( QPalette::Text, Qt::red );
    }
    cbAnswers[ i ]->setPalette( palette );
  }
}

void QuestionAnswerWidget::setCBsEnabled( bool state )
{
  for( int i = 0; i < cbAnswers.length(); i++ )
    cbAnswers[ i ]->setEnabled( state );
}

QList< int > QuestionAnswerWidget::getAnswerIds( void )
{
  return answerIds;
}

QList< bool > QuestionAnswerWidget::getIsAnswerTrue( void )
{
  return isAnswerTrue;
}
