#include "triviaapp.h"
#include <QDomDocument>
#include <QFile>
#include <QMessageBox>
#include <iostream>
#include "question.h"
#include "singlechoice.h"
#include "window.h"

TriviaApp::TriviaApp(Window * window)
    : score(0)
    , questionsCounter(0)
    , correctCounter(0)
    , window(window)

{
      srand(0);
   loadQuestions();
}

TriviaApp::~TriviaApp()
{
  qDeleteAll(questions);
}

int TriviaApp::exec()
{
  if ( ! loadQuestions() ) return 1;

  printQuestions();

  return 0;
}

inline bool showError(const QString& message)
{
  QMessageBox::critical(0, "Error", message);
  return false;
}

bool TriviaApp::loadQuestions()
{
  QFile file(triviaFilename);
  if ( ! file.open(QIODevice::ReadOnly | QIODevice::Text) )
     return showError( QString("Could not open %1").arg(triviaFilename) );

  QDomDocument document("trivia");
  QString errorMsg;
  int errorLine = 0;
  int errorColumn = 0;
  if ( ! document.setContent(&file, &errorMsg, &errorLine, &errorColumn) )
  {
     file.close();
     return showError(QString("%1:%2.%3: %4")
        .arg(triviaFilename).arg(errorLine).arg(errorColumn).arg(errorMsg));
  }
  file.close();

  QDomElement triviaElement = document.documentElement();
  //std::cout << qPrintable(triviaElement.tagName());
  for( QDomNode node = triviaElement.firstChild(); ! node.isNull(); node = node.nextSibling() )
  {
     //QDomElement element = node.toElement(); // try to convert the node to an element.
     if( node.isNull() || node.nodeName() != "question" ) continue;

     //Question* question = createQuestion(element.attribute("type"));

     Question* question = createQuestion(node.toElement().attribute("type"));
     if ( ! question->loadFromNode(node) )
     {
        // Hubo una pregunta mala. Que hacer con ella?
        delete question;
        continue;
     }

     questions.append(question);
  }

  return true;
}

void TriviaApp::printQuestions() const
{
  //for ( int i = 0; i < questions.count(); ++i )
     //questions[i]->print();
}

Question* TriviaApp::createQuestion(const QString& type)
{
  if ( type == "single_choice" ) return new SingleChoice();
  //if ( type == "numeric" ) return new NumericQuestion();
  //...
  return new Question(type);
}


void TriviaApp::decideMode(int index)
{
    switch(index)
    {
        case 0:
            std::cout<<"se escogio solo"<<std::endl;
            break;
        case 1:
            std::cout<<"se escogio multijugador"<<std::endl;
    }
}

QString TriviaApp::getNewQuestion(int index)
{
    //int index = rand() % questions.size();
    actQuestion = questions.at(index);
    //actQuestion->print();
    return actQuestion->getText();
}

int TriviaApp::getScore() const
{
    return score;
}



void TriviaApp::getName(const QString &name)
{
    username = name;
}


bool TriviaApp::isCorrect()
{
    if(userAnswer==actQuestion->getAnswer())
    {
        score+=10;
        return true;
    }
    return false;
}


void TriviaApp::getAnswerLine( const QString &answer)
{
    userAnswer = answer;

    std::cout<<userAnswer.toStdString()<<std::endl;
}
