// DataBase.cpp
// Stores data

#include "database.h"
#include "htmlwriter.h"

//Constructor
DataBase::DataBase() {
		//cout << "\nInitializing DataBase ...";
		for (int x=0; x<=25; x++) {					// assign appropriate letter to each element of letters
			char val = 'a' + x;
			letters[x].assignName(val);	
		}
		//cout << "DataBase Initialization Succesful!";
}

//Adds word and URL info to appropriate letter element
int DataBase::tally(string word, string URLaddress, string description) {
		return letters[word[0]-'a'].tally(word,URLaddress,description);
}

//Prints results home page, calls print for each letter, takes output directory as param.
void DataBase::print( string outputDIR) {
		ofstream test("results/test.html", ios::out);
		test << "You win";
		test.close();
		HTMLWriter mainPage;
		mainPage.open(outputDIR+"index.html");
		mainPage.header("Web Browser Results");
		for (int x=0; x<=25; x++) {					// make list of letters & links
			string lname = "";
			lname += letters[x].getName();
			mainPage.link(lname,lname + ".html");
			if (x!=25) mainPage.divideList();
		}
		mainPage.footer();
		mainPage.close();
		for (int x=0; x<=25; x++) {					// call print on letters
			letters[x].print(outputDIR);
		}
}

// Debugging method
void DataBase::testWord() {
	for (int x=0; x<=25; x++) {
		letters[x].testWord();
	}
}
