#include "phonebook.h"

phonebook::phonebook()
{
	name = "";
	phone = "";
	entries = 0;
}

void phonebook::Create()
{
	ifstream in("P08.txt");
	if (in.fail())
	{
		cerr << "Error opening P08.txt!\n";
		exit(1);
	}
	getline(in, book[entries][NAME]);
	getline(in, book[entries][PHONE]);
	while (!in.eof())
	{
		entries++;
		getline(in, book[entries][NAME]);
		getline(in, book[entries][PHONE]);
	}
	in.close();
}
void phonebook::Add()
{
	if (entries < MAX)
	{
		cout << "Enter name (last, first): ";
		cin.ignore(1);
		getline(cin, book[entries][NAME]);
		cout << "Enter phone number (xxx-xxxx): ";
		getline(cin, book[entries][PHONE]);
		cout << "Success! " << book[entries][NAME] << " was added.\n\n";
		entries++;
		string temp[ITEMS];
		int smallest_index;
		// Alphabetize array by last name
		for (int i = 0; i < entries; i++)
		{
			smallest_index = i;
			for (int j = i + 1; j < entries; j++) // Find smallest index
				if (book[j][NAME] < book[i][NAME])
					smallest_index = j;
			if (smallest_index != i) // Swap current position with smallest index
			{
				temp[NAME] = book[smallest_index][NAME];
				temp[PHONE] = book[smallest_index][PHONE];
				book[smallest_index][NAME] = book[i][NAME];
				book[smallest_index][PHONE] = book[i][PHONE];
				book[i][NAME] = temp[NAME];
				book[i][PHONE] = temp[PHONE];
			}
		}
	}
	else
		cout << "Error! Database full.\n\n";
}
void phonebook::Change()
{
	bool found = false;
	cout << "Enter name (last, first): ";
	cin.ignore(1);
	getline(cin, name);
	for (int i = 0; i < entries; i++)
	{
		if (book[i][NAME] == name)
		{
			found = true;
			cout << "Old phone number: " << book[i][PHONE] << endl;
			cout << "Enter new phone number: ";
			getline(cin, book[i][PHONE]);
		}
	}	
	if (found)
		cout << "Success! Number changed.\n\n";
	else
		cout << "Error! " << name << " was not found in the database.\n\n";
}
void phonebook::Display()
{
	if (entries == 0)
		cout << "Database is empty.\n\n";
	else
	{
		cout << endl;
		for (int i = 0; i < entries; i++)
			cout << book[i][NAME] << endl << book[i][PHONE] << endl;
		cout << endl;
	}
}
void phonebook::Delete()
{
	bool found = false;
	cout << "Enter name to delete (last, first): ";
	cin.ignore(1);
	getline(cin, name);
	for (int i = 0; i < entries; i++)
	{
		if (book[i][NAME] == name)
		{
			for (int j = i; j < entries; j++)
			{
				book[j][NAME] = book[j+1][NAME];
				book[j][PHONE] = book[j+1][PHONE];
			}
			found = true;
			entries--;
			book[entries][NAME] = "";
			book[entries][PHONE] = "";
		}
	}
	if (found) 
		cout << "Success! " << name << " was deleted.\n\n";
	else
		cout << "Error! " << name << " was not found in the database.\n\n";
}
void phonebook::Update()
{
	ofstream out("P08.txt");
	if (out.fail())
	{
		cerr << "Error writing to P08.txt\n";
		exit(1);
	}
	for (int i = 0; i < entries; i++)
		out << book[i][NAME] << endl << book[i][PHONE] << endl;
	out.close();
}
int phonebook::Menu()
{
	int choice;
	cout << "What would you like to do?\n\n"
		<< "1.\t Add an entry\n"
		<< "2.\t Change a listed number\n"
		<< "3.\t Display phone book\n"
		<< "4.\t Delete entry\n"
		<< "5.\t Quit\n\n"
		<< "Enter selection: ";
	cin >> choice;
	while (choice < 1 || choice > QUIT)
	{
		cout << "Invalid choice! Enter selection: ";
		cin >> choice;
	}
	switch (choice)
	{
	case 1: this->Add(); break;
	case 2: this->Change(); break;
	case 3: this->Display(); break;
	case 4: this->Delete(); break;
	case 5: break;
	default: break;
	}
	
	return choice;
}