#include <iostream>
#include <string>

using namespace std;

#include "cookies.h"


int getOption(int range);
string getName(string message);

int main()
{
	string * cookies [MAXIMUM_SIZE] = {};
	int x = 0;
	int * size = &(x);
	
	bool done = false;
	while(!done)
	{
		//Get menu option
		cout << "MENU:" << endl;
		cout << "\t1. Add an order\n\t2. Deliver an order\n\t3. Modify an order\n\t4. Quit\n" << endl;
		int option = getOption(4);
		
		//Adding
		if(option==1)
		{
			if((*size) >= MAXIMUM_SIZE)
			{
				cout << "The car is full; cannot add more orders" << endl;
			}
			else
			{
				string new_name = getName("Please enter the customer's name for the new order:");
				addOrder(cookies, size, new_name);
				cout << "Cookies added for costumer [" << new_name << "]" << endl;
			}
		}

		//Delivering
		else if(option==2)
		{
			string name = getName("Please enter the customer's name for the delivery:");
			int delivered = deliverOrder(cookies, size, name);
			cout << "Delivered " << delivered << " boxes of cookies to [" << name << "]" << endl;
		}

		//Modifying
		else if(option==3)
		{
			string original_name = getName("Please enter the original customer's name of the order:");
			string new_name = getName("Please enter the new customer's name for the order:");
			bool changed = modifyOrder(cookies,size,original_name,new_name);
			if(changed)
			{
				cout << "Changed name from [" << original_name << "] to [" << new_name << "]" << endl;
			}
			else
			{
				cout << "Could not find a customer with the name: [" << original_name << "]" << endl;
			}
		}

		//Quitting
		else if(option==4)
		{
			done = true;
		}
		
		cout << displayOrders(cookies, size) << endl;
	}

    clean_up(cookies, size);

	cout << "Thank you for using the cookie tracker!" << endl;
	//The following line may not work on all systems; therefore, you may change this line as needed
	return 0;
}

int getOption(int range)
{
	int input = 0;
	bool done = false;
	while(!done)
	{
		cout << "Please select an option:" << endl;
		input = 0;
		cin >> input;
		cin.ignore(1000,'\n');
		if(cin.fail())
		{
			cin.clear();
			cin.ignore(1000,'\n');
			cout << "Error: Invalid option" << endl;
		}
		else if(input < 1  ||  input > range)
		{
			cout << "Error: Invalid option number" << endl;
		}
		else
		{
			done = true;
		}
	}
	return input;
}

string getName(string message)
{
	string input = "";
	bool done = false;
	while(!done)
	{
		cout << message << endl;
		input = "";
		getline(cin, input);
		if(cin.fail())
		{
			cin.clear();
			cin.ignore(1000,'\n');
			cout << "Error: Invalid name" << endl;
		}
		else
		{
			done = true;
		}
	}
	return input;
}
