#include "question_item.h"

QuestionItem::QuestionItem()
{
     q_name = "";
     q_flag = -1;
     q_group = "";
     q_difficulty = 0;
     q_text = "";
     q_answers << "" << "false" << "" << "false" << "" << "false" << "" << "false";
     q_answer = None;
}

QuestionItem::QuestionItem(QString name)
{
     q_name = name;
     q_flag = -1;
     q_group = "";
     q_difficulty = 0;
     q_text = "";
     q_answers << "" << "false" << "" << "false" << "" << "false" << "" << "false";
     q_answer = None;
}

QuestionItem::QuestionItem(QString name, int flag, QString group, int difficulty, QString text, QStringList answers)
{
     q_name = name;
     q_flag = flag;
     q_group = group;
     q_difficulty = difficulty;
     setText(text);
     q_answers = answers;
     q_answer = None;
}

QString QuestionItem::name() { return q_name; }

int QuestionItem::flag() { return q_flag; }

QString QuestionItem::group() { return q_group; }

int QuestionItem::difficulty() { return q_difficulty; }

QString QuestionItem::text() { return q_text; }

QString QuestionItem::ansA() { return q_answers.at(0); }

QString QuestionItem::ansB() { return q_answers.at(2); }

QString QuestionItem::ansC() { return q_answers.at(4); }

QString QuestionItem::ansD() { return q_answers.at(6); }

bool QuestionItem::isAnsACorrect()
{
     if (q_answers.at(1) == "true") {return true;} else {return false;}
}

bool QuestionItem::isAnsBCorrect()
{
     if (q_answers.at(3) == "true") {return true;} else {return false;}
}

bool QuestionItem::isAnsCCorrect()
{
     if (q_answers.at(5) == "true") {return true;} else {return false;}
}

bool QuestionItem::isAnsDCorrect()
{
     if (q_answers.at(7) == "true") {return true;} else {return false;}
}

QStringList QuestionItem::answers() { return q_answers; }

void QuestionItem::setName(QString name) { q_name = name; }

void QuestionItem::setFlag(int flag) { q_flag = flag; }

void QuestionItem::setGroup(QString group) { q_group = group; }

void QuestionItem::setDifficulty(int difficulty) { q_difficulty = difficulty; }

void QuestionItem::setText(QString text)
{
	QTextDocument doc; doc.setHtml(text); QString final = text; QString lastgood; QTextDocument testdoc;
	QStringList before; QString after = "font-size:10pt;";
	before << "font-size:8.25pt;" << "font-size:8pt;" << "font-size:9pt;";
	for (int b = 0; b < before.count(); ++b) {
		int skip = 0; int c = text.count(before.at(b), Qt::CaseInsensitive);
		for (int i = 0; i < c; ++i) {
			lastgood = final;
			if (final.contains(before.at(b), Qt::CaseInsensitive)) {
				final.replace(final.indexOf(before.at(b), skip), before.at(b).count(), after);
				testdoc.setHtml(final);
				if (doc.toPlainText() != testdoc.toPlainText()) {
					skip = final.indexOf(before.at(b), skip) + 10;
					final = lastgood;
				}
			}
		}
	}
	q_text = final;
}

void QuestionItem::setAnsA(QString ans) { q_answers.replace(0, ans); }

void QuestionItem::setAnsB(QString ans) { q_answers.replace(2, ans); }

void QuestionItem::setAnsC(QString ans) { q_answers.replace(4, ans); }

void QuestionItem::setAnsD(QString ans) { q_answers.replace(6, ans); }

void QuestionItem::setAnsACorrect(bool correct)
{
     if (correct) {q_answers.replace(1, "true");} else {q_answers.replace(1, "false");}
}

void QuestionItem::setAnsBCorrect(bool correct)
{
     if (correct) {q_answers.replace(3, "true");} else {q_answers.replace(3, "false");}
}

void QuestionItem::setAnsCCorrect(bool correct)
{
     if (correct) {q_answers.replace(5, "true");} else {q_answers.replace(5, "false");}
}

void QuestionItem::setAnsDCorrect(bool correct)
{
     if (correct) {q_answers.replace(7, "true");} else {q_answers.replace(7, "false");}
}

void QuestionItem::setAnswers(QStringList answers) { q_answers = answers; }

bool QuestionItem::hasCorrectAnswer()
{
    if ((q_answers.at(1) == "true") || (q_answers.at(3) == "true") ||
        (q_answers.at(5) == "true") || (q_answers.at(7) == "true"))
    { return true; }
    return false;
}

QuestionItem::Answer QuestionItem::answered() { return q_answer; }

void QuestionItem::setAnswered(Answer ans) { q_answer = ans; }

bool QuestionItem::isAnsweredCorrectly()
{
    if (hasCorrectAnswer()) {
        switch (q_answer) {
            case None: return false; break;
            case A: return (isAnsACorrect()); break;
            case B: return (isAnsBCorrect()); break;
            case C: return (isAnsCCorrect()); break;
            case D: return (isAnsDCorrect()); break;
        }
    } else {
        switch (q_answer) {
            case None: return true; break;
            case A: return false; break;
            case B: return false; break;
            case C: return false; break;
            case D: return false; break;
        }
    }
    return false;
}