//Program Name:	Chao related Items
//Author:	Jeffery Wright
//Date:		2013/06/22
//Description:	Defines state and behavior for items to be used by Micro Chao Garden

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>

#include "Items.h"

using namespace std;

//Stat Item member functions
//Constructs a new Stat Item.
StatItem::StatItem() {
	swim = 0;
	run = 0;
	power = 0;
	fly = 0;
	stamina = 0;
} //end constructor
	
//Constructs a new Stat Item with given stats.
StatItem::StatItem(int swim, int run, int power, int fly, int stamina) {
	this->swim = swim;
	this->run = run;
	this->power = power;
	this->fly = fly;
	this->stamina = stamina;
} //end constructor
	
//accessors
int StatItem::getSwim() {return swim;}
int StatItem::getRun() {return run;}
int StatItem::getPower() {	return power;}
int StatItem::getFly() {return fly;}
int StatItem::getStamina() {return stamina;}

//Evolution Item member functions
//Constructs a null evolution item.
EvolutionItem::EvolutionItem() {
	alignment = 0;
	color = 0;
	strength = 0;
} //end constructor

//Constructs a new Evolution Item.
EvolutionItem::EvolutionItem(int alignment, int color, int strength) {
	this->alignment = alignment;
	this->color = color;
	this->strength = strength;
} //end constructor

//accessors
int EvolutionItem::getAlignment() {return alignment;}
int EvolutionItem::getColor() {return color;}
int EvolutionItem::getStrength() {return strength;}

//Chaos Drive member functions
//Creates a null Chaos Drive.
ChaosDrive::ChaosDrive() {
	stats = StatItem();
	evolution = EvolutionItem();
} //end constructor

//Creates a Chaos Drive of the given color
ChaosDrive::ChaosDrive(int color) {
	if (color == 0) {
		stats = StatItem(0, 0, 0, 0, 0);
	} else if (color == 1) {
		stats = StatItem(24, 0, 0, 0, 0);
	} else if (color == 2) {
		stats = StatItem(0, 24, 0, 0, 0);
	} else if (color == 3) {
		stats = StatItem(0, 0, 24, 0, 0);
	} else if (color == 4) {
		stats = StatItem(0, 0, 0, 24, 0);
	} else if (color == 5) {
		stats = StatItem(0, 0, 0, 0, 24);
	} else if (color == 6) {
		stats = StatItem(24, 24, 24, 24, 24);
	} else if (color == 7) {
		stats = StatItem(-24, -24, -24, -24, -24);
	} //end if
	evolution = EvolutionItem(0, color, 1);
} //end constructor

//accessors
StatItem ChaosDrive::getStats() {return stats;}
EvolutionItem ChaosDrive::getEvolution() {return evolution;}

//Fruit member functions.
//Creates a null fruit.
Fruit::Fruit() {
	name = "Fruit";
	stats = StatItem();
	lastBite = stats;
	bitesLeft = 0;
} //end constructor

//Loads fruit from given if stream.
Fruit::Fruit(ifstream &in) {
	int inputStats[7];
	in >> name;
	for (int i = 0; i < 7; i++) {
		in >> inputStats[i];
	} //end for
	stats = StatItem(inputStats[0], inputStats[2], inputStats[3], inputStats[1], inputStats[4]);
	for (int i = 0; i < 7; i++) {
		if (inputStats[i] == 2 || inputStats[i] == 7 || inputStats[i] == 12) {
			inputStats[i]+=2;
		} else if (inputStats[i] == -2 || inputStats[i] == -7 || inputStats[i] == -12) {
			inputStats[i]-=2;
		} //end if
	} //end for
	lastBite = StatItem(inputStats[0], inputStats[2], inputStats[3], inputStats[1], inputStats[4]);
	bitesLeft = 4;	
	} //end constructor

//accessors
string Fruit::getName() {return name;}
int Fruit::getBitesLeft() {return bitesLeft;}
StatItem Fruit::getStats() {
	if (bitesLeft == 1) {
		return lastBite;
	} else {
		return stats;
	} //end if
} //end function

//TODO: Write Evolution Fruit class.

//Animal member functions.
//Creates null animal.
Animal::Animal() {
	name = "Animal";
	stats = StatItem();
	evolution = EvolutionItem();
} //end constructor

//Loads animal from given if stream.
Animal::Animal(ifstream &in) {
	int inputStats[7];
	in >> inputStats[0];
	in >> name;
	for (int i = 1; i < 7; i++) {
		in >> inputStats[i];
	} //end for
	stats = StatItem(inputStats[1], inputStats[3], inputStats[4], inputStats[2], 0);
	evolution = EvolutionItem(0, inputStats[0], 2);
	} //end constructor

//accessors
string Animal::getName() {return name;}
StatItem Animal::getStats() {			return stats;}
EvolutionItem Animal::getEvolution() {return evolution;}

//Item Collection member functions.
//Creates Item Collection.
ItemCollection::ItemCollection() {
	for (int i = 0; i < 8; i++) {
		chaosDrives[i] = ChaosDrive(i);
	} //end for
	string labels;
	ifstream fruitReader;
	fruitReader.open("fruits.dat");
	for (int i = 0; i < 8; i++) {
		fruitReader >> labels;
	} //end for
	fruitCount = 0;
	while (!fruitReader.eof()) {
		fruits[fruitCount] = Fruit(fruitReader);
		fruitCount++;
	} //end while
	fruitCount--;
	fruitReader.close();
	ifstream animalReader;
	animalReader.open("animals.dat");
	for (int i = 0; i < 8; i++) {
		animalReader >> labels;
	} //end for
	animalCount = 0;
	while (!animalReader.eof()) {
		animals[animalCount] = Animal(animalReader);
		animalCount++;
	} //end while
	animalCount--;
	animalReader.close();
} //end constructor

//Accessors
ChaosDrive ItemCollection::getChaosDrive(int i) {return chaosDrives[i];}
Fruit ItemCollection::getFruit(int i) {return fruits[i];}
Animal ItemCollection::getAnimal(int i) {return animals[i];}

//Printers
//Prints list of Chaos Drives.
void ItemCollection::printChaosDrives() {
	for (int i = 0; i < 8; i++) {
		cout << i << ") " << ITEM_COLORS[i] << endl;
	} //end for
} //end function

//Prints list of Fruits.
void ItemCollection::printFruits() {
	for (int i = 0; i < fruitCount; i++) {
		cout << i << ") " << fruits[i].getName() << endl;
	} //end for
} //end function

//Prints list of Animals.
void ItemCollection::printAnimals() {
	for (int i = 0; i < animalCount; i++) {
		cout << i << ") " << animals[i].getName() << endl;
	} //end for
} //end function
