/***************************************************************************
    Copyright 2009 Frederik Gladhorn <gladhorn@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "multiplechoicemodewidget.h"
#include "multiplechoicedata.h"
#include "latexrenderer.h"

#include "ui_practice_widget_multiplechoice.h"

#include <KDebug>
#include <kcolorscheme.h>
#include <QtGui/QRadioButton>
#include <QTimer>
#include <QKeyEvent>

using namespace Practice;


MultiplechoiceModeWidget::MultiplechoiceModeWidget (GuiFrontend *frontend, QWidget* parent )
    : AbstractModeWidget (frontend, parent), m_latexRenderer(0)
{
    m_ui = new Ui::MultiplechoicePracticeWidget();
    m_ui->setupUi(this);

    for (int i = 0; i < 5; i++) {
        QAction *action = new QAction(this);
        action->setShortcut(Qt::Key_1+i);
        addAction(action);
        m_actions.append(action);
    }

}

void MultiplechoiceModeWidget::setQuestion(const QVariant& question)
{
    if (!question.canConvert<MultipleChoiceData>()) {
        kWarning() << "expected MultipleChoiceData";
        return;
    }
    MultipleChoiceData data = question.value<MultipleChoiceData>();

    m_ui->questionLabel->setMinimumSize(QSize(0, 0));
    if (LatexRenderer::isLatex(data.question)) {
        if(!m_latexRenderer) {
            m_latexRenderer = new LatexRenderer(this);
            m_latexRenderer->setResultLabel(m_ui->questionLabel);
        }
        m_latexRenderer->renderLatex(data.question);
    } else {
        m_ui->questionLabel->setText(data.question);
    }

    if (m_choiceButtons.size() != data.choices.size()){
        qDeleteAll(m_choiceButtons);
        m_choiceButtons.clear();
        setNumberOfRadioButtons(data.choices.size());
    }

    int j = 0;
    foreach(QRadioButton *radio,m_choiceButtons){
        radio->setText(data.choices[j]);
        radio->setToolTip(data.choices[j]);
        j++;
    }
}

void MultiplechoiceModeWidget::showQuestion()
{
    //necessary trick to uncheck'em all
    m_choiceButtons[0]->setChecked(true);
    m_choiceButtons[0]->setAutoExclusive(false);
    m_choiceButtons[0]->setChecked(false);
    m_choiceButtons[0]->setAutoExclusive(true);

    m_ui->feedbackLabel->clear();

    foreach(QRadioButton *radio, m_choiceButtons) {
        radio->setPalette(palette());
        radio->setEnabled(true);
    }


    QTimer::singleShot(0, m_choiceButtons[0], SLOT(setFocus()));
}

void MultiplechoiceModeWidget::setNumberOfRadioButtons(const int numberOfChoices)
{
    for (int i=0;i<numberOfChoices;i++){
        QRadioButton *radio_button = new QRadioButton(this);
        m_ui->verticalLayout->insertWidget(m_ui->verticalLayout->count()-1, radio_button);
        m_choiceButtons.append(radio_button);
        if (i < 5) {
            connect(m_actions.at(i), SIGNAL(triggered()), radio_button, SLOT(click()));
        }
        connect(radio_button, SIGNAL(clicked()), this, SIGNAL(continueAction()));
        radio_button->installEventFilter(this);
    }
}

void MultiplechoiceModeWidget::setSynonym(const QString& entry)
{
  //TODO Do something here to show synonyms
}


void MultiplechoiceModeWidget::showSynonym()
{
  //TODO Do something here to show synonyms
}

bool MultiplechoiceModeWidget::eventFilter(QObject *obj, QEvent *event)
{
    // make it possible to use the enter key to select multiple choice options
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) {
            QRadioButton *radioButton = qobject_cast<QRadioButton*>(obj);
            if (radioButton) {
                radioButton->click();
                return true;
            }
        }
    }

    return QObject::eventFilter(obj, event);
}

void MultiplechoiceModeWidget::setSolution(const QVariant& solution)
{
    m_solution = solution.toInt();
}

void MultiplechoiceModeWidget::setHint(const QVariant& hint)
{
    m_choiceButtons.at(hint.toInt())->setEnabled(false);
}

void MultiplechoiceModeWidget::setFeedback(const QVariant& feedback)
{
    m_ui->feedbackLabel->setText(feedback.toString());
}

void MultiplechoiceModeWidget::showSolution()
{
    int input = -1;
    if (userInput().isValid()) {
        input = userInput().toInt();
    }
    m_choiceButtons[m_solution]->setPalette(m_correctPalette);
    if (input != -1 && input != m_solution) {
        m_choiceButtons[input]->setPalette(m_wrongPalette);
    }
    foreach(QRadioButton *radio, m_choiceButtons) {
        radio->setEnabled(false);
    }
}

QVariant MultiplechoiceModeWidget::userInput()
{

    int i = 0;
    foreach(QRadioButton *radio,m_choiceButtons){
        if (radio->isChecked()) return i;
        i++;
    }

    return QVariant();
}

void MultiplechoiceModeWidget::setQuestionSound(const KUrl& soundUrl)
{

}

void MultiplechoiceModeWidget::setSolutionSound(const KUrl& soundUrl)
{

}

void MultiplechoiceModeWidget::setSolutionPronunciation(const QString& pronunciationText)
{

}

void MultiplechoiceModeWidget::setQuestionPronunciation(const QString& pronunciationText)
{

}

#include "multiplechoicemodewidget.moc"
