//Program Name: Micro Chao Garden
//Author: Jeffery Wright
//Date: 2017/02/11
//Description: Interface for the Micro Chao Garden Project

#include <iostream>
#include <cstdlib>
#include <fstream>
//#include <ncurses.h>

#include "Chao.h"
#include "Items.h"

using namespace std;

int const CAPACITY = 8;
Chao chao[CAPACITY]; // Array to hold the garden's inhabitants;
	int emptySlot = 0; // Index that will hold the next created Chao.

void userInterface();
void hatch();
void nameChao();
void giveItem();
void pet();
void statistics();
void breed();
void listChao();
void ageChao();

//Begins program execution, launches user interface, and handles save data.
int main() {
	srand(time(NULL));
	ifstream chaoReader;
	chaoReader.open("chao.dat");
	if (chaoReader) {
		while (!chaoReader.eof() && emptySlot < CAPACITY) {
			chao[emptySlot] = Chao(chaoReader);
			emptySlot++;
		} //end while
		emptySlot--;
	} else {
		chaoReader.clear();
	} //end if
		chaoReader.close();
	userInterface();
	ofstream chaoWriter;
	chaoWriter.open("chao.dat");
	for (int i = 0; i < emptySlot; i++) {
		chao[i].save(chaoWriter);
	} //end for
	chaoWriter.close();
	return 0;
} //end main

//Generates user menu and calls other methods
void userInterface() {
//	initscr(); //Start Curses Mode.
//	cbreak(); //Disable Line Buffer
//	keypad(stdscr, TRUE); //Enable Function and Navigation Keys.
//	noecho(); //Disable Key echo.
	int choice= -1;
	while (choice != 0) {
//		move(0, 0);
		cout << "Welcome to Micro Chao Garden. Please select an action." << endl << endl;;
		cout << "0) Leave the Garden" << endl;
		cout << "1) Hatch a new Chao" << endl;
		if (emptySlot > 0) {
			cout << "2) Name a Chao" << endl;
			cout << "3) Give a Chao an item" << endl;
			cout << "4) Pet a Chao" << endl;
			cout << "5) View a Chao's Statistics" << endl;
		} //end if
		if (emptySlot > 1) {
			cout << "6) Breed two Chao" << endl;
		} //end if
		cout << "\nEnter an option." << endl;
//		refresh(); //Refresh Screen.
		cin >> choice;
		if (choice == 1) {
			hatch();		
		} else if (choice == 2) {
			nameChao();
		} else if (choice == 3) {
			giveItem();
		} else if (choice == 4) {
			pet();
		} else if (choice == 5) {
			statistics();
		} else if (choice == 6) {			
			breed();
		} else if (choice == 0) {
			cout << "\nGoodbye!" << endl;
		} else {
			cout << "Invalid input!" << endl;
		} //end if
		ageChao();
	} //end while
//	endwin(); //End Curses Mode.
} //end function

//Prints a list of Chao to the screen.
void listChao() {
	for (int i = 0; i < emptySlot; i++) {
		cout <<i << ": " << chao[i].getName() << endl;
	} //end for
} //end function

//Ages all Chao
void ageChao() {
	for (int i = 0; i < emptySlot; i++) {
		chao[i].updateAge();
		if (chao[i].getAge() == 1000) {
			chao[i].evolve();
		} else if (chao[i].getAge() == 5000) {
			chao[i].reincarnate();
		} //end if
	} //end for
} //end function

//Name a Chao
void nameChao() {
	listChao();
	int choice;
	string name;
	cout << "Which Chao would you like to name? ";
	cin >> choice;
	cout << "What would you like to name it? ";
	cin >> name;
	chao[choice].setName(name);
} //end function

//Menu to manage giving items to Chao.
void giveItem() {
	ItemCollection items = ItemCollection();
	int choice = 0;
	int itemIndex = -1;
	int chaoIndex = -1;
	listChao();
	cout << "Which Chao should get the item? ";
	cin >> chaoIndex;
	cout << "Which kind of item do you want to give?" << endl;
	cout << "1) Food" << endl;
	cout << "2) Chaos Drives" << endl;
	cout << "3) Small Animals" << endl;
	cout << "enter choice: ";
	cin >> choice;
	if (choice == 1) {
		items.printFruits();
		cout << "Which fruit should be given? ";
		cin >> itemIndex;
		chao[chaoIndex].giveFruit(items.getFruit(itemIndex));
	} else if (choice == 2) {
		items.printChaosDrives();
		cout << "Which Chaos Drive should be given? ";
		cin >> itemIndex;
		chao[chaoIndex].giveStatItem(items.getChaosDrive(itemIndex).getStats());
		chao[chaoIndex].giveEvolutionItem(items.getChaosDrive(itemIndex).getEvolution());
	} else if (choice == 3) {
		items.printAnimals();
		cout << "Which animal should be given? ";
		cin >> itemIndex;
		chao[chaoIndex].giveStatItem(items.getAnimal(itemIndex).getStats());
		chao[chaoIndex].giveEvolutionItem(items.getAnimal(itemIndex).getEvolution());
	} //end if
} //end function

//Pet a Chao.
void pet() {
	int chaoIndex = -1;
	int choice;
	listChao();
	cout << "Pet which chao: ";
	cin >> chaoIndex;
	cout << "Which Alignment?(-1: Hero, 0: Neutral, 1: Dark) ";
	cin >> choice;
	chao[chaoIndex].pet(choice);
} //end function

//Prints the Information the User requests about the Chao.
void statistics() {
	int choice = -1;
	int chaoIndex = -1;
	while (choice !=0 ) {
		cout << "Which statistics should be printed?" << endl;
		cout << "1) Name, Age, and Reincarnations" << endl;
		cout << "2) Stats" << endl;
		cout << "3) Evolution" << endl;
		cout << "4) Breed" << endl;
		cout << "5) Genotype" << endl;
		cout << "0) Return to main menu" << endl << endl;	
		cout << "Enter option: ";
		cin >> choice;
		if (choice > 0 && choice <= 5) {
			listChao();
			cout << "Which chao's statistics should be printed? ";
			cin >> chaoIndex;
		} //end if
		if (choice == 1) {
			cout << chao[chaoIndex].getName() << " is " << chao[chaoIndex].getAge() << " chao hours old and has reincarnated " << chao[chaoIndex].getReincarnations() << " times" << endl;
		} else if (choice == 2) {
			chao[chaoIndex].printStats();
		} else if (choice == 3) {
			cout << chao[chaoIndex].getName() << " is a " << EVOLUTIONS[chao[chaoIndex].getEvolution().getEvolution()] << " chao." << endl;
			chao[chaoIndex].getEvolution().printSliders();
		} else if (choice == 4) {
			chao[chaoIndex].getBreed().printPhenotype();
			if (chao[chaoIndex].getBreed().isPure()){
				cout << "This chao is purebred!" << endl;
			} //end if
		} else if (choice == 5) {
			chao[chaoIndex].getBreed().printGenotype();
		} else {
			cout << "Invalid Input" << endl;
		} //end if
		ageChao();	
	} //end while
} //end function

//Breeds the selected Chao.
void breed() {
	int parent1;
	int parent2;
	listChao();
	cout << "Which chao should be bred? ";
	cin >> parent1 >> parent2;
	if (parent1 == parent2) {
		cout << "A chao cannot be bred with itself." << endl;
	} else if (chao[parent1].getEvolution().getEvolution() < 3  || chao[parent2].getEvolution().getEvolution() < 3) {
		cout << "Child chao cannot be bred." << endl;
	} else if (emptySlot == CAPACITY) {
		cout << "The garden is full, cannot breed a new Chao." << endl;
	} else {
		chao[emptySlot] = Chao(chao[parent1], chao[parent2]);
		emptySlot++;
	} //end if
} //end function

//Hatches a new chao.
void hatch() {
	if (emptySlot == CAPACITY) {
		cout << "Garden is full, cannot hatch a new chao." << endl;
	} else {
		int choice;
		int colorIndex;
		cout << "Which type of chao egg should be hatched?" << endl;
		cout << "1) Normal" << endl;
		cout << "2) Monotone" << endl;
		cout << "3) Shiny Monotone" << endl;
		cout << "4) Jewel" << endl;
		cout << "Enter choice: ";
		cin >> choice;
		if (choice == 1) {
			chao[emptySlot] = Chao();
		} else if (choice == 2) {
			printColors();
			cout << "Enter number of chosen color: ";
			cin >> colorIndex;
			chao[emptySlot] = Chao(colorIndex, 0, false, false);
		} else if (choice == 3) {
			printColors();
			cout << "Enter number of chosen color: ";
			cin >> colorIndex;
			chao[emptySlot] = Chao(colorIndex, 0, true, false);
		} else if (choice == 4) {
			printCoats();
			cout << "Enter number of chosen Jewel Coat: ";
			cin >> colorIndex;
			chao[emptySlot] = Chao(0, colorIndex, false, true);
		} //end if
		emptySlot++;
	} //end if
} //end function
