#include "NumericQuestion.h"
#include <iostream>
#include <QApplication>
#include <QLabel>
#include <QMessageBox>
#include <QTimer>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QFont>
#include <QSound>
#include <QFormLayout>

NumericQuestion::NumericQuestion(const QString& duration, QPushButton *bottonContinueE) :
    Question(duration) {
    botonContinue = bottonContinueE;
}

//Lectura del XML
bool NumericQuestion::loadFromNode(const QDomElement& element){
    for ( QDomNode child = element.firstChild(); ! child.isNull(); child = child.nextSibling() )   {
        QDomElement element = child.toElement();
        if ( element.isNull() ) continue;
        if ( element.tagName() == "text" )
            text = element.text();
        else if ( element.tagName() == "answer" )
            answer = element.text();
    }
    return true;
}

//Retorna La Pregunta NO UTILIZADO
QString NumericQuestion::print() const{
    return text;
}


//Aplicar La Pregunta
bool NumericQuestion::aplicar(int &userScoreE) {

    //QFormLayout *layoutPrincipal = new QFormLayout;
    //layoutPrincipal->addRow(trUtf8("Nickname"), campoUsuario);
    //setLayout(layoutPrincipal);

    userScore = &userScoreE;

    //Creacion de variables internas
    //QWidget *window = new QWidget;
    QLabel * labelQuestion = new QLabel(text);
    QHBoxLayout * layoutH = new QHBoxLayout();
    QFormLayout *layoutPrincipal = new QFormLayout;

    //Asignacion de variables del .h
    timer = new QTimer(this);
    timerLabel = new QTimer(this);
    labelAnswer = new QLabel();
    labelTimer = new QLabel(stringLabelTime.setNum(durationQuestion));
    mainLayout = new QVBoxLayout(this);
    userAnswerV = new QLineEdit("");
    botonOk = new QPushButton("Accept");
    botonContinue = new QPushButton("Continue");
    labelScore = new QLabel(tempScore.setNum(*userScore));

    //Estilo
    botonOk->setStyleSheet(QString::fromUtf8("background-color: rgb(0, 85, 127);\n" "color: rgb(255, 255, 255);"));
    botonContinue->setStyleSheet(QString::fromUtf8("background-color: rgb(0, 85, 127);\n" "color: rgb(255, 255, 255);"));
    this->resize(600, 200);
    //window->setWindowTitle("Numeric Question");
    this->setStyleSheet(QString::fromUtf8("background-color: rgb(170, 170, 127);\n" "color: rgb(0, 0, 0);\n" "font: 8pt \"Trebuchet MS\";"));

    //Fuente de Letra
    QFont font;
    font.setPointSize(10);
    font.setBold(true);
    font.setItalic(true);
    font.setWeight(75);
    labelAnswer->setFont(font);

    //Alineaciones de Etiquetas
    labelAnswer->setAlignment(Qt::AlignCenter);
    labelQuestion->setAlignment(Qt::AlignCenter);

    //Insertar all mainLayout
    mainLayout->addWidget(labelQuestion);
    mainLayout->addWidget(userAnswerV);
    layoutPrincipal->addRow(trUtf8("Puntaje"), labelScore);
    layoutPrincipal->addRow(trUtf8("Tiempo Restante"), labelTimer);
    mainLayout->addLayout(layoutPrincipal);
    mainLayout->addWidget(labelAnswer);
    layoutH->addWidget(botonOk);
    layoutH->addWidget(botonContinue);
    mainLayout->addLayout(layoutH);

    //Inicio de los timer
    timer->start(1000*durationQuestion); //Duracion Total
    timerLabel->start(500); //Actualizacion por segundo del LabelTimer

    //Conexiones
    connect( timerLabel, SIGNAL(timeout()), this, SLOT(remainingTimeAnswer()));
    connect( timer, SIGNAL(timeout()), this, SLOT(check()));
    //connect( botonContinue, SIGNAL(clicked()), this, SLOT(close()) );
    connect( botonOk, SIGNAL(clicked(bool)), this, SLOT(check()));

    //Habilitar o deshabilitar Botones y etiquetas
    botonOk->setEnabled(true);
    botonContinue->setDisabled(true);
    labelAnswer->setHidden(true);
    labelQuestion->setStyleSheet(QString::fromUtf8("font: 75 12pt \"Ubuntu\";\n" "color: rgb(255, 255, 255);\n" "background-color: rgb(0, 85, 127);\n"));

    //window->show();

    //QApplication::exec() ;

    return true;
}

//Cuando se preciona ok o el tiempo se acaba
void NumericQuestion::check(){

    //Detencion de los timer
    timer->stop();
    timerLabel->stop();

    //Sonidos
    QSound *bells = new QSound("sounds/applause.wav");
    QSound *error = new QSound("sounds/error.wav");

    //Pasar a entero la respuesta
    int userAnswer = userAnswerV->displayText().toInt();

    //Comparacion con la respuesta
    if (userAnswer == answer.toInt()){
        //Estilo verde de correcto
        userAnswerV->setStyleSheet(QString::fromUtf8("color: rgb(255, 255, 255); background-color: rgb(0, 170, 0);"));
        bells->play();
        *userScore += 10;
        labelScore->setText(tempScore.setNum(*userScore));
    }
    else {
        //Muestra la respuesta correcta y pone estilo rojo y verde
        labelAnswer->setText(answer);
        userAnswerV->setStyleSheet(QString::fromUtf8("color: rgb(255, 255, 255); background-color: rgb(255, 0, 0);"));
        labelAnswer->setStyleSheet(QString::fromUtf8("color: rgb(255, 255, 255); background-color: rgb(0, 170, 0);"));
        error->play();
    }

    //Activa Continue y desactiva OK y el labelAnswer
    labelAnswer->setHidden(false);
    botonContinue->setEnabled(true);
    botonOk->setDisabled(true);

}

//Actualizacion del la etiqueta de labelTimer
void NumericQuestion::remainingTimeAnswer(){
    stringLabelTime.setNum((timer->remainingTime()/1000));
    labelTimer->setText(stringLabelTime);
}
