// main.cpp
// CSI 2131 Asst 2a by Naim R. El-Far (naim@discover.uottawa.ca)
// CSI 2131 - Winter 2006 - Prof. L.M. Moura

#include <iostream>
#include <fstream>
#include <string>
#include "StudentRecord.h"
#include "DataFile.h"
#include "PrimaryIndex.h"
using namespace std;

int main() {
	//Print welcome message
	//---------------------
	cout << "CSI 2131 Asst 2a W06" << endl
		 << "--------------------" << endl
		 << endl;

	//---------------------------
	//Step 1: Build primary index
	//---------------------------
	
	//Prompt user for data file name
	//------------------------------
	cout << "Please enter data file name (default: studentfile.txt): ";
	string dataFileName = "";
	getline(cin, dataFileName, '\n');
	if (dataFileName == "")
		dataFileName = "studentfile.txt";
	
	//Instantiate data file class with data file name
	//-----------------------------------------------
	DataFile studentDataFile(dataFileName);

	//Prompt user for primary index file name
	//---------------------------------------
	cout << "Please enter primary index file name (default: primaryindex.txt): ";
	string primaryIndexFileName = "";
	getline(cin, primaryIndexFileName, '\n');
	if (primaryIndexFileName == "")
		primaryIndexFileName = "primaryindex.txt";

	//Instantiate primary index class with index file name
	//----------------------------------------------------
	PrimaryIndex	primaryIndex(primaryIndexFileName);

	//Read each record from the data file and insert the student number and byte offset into the primary index
	//--------------------------------------------------------------------------------------------------------
	string			rawString = "";
	long int		offsetInDataFile = studentDataFile.GetCurrentFilePointerOffset(); //Get offset of first record (i.e. 0)
	while (getline(studentDataFile.dataFileStream, rawString, '\n')) {
		//Read record from data file
		StudentRecord currentStudentRecord(offsetInDataFile, rawString);
		currentStudentRecord.PrintFormatted();

		//Write record to index file
		primaryIndex.Addition(currentStudentRecord.studentNumber, currentStudentRecord.offsetInDataFile);

		//Get offset of next record
		offsetInDataFile = studentDataFile.GetCurrentFilePointerOffset();
	}

	//------------------------
	//Step 2: Process searches
	//------------------------
	
	//Prompt user for search file name
	//--------------------------------
	cout << endl
		 << "Please enter search file name (default: searches.txt): ";
	string searchFileName = "";
	getline(cin, searchFileName, '\n');
	if (searchFileName == "")
		searchFileName = "searches.txt";
	cout << endl;
	
	//Open the search file
	//--------------------
	ifstream searchFileStream(searchFileName.c_str(), ios::in);
	if (!searchFileStream) {
		cerr << endl
			 << "------------------------------------------------" << endl
			 << "Error! Specified search file could not be opened!" << endl
			 << "------------------------------------------------" << endl
			 << endl;
		exit(1);
	}
	
	//Read each search request and process it
	//---------------------------------------
	string studentNumberString = "";
	while (getline(searchFileStream, studentNumberString, '\n')) {
		long int recordOffset = primaryIndex.BinarySearch(studentNumberString);
		if (recordOffset == -1)
			cout << "Record for student number " << studentNumberString << " does not exist." << endl
				 << "-----------------------" << endl
			     << endl;
		else {
			cout << "Search for student number " << studentNumberString << " returned: " << endl;
			studentDataFile.PrintRecordAtOffset(recordOffset);
			cout << endl;
		}

	}
		
	//Successful termination
	//----------------------
	return 0;
}