/************************************
 * InterGlos Basic, version 1.2.1.6 *
 *                                  *
 *           © 2005 Jocke Andersson *
 ************************************/

#include <iostream>
#include <string> // MS VC++ fixa rad 165, "next" -> "bump". Annars buggar programmet totalt.
#include <vector>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

struct Glosa {
	string sve,eng;
};

void mataIn(vector<Glosa>& lista);
void glostest(vector<Glosa> lista);
void skrivUt(vector<Glosa> lista);
void sparaFil(vector<Glosa> lista);
void hamtaFil(vector<Glosa>& lista);
void tomLista(vector<Glosa>& lista);

int main(void) {
	vector<Glosa> gloslista;
	bool fortsatt = true;
	int val;

	srand(time(0));
	while (fortsatt) {
		system("cls");
		cout << "InterGlos Basic - Meny" << endl;
		cout << "======================" << endl;
		cout << endl;
		cout << "1. Mata in ord till svensk-engelsk gloslista" << endl;
		cout << "2. Glostest" << endl;
		cout << "3. Skriv ut gloslista" << endl;
		cout << "4. Spara gloslista till fil" << endl;
		cout << "5. H\x84mta gloslista fr\x86n fil" << endl;
		cout << "6. T\x94m lista" << endl;
		cout << "7. Avsluta" << endl;
		cout << endl << "Val: ";
		cin >> val;

		system("cls");
		
		switch (val) {
			case 1:
				mataIn(gloslista);
				break;
			case 2:
				glostest(gloslista);
				break;
			case 3:
				skrivUt(gloslista);
				break;
			case 4:
				sparaFil(gloslista);
				break;
			case 5:
				hamtaFil(gloslista);
				break;
			case 6:
				tomLista(gloslista);
				break;
			case 7:
				fortsatt = false;
				break;
			default:
				cout << "Felaktigt val!" << endl;
				break;
		}
		if (fortsatt) {
			cout << endl;
			system("pause");
		}
	}
	return 0;
}


void mataIn(vector<Glosa>& lista) {
	Glosa inGlos;

	cout << "InterGlos Basic - Mata in glosor" << endl;
	cout << "================================" << endl;
	cout << endl;
	
	cout << "Mata in glosor, avsluta genom att inte skriva n\x86got." << endl << endl;
	inGlos.eng = ""; //just in case...
	cout << "Svenskt ord/fras: ";
	cin.ignore(1000,'\n');
	getline(cin,inGlos.sve);
	while (inGlos.sve != "") {
		cout << "\"" << inGlos.sve << "\" p\x86 engelska: ";
		getline(cin,inGlos.eng);
		if (inGlos.eng != "") {
			lista.push_back(inGlos);
			cout << endl << "Svenskt ord/fras: ";
			getline(cin,inGlos.sve);
		} else {
			inGlos.sve = "";
		}
	}
	cout << endl << endl << "Inmatning f\x84rdig." << endl;
}

void glostest(vector<Glosa> lista) {
	vector<Glosa> testlist = lista; //används för att kunna sålla bort testade frågor.
	vector<Glosa>::iterator it;
	vector<Glosa> fellista; //använder sve som svar, för att slippa definiera en till struct.
	Glosa felGlos;
	string inString;
	int i, ratt = 0; //antalet fel fås från fellistan.
	
	cout << "InterGlos Basic - Glostest" << endl;
	cout << "==========================" << endl;
	cout << endl;
	
	if (lista.empty()) {
		cout << "Gloslistan \x84r tom!" << endl;
	} else {
		cin.ignore(1000,'\n');
		while (!testlist.empty()) {
			i = rand() % testlist.size();
			cout << "Vad heter \"" << testlist.at(i).sve << "\" p\x86 engelska?" << endl;
			do {
				cout << "Ditt svar: ";
				getline(cin,inString);
			} while (inString == ""); //förhindra tomt svar av misstag.
			if (inString == testlist.at(i).eng) {
				cout << "R\x84tt!" << endl;
				ratt++;
			} else {
				cout << "Tyv\x84rr, \"" << inString << "\" \x84r fel svar!" << endl;
				cout << "Det korrekta svaret \x84r \"" << testlist.at(i).eng << "\"." << endl;
				felGlos.sve = inString; //sve = svar, se ovan.
				felGlos.eng = testlist.at(i).eng;
				fellista.push_back(felGlos);
			}
			it = testlist.begin() + i;
			testlist.erase(it);
			cout << endl;
		}
		
		system("pause");
		system("cls");
		
		cout << "InterGlos Basic - Sammanst\x84llning av glostest" << endl;
		cout << "=============================================" << endl;
		cout << endl;
		
		if (fellista.empty()) {
			cout << "Grattis! Du svarade r\x84tt p\x86 samtliga " << lista.size() << " ord!" << endl;
		} else {
			cout << "Av " << lista.size() << " ord svarade du r\x84tt p\x86 " << ratt << "." << endl;
			cout << endl << "~~~~~~ ~~~~~ ~~~~ ~~~ ~~ ~" << endl << endl;
			cout << "Du gjorde f\x94ljande ";
			if (fellista.size() > 1) {
				cout << fellista.size() << " ";
			}
			cout << "fel:" << endl << endl;
			cout << "Ditt svar\tR\x84tt svar" << endl;
			cout << "---------\t---------" << endl;
			for (i = 0; i < fellista.size(); i++) {
				cout << fellista.at(i).sve << (fellista.at(i).sve.size() < 8 ? "\t" : "" ) << "\t" << fellista.at(i).eng << endl;	
			}
		}
	}
}

void skrivUt(vector<Glosa> lista) {
	cout << "InterGlos Basic - Aktuell gloslista" << endl;
	cout << "===================================" << endl;
	cout << endl;
	
	if (lista.empty()) {
		cout << "Gloslistan \x84r tom!";
	} else {
		cout << "Svenska\t\tEngelska" << endl;
		cout << "-------\t\t--------" << endl;
		for (int i = 0; i < lista.size(); i++) {
			cout << lista.at(i).sve << (lista.at(i).sve.size() < 8 ? "\t" : "" ) << "\t" << lista.at(i).eng << endl;
		}
		cout << endl << "Totalt " << lista.size() << " ord.";
	}
	cout << endl;
}

void sparaFil(vector<Glosa> lista) {
	ofstream utFil;
	char filnamn[100];

	cout << "InterGlos Basic - Spara till fil" << endl;
	cout << "================================" << endl;
	cout << endl;
	
	if (lista.empty()) {
		cout << "Gloslistan \x84r tom!";
	} else {
		cout << "Filnamn (max 99 tecken): ";
		cin.ignore(1000,'\n');
		cin.getline(filnamn, 100); //getline funkar bättre än get för tomma strängar. get resulterar i det fallet i en oändlig loop.
		cout << endl;

		utFil.open(filnamn);
		if (utFil) {
			cout << "Skriver till fil";
			utFil << "IGB::WordFile";
			for (int i = 0; i < lista.size(); i++) {
				utFil << endl << lista.at(i).sve << '|' << lista.at(i).eng;
				cout << '.';
			}
			utFil.close();
			cout << endl << endl << "F\x84rdig.";
		} else {
			cout << "Kunde inte \x94ppna filen f\x94r skrivning!";
		}
	}
	cout << endl;
}

void hamtaFil(vector<Glosa>& lista) {
	ifstream inFil;
	char svar;
	Glosa inGlos;
	string inString;
	char filnamn[100];

	cout << "InterGlos Basic - H\x84mta fr\x86n fil" << endl;
	cout << "================================" << endl;
	cout << endl;
	
	if (!lista.empty()) {
		cout << "Gloslistan \x84r inte tom." << endl;
		cout << "Vill du t\x94mma den innan inl\x84sning fr\x86n fil (j/n)? ";
		cin >> svar;
		if (tolower(svar) == 'j') { //tolower() för att både j och J ska funka.
			lista.clear();
		}
		cout << endl;
	} 
	cout << "Filnamn (max 99 tecken): ";
	cin.ignore(1000,'\n');
	cin.getline(filnamn, 100); //se funktionen ovan för förklaring.
	cout << endl;
	
	inFil.open(filnamn);
	if (inFil) {
		getline(inFil,inString);
		if (inString != "IGB::WordFile") { //kontroll av fil
			cout << "Filen \x84r inte en InterGlos Basic-fil!";
		} else {
			cout << "L\x84ser fr\x86n fil";
			getline(inFil,inGlos.sve,'|'); //läser till första |-tecknet på raden.
			while (inGlos.sve != "") {
				inGlos.eng = ""; //vissa kompilatorer kräver detta för att förhindra en oändlig loop.
				getline(inFil,inGlos.eng); //läser till slutet på raden
				if (inGlos.eng != "") {
					cout << '.';
					lista.push_back(inGlos);
					inGlos.sve = ""; //se ovan.
					getline(inFil,inGlos.sve,'|'); //se ovan.
				} else {
					inGlos.sve = "";
				}
			}
			inFil.close();
			cout << endl << endl << "F\x84rdig.";
		}
	} else {
		cout << "Kunde inte \x94ppna filen f\x94r l\x84sning!";
	}
	cout << endl;
}

void tomLista(vector<Glosa>& lista) {
	char svar;

	cout << "InterGlos Basic - T\x94m lista" << endl;
	cout << "===========================" << endl;
	cout << endl;
	
	if (!lista.empty()) {
		cout << "\x8Er du s\x84ker p\x86 att du vill t\x94mma listan (j/n)? ";
		cin >> svar;
		if (tolower(svar) == 'j') { // se ovan.
			lista.clear();
		}
	} else {
		cout << "Gloslistan \x84r redan tom!" << endl;
	}
}

