#include "addquestionsperfile.h"

AddQuestionsPerFile::AddQuestionsPerFile( Controller *controller, QWidget *parent ) : QWidget( parent )
{
  setupUi( this );
  this->controller = controller;

  QVBoxLayout *mainLayoutT = new QVBoxLayout();
  mainLayoutT->addWidget( tlExplanation );
  mainLayoutT->addWidget( tlCSVTemplateImage );
  mainLayoutT->addWidget( tlFileIncorrect );
  mainLayoutT->addWidget( tlFileTexts );
  mainLayoutT->addLayout( numberOfQuestionsLayout );
  mainScrollAreaWidget->setLayout( mainLayoutT );

  tlFileIncorrect->hide();
  tlFileTexts->hide();
  tlNumberOfQuestionsText->hide();
  tlNumberOfQuestions->hide();
  pbAddValues->hide();

  connect( pbMenu, SIGNAL( clicked( bool ) ), this->controller, SLOT( showManageQuestionsMenu() ) );
  connect( pbOpenFile, SIGNAL( clicked( bool ) ), this, SLOT( openFileDialog() ) );
  connect( pbAddValues, SIGNAL( clicked( bool ) ), this, SLOT( addSplitLinesToDatabase() ) );
}

int AddQuestionsPerFile::areFilesOk( void )
{
  if( files.isEmpty() )
    return 1;
  for( int i = 0; i < files.length(); i++ )
  {
    QFile data( files[ i ] );
    if( data.open( QFile::ReadOnly ) )
    {
      QTextStream out( &data );
      bool weiter = true;
      while( weiter )
      {
        if( !out.atEnd() )
        {
          QString line( out.readLine() );
          QStringList lineParts = line.split( ";", QString::SkipEmptyParts );
          if( !lineParts.isEmpty() )
          {
            if( lineParts.length() != 3 && lineParts.length() != 6 )//Eine Zeile muss 3 oder 6 Zellen enthalten
              return 2;
            for( int j = 0; j < lineParts.length(); j++ )//Eine Zelle darf nicht leer sein
              if( lineParts[ j ].isEmpty() )
                return 3;
            lines << line;
          }
        }else
        {
          if( lines.length() % 6 != 0 )
            return 4;
          else if( lines.length() % 6 == 0 )//Es muss immer ein ganzes Paket vorhanden sein (Category,Question,Answer1-4)
          {
            tlNumberOfQuestions->setText( QString::number( lines.length() / 6 ) );
            tlNumberOfQuestions->show();
            tlNumberOfQuestionsText->show();
          }
          weiter = false;
        }
      }
    }
    data.close();
  }
  return 0;
}

void AddQuestionsPerFile::splitLines( void )
{
  for( int i = 0; i < lines.length(); i += 6 )
  {
    for( int j = 0; j < 3; j++)//DE,EN,EE
    {
      categories << lines[ i ].split( ";", QString::SkipEmptyParts )[ j ];
      questions << lines[ i + 1 ].split( ";", QString::SkipEmptyParts )[ j ];

      answers1 << lines[ i + 2 ].split( ";", QString::SkipEmptyParts )[ j ];
      if( lines[ i + 2 ].split( ";", QString::SkipEmptyParts ).length() == 6 )
        explanations1 << lines[ i + 2 ].split( ";", QString::SkipEmptyParts )[ j + 3 ];
      else explanations1 << QString( "" );

      answers2 << lines[ i + 3 ].split( ";", QString::SkipEmptyParts )[ j ];
      if( lines[ i + 3 ].split( ";", QString::SkipEmptyParts ).length() == 6 )
        explanations2 << lines[ i + 3 ].split( ";", QString::SkipEmptyParts )[ j + 3 ];
      else explanations2 << QString( "" );

      answers3 << lines[ i + 4 ].split( ";", QString::SkipEmptyParts )[ j ];
      if( lines[ i + 4 ].split( ";", QString::SkipEmptyParts ).length() == 6 )
        explanations3 << lines[ i + 4 ].split( ";", QString::SkipEmptyParts )[ j + 3 ];
      else explanations3 << QString( "" );

      answers4 << lines[ i + 5 ].split( ";", QString::SkipEmptyParts )[ j ];
      if( lines[ i + 5 ].split( ";", QString::SkipEmptyParts ).length() == 6 )
        explanations4 << lines[ i + 5 ].split( ";", QString::SkipEmptyParts )[ j + 3 ];
      else explanations4 << QString( "" );
    }
  }
}

void AddQuestionsPerFile::openFileDialog( void )
{
  files = QFileDialog::getOpenFileNames( this, tr( "Open file..." ), "", "CSV-file (*.csv)" );
  while( lines.length() > 0 )
    lines.removeLast();
  if( areFilesOk() != 0 )
  {
    qDebug() << "CSV-Datei ist fehlerhaft!";
    tlFileIncorrect->show();
    pbAddValues->hide();
    return;
  }
  splitLines();

  showFileTexts();
}

void AddQuestionsPerFile::showFileTexts( void )
{
  QString fileTexts( "" );
  for( int i = 0; i < lines.length(); i++ )
    fileTexts += ( lines[ i ] + "\r\n" );
  tlFileTexts->show();
  tlFileTexts->setText( fileTexts );
  tlExplanation->hide();
  tlCSVTemplateImage->hide();
  tlFileIncorrect->hide();
  pbOpenFile->hide();
  pbAddValues->show();
}

void AddQuestionsPerFile::addSplitLinesToDatabase( void )
{
  pbAddValues->setEnabled( false );
  for( int q = 0; q < categories.length(); q += 3 )
  {
    //Category
    int categoryId = -1;
    QList< int > categoryIds = controller->getAllCategoryIds();
    //Kategorienamen mit CategoryId abgleichen
    if( !categoryIds.isEmpty() )
      for( int i = 0; i < categoryIds.length(); i++ )
        if( categories[ q + ( controller->getCurrentLanguageId() - 1 ) ] == controller->getCategoryNameAt( categoryIds[ i ] ) )
        {
          categoryId = categoryIds[ i ];
          i = categories.length();
        }
    if( categoryId == -1 )
    {
      categoryId = controller->addCategory();
      controller->setDECategoryText( categoryId, categories[ q ] );
      controller->setENCategoryText( categoryId, categories[ q + 1 ] );
      controller->setEECategoryText( categoryId, categories[ q + 2 ] );
    }

    //Questions
    int questionId = controller->addQuestion( categoryId );
    controller->setDEQuestionText( questionId, questions[ q ] );
    controller->setENQuestionText( questionId, questions[ q + 1 ] );
    controller->setEEQuestionText( questionId, questions[ q + 2 ] );

    //Answers1
    int answer1Id = controller->addAnswer();
    controller->setDEAnswerText( answer1Id, answers1[ q ], explanations1[ q ] );
    controller->setENAnswerText( answer1Id, answers1[ q + 1 ], explanations1[ q + 1 ] );
    controller->setEEAnswerText( answer1Id, answers1[ q + 2 ], explanations1[ q + 2 ] );
    controller->setQuestionAnswer( answer1Id, questionId, !explanations1[ q ].isEmpty() );

    //Answers2
    int answer2Id = controller->addAnswer();
    controller->setDEAnswerText( answer2Id, answers2[ q ], explanations2[ q ] );
    controller->setENAnswerText( answer2Id, answers2[ q + 1 ], explanations2[ q + 1 ] );
    controller->setEEAnswerText( answer2Id, answers2[ q + 2 ], explanations2[ q + 2 ] );
    controller->setQuestionAnswer( answer2Id, questionId, !explanations2[ q ].isEmpty() );

    //Answers3
    int answer3Id = controller->addAnswer();
    controller->setDEAnswerText( answer3Id, answers3[ q ], explanations3[ q ] );
    controller->setENAnswerText( answer3Id, answers3[ q + 1 ], explanations3[ q + 1 ] );
    controller->setEEAnswerText( answer3Id, answers3[ q + 2 ], explanations3[ q + 2 ] );
    controller->setQuestionAnswer( answer3Id, questionId, !explanations3[ q ].isEmpty() );

    //Answers4
    int answer4Id = controller->addAnswer();
    controller->setDEAnswerText( answer4Id, answers4[ q ], explanations4[ q ] );
    controller->setENAnswerText( answer4Id, answers4[ q + 1 ], explanations4[ q + 1 ] );
    controller->setEEAnswerText( answer4Id, answers4[ q + 2 ], explanations4[ q + 2 ] );
    controller->setQuestionAnswer( answer4Id, questionId, !explanations4[ q ].isEmpty() );
  }

  //Fenster reset
  controller->showAddQuestionsPerFile();
}
