// User Includes

#include "book.h"

book::book()
{
	initBook();
	listingCount = 0;
	currentSort = LAST;

}

void book::initBook()
{
	memset(listing, 0, sizeof(person) * BOOK_SIZE);

	return;
}

void book::addContact()
{
	person temp;

	temp.inputPerson();

	listing[listingCount] = temp;
	listingCount++;

	// Sort once ready.

	return;

}

void book::displayBook()
{
	int curPos;
	char junk[256];

	for (curPos = 0; curPos < listingCount; curPos++)
	{
		listing[curPos].displayPerson();

		if ( (curPos) && !( curPos % 5 ) )
		{
			cout << "[Press enter to continue]" << endl;
			cin.getline(junk, 256);
		}
	}

}

int book::readBook(char* filename)
{
	ifstream input;
	person temp;
	char junk[256];

	if (!filename)
	{
		filename = new char[256];
		cout << "Enter an address book to read in: ";
		cin.getline(filename, 256);
	}

	input.open(filename);

	if (input)
	{
		cout << "Reading from file: " << filename << endl;
		while (! input.eof() )
		{
			temp.inputPerson(input);
			input.getline(junk, 256);
			listing[listingCount] = temp;
			listingCount++;
		}

		input.close();
	}

	return(listingCount);

}

void book::writeBook(char* filename)
{
	ofstream output;
	int curPos;

	if (!filename)
	{
		filename = new char[256];
		cout << "Enter an address book to write to: ";
		cin.getline(filename, 256);
	}

	output.open(filename);

	if (output)
	{
		cout << "Writing to file: " << filename << endl;
		for (curPos = 0; curPos < listingCount; curPos++)
		{
			listing[curPos].writePerson(output);
			if ( !( curPos == (listingCount - 1) ) )
				output << endl;
		}

		output.close();
	}

}

personField book::setSearchType()
{
	int ans;
	personField type;
	char junk[256];

	cout << endl
		<< "0.  First Name." << endl
		<< "1.  Last Name." << endl
		<< "2.  Address." << endl
		<< "3.  City." << endl
		<< "4.  Province." << endl
		<< "5.  Postal Code." << endl
		<< "6.  Country." << endl
		<< "7.  Phone (Home)." << endl
		<< "8.  Phone (Cell)." << endl
		<< "9.  E-Mail." << endl
		<< endl
		<< "Select a field: ";

	cin >> ans;
	cin.getline(junk, 256);

	type = (personField) ans;

	currentSort = type;

	return (type);

}

int book::findContact()
{
	char searchField[256];
	int currentPos;
	char ansChar;
	bool found = false;

	setSearchType();

	cout << "Searching via: " << fieldNames[ currentSort ] << endl
		<< endl
		<< "Enter text to search for: ";

	cin.getline(searchField, 256);

	for( currentPos = 0; ( (currentPos < listingCount) && (!found) ); currentPos++)
	{
		if ( listing[currentPos].checkField(searchField, currentSort) == 0 )
		{	
			found = true;
			listing[currentPos].displayPerson();
			cout << endl
				<< "Correct Person? (Y/N): ";;
			cin >> ansChar;

			if ( (ansChar == 'Y') || (ansChar == 'y') )
				break;

			else
				found = false;

		}
	}
	
	if (!found)
		cout << "Person not found." << endl;

	return (currentPos);
}

void book::editContact(int contactNum)
{
	int opt;
	char junk[256];
	char dataBuffer[256];

	cout << "Editing Menu:" << endl << endl;

	if ( contactNum == listingCount)
		opt = END_FIELD;

	while ( opt != END_FIELD )
	{
		cout << endl << "Enter in new information.  Select number to enter information." << endl << endl
			<< "0.  First Name: [" << listing[contactNum].getFirstName() << "]" << endl
			<< "1.  Last Name: [" << listing[contactNum].getLastName() << "]" << endl
			<< "2.  Address: [" << listing[contactNum].getAddress() << "]" << endl
			<< "3.  City: [" << listing[contactNum].getCity() << "]" << endl
			<< "4.  Province: [" << listing[contactNum].getProvince() << "]" << endl
			<< "5.  Postal Code: [" << listing[contactNum].getPostalCode() << "]" << endl
			<< "6.  Country: [" << listing[contactNum].getCountry() << "]" << endl
			<< "7.  Phone (Home): [" << listing[contactNum].getPhoneHome() << "]" << endl
			<< "8.  Phone (Cell): [" << listing[contactNum].getPhoneCell() << "]" << endl
			<< "9.  E-Mail: [" << listing[contactNum].getEmail() << "]" << endl
			<< endl
			<< "10.  Quit Editing." << endl
			<< "Option: ";
		
		cin >> opt;
		cin.getline(junk, 256);
		
		if (opt != END_FIELD)
		{
			cout << opt << ". " << fieldNames[opt] << ": ";
			cin.getline(dataBuffer, 256);
		}

		switch ( opt )
		{
		case FIRST:
			
			listing[contactNum].setFirstName(dataBuffer);
			break;
			
		case LAST:
			
			listing[contactNum].setLastName(dataBuffer);
			break;
			
		case ADDRESS:
			
			listing[contactNum].setAddress(dataBuffer);
			break;
			
		case CITY:
			
			listing[contactNum].setCity(dataBuffer);
			break;
			
		case PROVINCE:
			
			listing[contactNum].setProvince(dataBuffer);
			break;
			
		case POSTAL:
			
			listing[contactNum].setPostalCode(dataBuffer);
			break;
			
		case COUNTRY:
			
			listing[contactNum].setCountry(dataBuffer);
			break;
			
		case PHONE_HOME:
			
			listing[contactNum].setPhoneHome(dataBuffer);
			break;
			
		case PHONE_CELL:
			
			listing[contactNum].setPhoneCell(dataBuffer);
			break;
			
		case EMAIL:
			
			listing[contactNum].setEmail(dataBuffer);
			break;
			
		}

	}
}

void book::deleteContact(int contactNum)
{
	char ans;
	char junk[256];
	int curPos;

	if ( contactNum != listingCount)
	{

		cout << "Are you sure you want to delete this record? (Y/N): ";

		cin >> ans;
		cin.getline(junk, 256);

		if ( (ans == 'Y') || (ans == 'y') )
		{
			listingCount--;
			for (curPos = contactNum; curPos < listingCount; curPos++)
				listing[curPos] = listing[curPos + 1];

			cout << "Record Deleted!" << endl;

		}

		else
			cout << "Unreconized answer.  Record no deleted." << endl;

	}
}

void book::sortBook()
{
	int sortedRecords;
	int currentRecord;
	person temp;

	for (sortedRecords = 0; sortedRecords < ((listingCount) - 1); sortedRecords++)
	{
		for (currentRecord = listingCount - 2; currentRecord >= sortedRecords; currentRecord--)
		{
			if ( listing[currentRecord] > listing[currentRecord + 1] )
			{
				temp = listing[currentRecord];
				listing[currentRecord] = listing[currentRecord + 1];
				listing[currentRecord + 1] = temp;
			}
	
		}

	}

}