#include "addquestion.h"

AddQuestion::AddQuestion( Controller *controller, QWidget *parent ) : QWidget( parent )
{
  setupUi( this );
  this->controller = controller;
//RadioButton Category Selection
  //Categories
  QList< QString > categoryNames;
  QList< int > categoryIds = this->controller->getAllCategoryIds();
  for( int i = 0; i < categoryIds.length(); i++)
    categoryNames << this->controller->getCategoryNameAt( categoryIds[ i ] );
  categorySelection->setValues( categoryIds, categoryNames );
  //Category Connect
  connect( rbCategories, SIGNAL( clicked( bool ) ), this, SLOT( displayRightCategoryWidget() ) );
  rbCategories->click();
  connect( rbAddNewCategory, SIGNAL( clicked( bool ) ), this, SLOT( displayRightCategoryWidget() ) );
  //Answer Connect
  rbAnswers << rbAnswer1 << rbAnswer2 << rbAnswer3 << rbAnswer4;
  for( int i = 0; i < rbAnswers.length(); i++ )
  {
    answerWidgets << new AddAnswerWidget();
    connect( rbAnswers[ i ], SIGNAL( clicked( bool ) ), this, SLOT( displayRightMleAnswer() ) );
  }
  QGridLayout *gbAnswersLayout = static_cast< QGridLayout* >( gbAnswers->layout() );
  for( int i = 0; i < answerWidgets.length(); i++ )
    gbAnswersLayout->addWidget( answerWidgets[ i ] );
  rbAnswers[ 0 ]->click();

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

  connect( pbManageQuestionsMenu, SIGNAL( clicked( bool ) ), this->controller, SLOT( showManageQuestionsMenu() ) );
  connect( pbAdd, SIGNAL( clicked( bool ) ), this, SLOT( addAllValues() ) );
}

void AddQuestion::displayRightCategoryWidget( void )
{
  rbCategories->setChecked( false );
  rbAddNewCategory->setChecked( false );
  mleCategory->hide();
  saCategories->hide();

  if( rbCategories == sender() )
  {
    rbCategories->setChecked( true );
    saCategories->show();
  }
  else if( rbAddNewCategory == sender() )
  {
    rbAddNewCategory->setChecked( true );
    mleCategory->show();
  }
}

void AddQuestion::displayRightMleAnswer( void )
{

  for( int i = 0; i < rbAnswers.length(); i++ )
  {
    answerWidgets[ i ]->hide();
    rbAnswers[ i ]->setChecked( false );
  }
  for( int i = 0; i < rbAnswers.length(); i++ )
  {
    if( rbAnswers[ i ] == sender() )
    {
      rbAnswers[ i ]->setChecked( true );
      answerWidgets[ i ]->show();
    }
  }
}

bool AddQuestion::checkInputs( void )
{
//Überprüfen ob alles korrekt ausgewählt ist
  bool ok = true;
  QPalette defaultPalette, palette;
  palette.setColor( QPalette::WindowText, Qt::red );
  if( rbCategories->isChecked() && categorySelection->getCheckedId() == -1  )//categorySelection ist nicht vollständig
  {
    ok = false;
    rbCategories->setPalette( palette );
  }else rbCategories->setPalette( defaultPalette );
  if( rbAddNewCategory->isChecked() && mleCategory->isSomeoneEmpty() )//AddNewCategory ist nicht vollständig
  {
    ok = false;
    rbAddNewCategory->setPalette( palette );
  }else rbAddNewCategory->setPalette( defaultPalette );
  if( mleQuestion->isSomeoneEmpty() )
  {
    ok = false;
    tlQuestion->setPalette( palette );
  }else tlQuestion->setPalette( defaultPalette );
  for( int i = 0; i < rbAnswers.length(); i++ )
  {
    if( answerWidgets[ i ]->isSomeoneEmpty() )
    {
      ok = false;
      rbAnswers[ i ]->setPalette( palette );
    }else rbAnswers[ i ]->setPalette( defaultPalette );
  }
  return ok;
}

void AddQuestion::addAllValues( void )
{
  if( !checkInputs() )
    return;

  //Category
  int categoryId = -1;
  if( rbCategories->isChecked() )
  {
    categoryId = categorySelection->getCheckedId();
  }
  else if( rbAddNewCategory->isChecked() )
  {
    categoryId = controller->addCategory();
    controller->setDECategoryText( categoryId, mleCategory->getDEText() );
    controller->setENCategoryText( categoryId, mleCategory->getENText() );
    controller->setEECategoryText( categoryId, mleCategory->getEEText() );
    mleCategory->clearEdits();
  }
  //Question
  int questionId = controller->addQuestion( categoryId );
  controller->setDEQuestionText( questionId, mleQuestion->getDEText() );
  controller->setENQuestionText( questionId, mleQuestion->getENText() );
  controller->setEEQuestionText( questionId, mleQuestion->getEEText() );
  //Answers
  QList< int > answerIds;
  for( int i = 0; i < answerWidgets.length(); i++ )
  {
    answerIds << controller->addAnswer();
    controller->setDEAnswerText( answerIds[ i ], answerWidgets[ i ]->getDEAnswerText(), answerWidgets[ i ]->getDEExplanationText() );
    controller->setENAnswerText( answerIds[ i ], answerWidgets[ i ]->getENAnswerText(), answerWidgets[ i ]->getENExplanationText() );
    controller->setEEAnswerText( answerIds[ i ], answerWidgets[ i ]->getEEAnswerText(), answerWidgets[ i ]->getEEExplanationText() );
    controller->setQuestionAnswer( answerIds[ i ], questionId, answerWidgets[ i ]->isAnswerTrue() );
  }

  //Clear Edits
  mleCategory->clearEdits();
  mleQuestion->clearEdits();
  for( int i = 0; i < answerWidgets.length(); i++ )
    answerWidgets[ i ]->reset();

  controller->showAddQuestion();
}
