//File Functions.h - contains miscellaneous functions that don't belong in a class
//Functions in this file are recreations of Java's string functions

//Make sure the file hasn't been included already
#ifndef _menufunctions_h
#define _menufunctions_h

#include <iostream>
#include <string>
#include <list>
#include "Recipe.h"

using namespace std;

//FindContributor function
void FindContributor(list<Recipe> & recipes){
	string studentNum;
	bool isValidNumber = false;

	while(!isValidNumber){
		isValidNumber = true;
		cout << "Enter student number (6 digits): ";
		cin >> studentNum;
		if(studentNum.length()!=6){
			cout << "Not a valid student number." << endl;
			isValidNumber = false;
		}
		else{
			/*char * num;
			num = new char [studentNum.size()+1];
			strcpy(num, studentNum.c_str());*/
			int test = atoi(studentNum.c_str());
			if((test == 0) || (test == INT_MAX) || (test == INT_MIN)){
				cout << "Not a valid student number." << endl;
				isValidNumber = false;
			}
		}
	}

	bool isContributor = false;
	list<Recipe>::iterator it;
	int count = 0;
	for (it=recipes.begin(); it!=recipes.end(); it++){
		count++;
		if (Contains(it->getNumber(), studentNum)){
			cout << count << ". " << it->getName() << endl;
			isContributor = true;
		}
	}

	if (!isContributor){
		cout << "Sorry, no such contributor exists." << endl;
	}
	cout << "\nPress enter to return to menu." << endl;
	while(getchar()!='\n');
}

//SelectSpice function
string SelectSpice(){
	string spiceSelected;
	cout << "Enter spice: ";
	cin >> spiceSelected;

	cout << "\nPress enter to return to menu." << endl;
	while(getchar()!='\n');
	return spiceSelected;
}

//SelectRating function
string SelectRating(){
	string ratingSelected;
	cout << "Enter rating: ";
	cin >> ratingSelected;

	cout << "\nPress enter to return to menu." << endl;
	while(getchar()!='\n');
	return ratingSelected;
}

//ListRecipeNames function
void ListRecipeNames(list<Recipe> & recipes, string spice, string rating){
	int count = 0;
	int lines = 0;
	list<Recipe>::iterator it;

	for (it=recipes.begin(); it!=recipes.end(); it++){
		count++;
		if(((LowerCase(rating).compare("any") == 0) || 
			(LowerCase(rating).compare(LowerCase(it->getRating())) == 0)) &&
			((LowerCase(spice).compare("any") == 0) || 
			Contains(LowerCase(it->getSpice()), LowerCase(spice)))){
				cout << count << ". " << Trim(it->getName()) << endl;
				lines++;
				if (lines > 20){
					lines = 0;
					cout << "\nPress enter for more...";
					while(getchar()!='\n');
				}
		}
	}
	cout << "\nPress enter to return to menu." << endl;
	while(getchar()!='\n');
}

//DisplayRecipe function
void DisplayRecipe(list<Recipe> & recipes){
	int recipeNumber;
	cout << "Enter a recipe number(starting at 1): ";
	cin >> recipeNumber;
	list<Recipe>::iterator it;
	it=recipes.begin();
	for(int i=0; i<recipeNumber; i++)
		it++;
	it->printOn(cout);
	
	cout << "\nPress enter to return to menu." << endl;
	while(getchar()!='\n');
}

//EnterNewRecipe function
void EnterNewRecipe(list<Recipe> & recipes){
	string str = "";
	string aName="", aRating="", aContributor="", someSpices="", aSource="", aCategory="", aDescription="";
	string someIngredients="", someDirections="";
	
	//System.out.println("When finished each entry, type * to end.");
	
	cout << "Enter name of recipe (1 line only): ";
	cin >> aName;
	/*while(!(str = input.nextLine()).equals("*")){
		aName = aName + str;
	}*/
	
	cout << "\nEnter a rating (1 line only): ";
	cin >> aRating;
	/*while(!(str = input.nextLine()).equals("*")){
		aRating = aRating + str;
	}*/
	
	cout << "\nEnter your student number (6 digits): ";
	cin >> aContributor;
	/*while(!(str = input.nextLine()).equals("*")){
		aContributor = aContributor + str;
	}*/
	
	cout << "\nEnter the main spices used (enter * as last line to end field): ";
	while(str.compare("*") != 0){
		cin >> str;
		someSpices += str;
	}
	str = "";
	
	cout << "\nEnter the source of the recipe (1 line only): ";
	cin >> aSource;
	/*while(!(str = input.nextLine()).equals("*")){
		aSource = aSource + str;
	}*/
	
	cout << "\nEnter the category of the dish (1 line only): ";
	cin >> aCategory;
	/*while(!(str = input.nextLine()).equals("*")){
		aCategory = aCategory + str;
	}*/
	
	cout << "\nEnter a description (enter * as last line to end field): ";
	while(str.compare("*") != 0){
		cin >> str;
		aDescription += str;
	}
	str = "";
	
	cout << "\nEnter the ingredients (enter * as last line to end field): ";
	while(str.compare("*") != 0){
		cin >> str;
		someIngredients += str;
	}
	str = "";
	
	cout << "\nEnter the directions (enter * as last line to end field): ";
	while(str.compare("*") != 0){
		cin >> str;
		someDirections += str;
	}
	str = "";

	recipes.push_front(*(new Recipe(aName, aRating, aContributor, someSpices, aSource, aCategory, aDescription,
		someIngredients, someDirections)));
	cout << "\nPress enter to return to menu." << endl;
	while(getchar()!='\n');
}

//Save function
void Save(list<Recipe> & recipes, string oldFileName, string xmlStartTag, string xmlEndTag){
	cout << "Would you like to overwrite the old file? (y/n): ";
	string fileName;
	
	char userChoice;
	cin >> userChoice;
	if (userChoice != 'y' && userChoice != 'Y'){
		cout << "Enter name of new file: ";
		cin >> fileName;
	}
	else{
		fileName = oldFileName;
	}
	ofstream fout(fileName.c_str(), ios::out);

    if(!fout){
        cout << "ERROR: could not open file " << fileName << endl;
    }
	else{
		fout << xmlStartTag;
		list<Recipe>::iterator it;
		for (it=recipes.begin(); it!=recipes.end(); it++){
			it->printXMLOn("\t", fout);
		}
		fout << xmlEndTag;
	}
	fout.close();
	cout << "Saving file...";

	cout << "\nPress enter to return to menu." << endl;
	while(getchar()!='\n');
}
void SaveAndExit(list<Recipe> & recipes, string fileName, string xmlStartTag, string xmlEndTag){
	ofstream fout(fileName.c_str(), ios::out);

    if(!fout){
        cout << "ERROR: could not open file " << fileName << endl;
    }
	else{
		fout << xmlStartTag;
		list<Recipe>::iterator it;
		for (it=recipes.begin(); it!=recipes.end(); it++){
			it->printXMLOn("\t", fout);
		}
		fout << xmlEndTag;
	}
	fout.close();
	cout << "Saving file...";
}
#endif
//End of MenuFunctions.h