/***************************************************************************
                          klatingrammar.cpp  -  description
                             -------------------
    begin                : Thu Jul 17
    copyright            : (C) 2001-2004 by George Wright
    email                : gwright@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 <qfile.h>
#include <qlabel.h>

#include <kapplication.h>
#include <kcombobox.h>
#include <klineedit.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>

#include <stdlib.h>
#include <time.h>

#include "klatingrammar.h"

KLatinGrammar::KLatinGrammar(QWidget* parent, const char *name)
	: KLatinGrammarWidget(parent, name)
{
	// Set defaults
	m_total = 0;
	m_totalCorrect = 0;
	m_numWrong = 0;

	// Set connections
	connect(BackButton, SIGNAL(clicked()), this, SLOT(goBack()));
	connect(OKButton, SIGNAL(clicked()), this, SLOT(checkAnswer()));
	connect(AnswerBox, SIGNAL(returnPressed()), this, SLOT(checkAnswer()));
	connect(ChooseSection, SIGNAL(activated(int)), this, SLOT(loadDatabase()));

	// Insert the various vocab options
	ChooseSection->insertItem(i18n("1st Feminine"));
	ChooseSection->insertItem(i18n("2nd Masculine"));
	ChooseSection->insertItem(i18n("2nd Neuter"));
	ChooseSection->insertItem(i18n("3rd Masc/Fem"));
	ChooseSection->insertItem(i18n("3rd Neuter"));
	ChooseSection->insertItem(i18n("4th Masc/Fem"));
	ChooseSection->insertItem(i18n("4th Neuter"));
	ChooseSection->insertItem(i18n("5th Feminine"));

	// Load the database of nouns
	loadDatabase();
}

KLatinGrammar::~KLatinGrammar()
{
}

void KLatinGrammar::loadDatabase()
{
	// Various files that are used for each declension
	QString file;
	if (ChooseSection->currentItem() == 0)
		file = "klatin/data/grammar/puella.klat";
	if (ChooseSection->currentItem() == 1)
		file = "klatin/data/grammar/servus.klat";
	if (ChooseSection->currentItem() == 2)
		file = "klatin/data/grammar/bellum.klat";
	if (ChooseSection->currentItem() == 3)
		file = "klatin/data/grammar/rex.klat";
	if (ChooseSection->currentItem() == 4)
		file = "klatin/data/grammar/opus.klat";
	if (ChooseSection->currentItem() == 5)
		file = "klatin/data/grammar/gradus.klat";
	if (ChooseSection->currentItem() == 6)
		file = "klatin/data/grammar/genu.klat";
	if (ChooseSection->currentItem() == 7)
		file = "klatin/data/grammar/res.klat";

	// Check whether the file actually exists
	QFile check(locate("data", file));

	// If not - warn user.
	if (check.exists() == FALSE) {
		check.close();
		KMessageBox::error(0, i18n("Could not load grammar-definition file, check KLatin installation."), i18n("Error with KLatin"));
		QuestionLabel->setText(i18n("Error."));
		return;
	}

	KEduVocDataItemList grammar = KEduVocData::parse(locate("data", file));

	// Load XML file into memory
	int numberOfWords = 0;
	for(KEduVocDataItemList::Iterator dataIt = grammar.begin(); dataIt != grammar.end(); dataIt++) {
		m_noun[numberOfWords] = grammar[numberOfWords].originalText();
		numberOfWords++;
	}

	nextQuestion();
}

void KLatinGrammar::goBack()
{
	emit exited();
	close();
}

void KLatinGrammar::nextQuestion()
{
	// Seed the random number generator
	srandom(time(NULL));

	m_grammarNumber = (random() % 11) + 1;

	QString questionString;
	
	// Question string...
	switch (m_grammarNumber) {
		case 1: 
			questionString = i18n("What is the nominative singular of %1?").arg(m_noun[0]);
			break;
		case 2: 
			questionString = i18n("What is the vocative singular of %1?").arg(m_noun[0]);
			break;
		case 3: 
			questionString = i18n("What is the accusative singular of %1?").arg(m_noun[0]);
			break;
		case 4: 
			questionString = i18n("What is the genitive singular of %1?").arg(m_noun[0]);
			break;
		case 5: 
			questionString = i18n("What is the dative singular of %1?").arg(m_noun[0]);
			break;
		case 6: 
			questionString = i18n("What is the ablative singular of %1?").arg(m_noun[0]);
			break;
		case 7: 
			questionString = i18n("What is the nominative plural of %1?").arg(m_noun[0]);
			break;
		case 8: 
			questionString = i18n("What is the vocative plural of %1?").arg(m_noun[0]);
			break;
		case 9: 
			questionString = i18n("What is the accusative plural of %1?").arg(m_noun[0]);
			break;
		case 10: 
			questionString = i18n("What is the genitive plural of %1?").arg(m_noun[0]);
			break;
		case 11: 
			questionString = i18n("What is the dative plural of %1?").arg(m_noun[0]);
			break;
		case 12: 
			questionString = i18n("What is the ablative plural of %1?").arg(m_noun[0]);
			break;
	}
	
	// Set question on screen
	QuestionLabel->setText(questionString);
}

void KLatinGrammar::checkAnswer()
{
	if (AnswerBox->text() == "") {
		KMessageBox::information(0, i18n("Inform the user that nothing has been typed in", "Please enter an answer."), i18n("Error"));
	}
	
	if (m_noun[m_grammarNumber - 1] != AnswerBox->text().lower()) {
		m_total++;
		m_numWrong++;

		// Allow a tolerance of 1 retry
		if (m_numWrong == 2) {
			m_numWrong = 0;
			KMessageBox::information(0, i18n("The correct answer was %1.").arg(m_noun[m_grammarNumber - 1]), i18n("Wrong"));
			nextQuestion();
		}

		AnswerBox->setText(NULL);
	}
	if (m_noun[m_grammarNumber - 1] == AnswerBox->text().lower()) {
		m_totalCorrect++;
		m_total++;

		// Reset the number of questions that were wrong
		m_numWrong = 0;
		AnswerBox->setText(NULL);

		nextQuestion();
	}

	updateScores();
}

void KLatinGrammar::updateScores()
{
	// Update the score labels
	Score1Label->setNum(m_totalCorrect);
	Score2Label->setNum(m_total);

	// Set percentage
	QString temp;
	temp.setNum(int(m_totalCorrect / m_total * 100));
	temp.insert(temp.length(), "%");
	PercentageLabel->setText(temp);
}

#include "klatingrammar.moc"
