#include "SingleChoiceQuestion.h"
#include <iostream>
#include <QApplication>
#include <QLabel>
#include <QRadioButton>
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QFormLayout>
#include <QGroupBox>
#include <QTimer>
#include <QSound>

//Constructor
SingleChoiceQuestion::SingleChoiceQuestion(const QString& duration, QPushButton *bottonContinueE) :
    Question(duration) {
    pushButton_2 = bottonContinueE;
}

//Lectura del XML
bool SingleChoiceQuestion::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();
        else if ( element.tagName() == "options" ){
            for ( QDomNode child2 = element.firstChild(); ! child2.isNull(); child2 = child2.nextSibling() )   {
                QDomElement element2 = child2.toElement();
                if ( element2.isNull() ) continue;
                if ( element2.tagName() == "option" )
                    opciones.append(element2.text());
            }
        }
    }
    return true;
}

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

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

    QString tempScore;
    userScore = &userScoreE;

    //Creacion de punteros a variables
    QWidget * Form = new QWidget(this);
    QWidget *verticalLayoutWidget;
    QVBoxLayout *verticalLayout;
    QLabel *label;
    QGridLayout *gridLayout;
    QWidget *formLayoutWidget;
    QFormLayout *formLayout, *layoutPrincipal;

    //Cracion de variables
    formLayoutWidget = new QWidget(Form);
    formLayout = new QFormLayout(formLayoutWidget);
    layoutPrincipal = new QFormLayout();
    verticalLayoutWidget = new QWidget(Form);
    verticalLayout = new QVBoxLayout(verticalLayoutWidget);
    label = new QLabel(verticalLayoutWidget);
    gridLayout = new QGridLayout();
    labelScore = new QLabel(tempScore.setNum(*userScore));

    //Inicializacion de variables del .h
    pushButton_2 = new QPushButton(formLayoutWidget);
    pushButton = new QPushButton(formLayoutWidget);
    timer = new QTimer(this);
    timerLabel = new QTimer(this);
    labelTimer = new QLabel(stringLabelTime.setNum(durationQuestion));

    //Formatos y nombres
    Form->resize(600, 200);
    Form->setWindowTitle(QApplication::translate("Form", "Single Choice Question", 0));
    formLayoutWidget->setObjectName(QString::fromUtf8("formLayoutWidget"));
    formLayoutWidget->setGeometry(QRect(390, 150, 178, 31));
    formLayout->setObjectName(QString::fromUtf8("formLayout"));
    formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
    formLayout->setContentsMargins(0, 0, 0, 0);
    pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
    pushButton_2->setStyleSheet(QString::fromUtf8("background-color: rgb(0, 85, 127);\n" "color: rgb(255, 255, 255);"));
    pushButton->setObjectName(QString::fromUtf8("pushButton"));
    pushButton->setStyleSheet(QString::fromUtf8("background-color: rgb(0, 85, 127);\n" "color: rgb(255, 255, 255);"));
    verticalLayoutWidget->setObjectName(QString::fromUtf8("verticalLayoutWidget"));
    verticalLayoutWidget->setGeometry(QRect(9, 9, 561, 129));
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    verticalLayout->setContentsMargins(0, 0, 0, 0);
    label->setObjectName(QString::fromUtf8("label"));
    gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
    pushButton->setText(QApplication::translate("Form", "Accept", 0));
    pushButton_2->setText(QApplication::translate("Form", "Continue", 0));
    Form->setStyleSheet(QString::fromUtf8("background-color: rgb(170, 170, 127);\n" "color: rgb(0, 0, 0);\n" "font: 8pt \"Trebuchet MS\";"));


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

    //Agregar al Layout
    formLayout->setWidget(0, QFormLayout::FieldRole, pushButton_2);
    formLayout->setWidget(0, QFormLayout::LabelRole, pushButton);
    verticalLayout->addWidget(label);
    verticalLayout->addLayout(gridLayout);
    formLayoutWidget->raise();
    verticalLayoutWidget->raise();
    pushButton->raise();


    for ( int i = 0; i < opciones.size(); ++i ){
        QRadioButton * radio = new QRadioButton(QString(opciones[i]));
        m_radios <<  radio;
        gridLayout->addWidget( radio,i,1);
     }
    layoutPrincipal->addRow(trUtf8("Puntaje"), labelScore);
    layoutPrincipal->addRow(trUtf8("Tiempo Restante"), labelTimer);
    verticalLayout->addLayout(layoutPrincipal);

     //Setea el primer radio con true
     m_radios[0]->setChecked(true);


     pushButton_2->setDisabled(true);

     //Setea la pregunta
     label->setText(text);
     label->setStyleSheet(QString::fromUtf8("font: 75 12pt \"Ubuntu\";\n color: rgb(255, 255, 255);\n" "background-color: rgb(0, 85, 127);\n"));

     //Timer Inicializacion
     timer->start(durationQuestion*1000);
     timerLabel->start(500);

     //Conexiones para los botones de ok y siguiente
     connect(pushButton, SIGNAL(pressed()), this , SLOT(check()));
     //connect(pushButton_2, SIGNAL(pressed()), Form, SLOT(close()));
     QMetaObject::connectSlotsByName(Form);

     //Conexiones de los timer
     connect(timer, SIGNAL(timeout()), pushButton , SLOT(click()));
     connect(timerLabel, SIGNAL(timeout()), this , SLOT(remainingTimeAnswer()));

     //Form->show();

     //QApplication::exec();

     return true;
}

void SingleChoiceQuestion::check(){

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

    for ( int i = 0; i < opciones.size(); ++i ){
        if(m_radios[i]->isChecked()){
            if (i+1 == answer.toInt()){
                m_radios[i]->setStyleSheet(QString::fromUtf8("color: rgb(255, 255, 255);\n background-color: rgb(0, 170, 0);"));
                bells->play();
                *userScore += 10;
                labelScore->setText(tempScore.setNum(*userScore));
            }else{
                m_radios[i]->setStyleSheet(QString::fromUtf8("color: rgb(255, 255, 255);\n background-color: rgb(255, 0, 0);"));
                m_radios[answer.toInt()-1]->setStyleSheet(QString::fromUtf8("color: rgb(255, 255, 255);\n background-color: rgb(0, 170, 0);"));
                error->play();
            }

        }
    }
    pushButton->setDisabled(true);
    pushButton_2->setEnabled(true);
    timer->stop();
    timerLabel->stop();
}

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