#include "controller.h"

Controller::Controller( QWidget *parent ) : QWidget( parent )
{
  setupUi( this );
  showMenu();
}

Controller::~Controller( void )
{
  //delete menu;
  //delete quizSettings;
  //delete quiz;
  //delete quizEvaluation;
  //delete manageQuestionsMenu;
  //delete addQuestions;
}

void Controller::showMenu( void )
{
  setWindowTitle( tr( "Menu" ) );
  switchLanguage();
  menu = new Menu( this );
  delete layout()->replaceWidget( layout()->itemAt( 0 )->widget(), menu )->widget();
}

void Controller::showQuizSettings( void )
{
  setWindowTitle( tr( "Quiz settings" ) );
  switchLanguage();
  quizSettings = new QuizSettings( this );
  delete layout()->replaceWidget( layout()->itemAt( 0 )->widget(), quizSettings )->widget();
}

void Controller::showQuiz( void )
{
  setWindowTitle( tr( "Quiz" ) );
  switchLanguage();
  quiz = new Quiz( this );
  delete layout()->replaceWidget( layout()->itemAt( 0 )->widget(), quiz )->widget();
}

void Controller::showQuizCancelWindow()
{
  CancelWindow *cw = new CancelWindow( tr( "Do you want to cancel the quiz?" ) );
  connect( cw, SIGNAL( clickedYes() ), this, SIGNAL( quizQuitted() ) );
  connect( cw, SIGNAL( clickedYes() ), this, SLOT( showMenu() ) );
  cw->show();
}

void Controller::showQuizEvaluation( int numberOfTrueAnsweredQuestions, int numberOfQuestions, int averageTimePerQuestion )
{
  setWindowTitle( tr( "Quiz Evaluation" ) );
  switchLanguage();
  quizEvaluation = new QuizEvaluation( this );
  quizEvaluation->setValues( numberOfTrueAnsweredQuestions, numberOfQuestions, averageTimePerQuestion );
  delete layout()->replaceWidget( layout()->itemAt( 0 )->widget(), quizEvaluation )->widget();
}

void Controller::showManageQuestionsMenu( void )
{
  setWindowTitle( tr( "Manage questions" ) );
  switchLanguage();
  manageQuestionsMenu = new ManageQuestionsMenu( this );
  delete layout()->replaceWidget( layout()->itemAt( 0 )->widget(), manageQuestionsMenu )->widget();
}

void Controller::showAddQuestion( void )
{
  setWindowTitle( tr( "Add questions" ) );
  switchLanguage();
  addQuestions = new AddQuestion( this );
  delete layout()->replaceWidget( layout()->itemAt( 0 )->widget(), addQuestions )->widget();
}

void Controller::showAddQuestionsPerFile( void )
{
  setWindowTitle( tr( "Add questions per CSV-file" ) );
  switchLanguage();
  addQuestionsPerFile = new AddQuestionsPerFile( this );
  delete layout()->replaceWidget( layout()->itemAt( 0 )->widget(), addQuestionsPerFile )->widget();
}

void Controller::showDeleteQuestion( void )
{
  setWindowTitle( tr( "Delete questions" ) );
  switchLanguage();
  deleteQuestions = new DeleteQuestion( this );
  delete layout()->replaceWidget( layout()->itemAt( 0 )->widget(), deleteQuestions )->widget();
}

void Controller::showPasswordCheck( void )
{
  PasswordCheck *passCheck = new PasswordCheck();
  connect( passCheck, SIGNAL( enteredRightPassword() ), this, SLOT( showManageQuestionsMenu() ) );
  passCheck->show();
}

void Controller::showDatabaseEditor( void )
{
  system( "start ./Database_editor" );
}

int Controller::getCurrentLanguageId( void )
{
  return db.getCurrentLanguageId();
}

//Settings
void Controller::changeLanguageToDE( void )
{
  db.setCurrentCountryCode( QString( "DE" ) );
  db.setCurrentLanguageId( 1 );
  showMenu();//weil die Sprache nur im Menü änderbar ist.
}

void Controller::changeLanguageToEN( void )
{
  db.setCurrentCountryCode( QString( "EN" ) );
  db.setCurrentLanguageId( 2 );
  showMenu();
}

void Controller::changeLanguageToEE( void )
{
  db.setCurrentCountryCode( QString( "EE" ) );
  db.setCurrentLanguageId( 3 );
  showMenu();
}

void Controller::switchLanguage( void )
{
  qApp->removeTranslator( &translator );
  translator.load( ":/Languages/text_" + db.getCurrentCountryCode().toLower() + ".qm", "." );
  qApp->installTranslator( &translator );
}

//QuizSettings
void Controller::setNumberOfQuestions( int qNumber )
{
  db.setNumberOfQuestions( qNumber );
}

int Controller::getNumberOfQuestions( void )
{
  return db.getNumberOfQuestions();
}

void Controller::setTimePerQuestion(int time)
{
  db.setTimePerQuestion( time );
}

int Controller::getTimePerQuestion( void )
{
  return db.getTimePerQuestion();
}

void Controller::setCurrentCategoryIds( QList< int > categoryIds )
{
  db.setCurrentCategoryIds( categoryIds );
}

QList< int > Controller::getCurrentCategoryIds( void )
{
  return db.getCurrentCategoryIds();
}

void Controller::saveQuizSettings( int numberOfQuestion, int timePerQuestion, QList< int > currentCategoryIds )
{
  this->setNumberOfQuestions( numberOfQuestion );
  this->setTimePerQuestion( timePerQuestion );
  this->setCurrentCategoryIds( currentCategoryIds );
}

//Category
QList< int > Controller::getAllCategoryIds( void )
{
  return db.getAllCategoryIds();
}

QString Controller::getCategoryNameAt( int id )
{
  return db.getCategoryNameAt( id );
}

QList< QString > Controller::getCategoryNamesAt( int categoryId )
{
  return db.getCategoryNamesAt( categoryId );
}

QString Controller::getCategoryNameForQuestionId(int questionId)
{
  return db.getCategoryNameForQuestionId( questionId );
}

int Controller::getCategoryIdForQuestionId( int questionId )
{
  return db.getCategoryIdForQuestionId( questionId );
}

int Controller::addCategory( void )
{
  return db.addCategory();
}

void Controller::setDECategoryText( int categoryId, QString DECategoryText )
{
  db.setCategoryText( 1, categoryId, DECategoryText );
}

void Controller::setENCategoryText( int categoryId, QString ENCategoryText )
{
  db.setCategoryText( 2, categoryId, ENCategoryText );
}

void Controller::setEECategoryText( int categoryId, QString EECategoryText )
{
  db.setCategoryText( 3, categoryId, EECategoryText );
}

//Question
QList< int > Controller::getAllQuestionIds( void )
{
  return db.getAllQuestionIds();
}

QList< int > Controller::getQuestionIdsToCategoryIds( QList< int > categoryIds )
{
  return db.getQuestionIdsToCategoryIds( categoryIds );
}

QString Controller::getQuestionTextAt( int questionId )
{
  return db.getQuestionTextAt( questionId );
}

QList< QString > Controller::getQuestionTextsAt( int questionId )
{
  return db.getQuestionTextsAt( questionId );
}

int Controller::addQuestion( int categoryId )
{
  return db.addQuestion( categoryId );
}

void Controller::setDEQuestionText( int questionId, QString DEQuestionText )
{
  db.setQuestionText( 1, questionId, DEQuestionText );
}

void Controller::setENQuestionText( int questionId, QString ENQuestionText )
{
  db.setQuestionText( 2, questionId, ENQuestionText );
}

void Controller::setEEQuestionText( int questionId, QString EEQuestionText )
{
  db.setQuestionText( 3, questionId, EEQuestionText );
}

void Controller::setQuestionAnswer( int answerId, int questionId, int isAnswerTrue )
{
  db.setQuestionAnswer( answerId, questionId, isAnswerTrue );
}

void Controller::deleteQuestionAndAnswers( int questionId )
{
  int categoryId = db.getCategoryIdForQuestionId( questionId );
  //DeleteAnswers
  QList< int > answerIds = getTrueAnswerIdsToQuestionId( questionId );
  for( int i = 0; i < getFalseAnswerIdsToQuestionId( questionId ).length(); i++ )
    answerIds << getFalseAnswerIdsToQuestionId( questionId )[ i ];
  for( int i = 0; i < answerIds.length(); i++ )
    db.deleteAnswer( answerIds[ i ] );
  //DeleteQuestion
  db.deleteQuestion( questionId );
  //DeleteCategory
  QList< int > categoryIds;
  categoryIds << categoryId;
  if( getQuestionIdsToCategoryIds( categoryIds ).length() <= 0 )
    db.deleteCategory( categoryId );
}

//Answer
QList< QString > Controller::getTrueAnswerTextsToQuestionId( int questionId )
{
  return db.getTrueAnswerTextsToQuestionId( questionId );
}

QList< int > Controller::getTrueAnswerIdsToQuestionId( int questionId )
{
  return db.getTrueAnswerIdsToQuestionId( questionId );
}

QList< QString > Controller::getFalseAnswerTextsToQuestionId( int questionId )
{
  return db.getFalseAnswerTextsToQuestionId( questionId );
}

QList< int > Controller::getFalseAnswerIdsToQuestionId( int questionId )
{
  return db.getFalseAnswerIdsToQuestionId( questionId );
}

QList< QString > Controller::getAnswerTextsAt( int answerId )
{
  return db.getAnswerTextsAt( answerId );
}

QString Controller::getExplanationTextToAnswerId(int answerId)
{
  return db.getExplanationTextToAnswerId( answerId );
}

QList< QString > Controller::getExplanationTextsToAnswerId( int answerId )
{
  return db.getExplanationTextsToAnswerId( answerId );
}

int Controller::addAnswer( void )
{
  return db.addAnswer();
}

void Controller::setDEAnswerText( int answerId, QString DEAnswerText, QString DEExplanationText )
{
  db.setAnswerText( 1, answerId, DEAnswerText, DEExplanationText );
}

void Controller::setENAnswerText( int answerId, QString ENAnswerText, QString ENExplanationText )
{
  db.setAnswerText( 2, answerId, ENAnswerText, ENExplanationText );
}

void Controller::setEEAnswerText( int answerId, QString EEAnswerText, QString EEExplanationText )
{
  db.setAnswerText( 3, answerId, EEAnswerText, EEExplanationText );
}

