//Program name: Chao Breed
//Author: Jeffery Wright
//Date: 2017/02/11
//Description:	Definition of a Chao's Breed.

#include <iostream>
#include <cstdlib>
#include <fstream>

#include "Breed.h"

using namespace std;

	//Creates a default Chao Breed(Two-Tone Normal).
	Breed::Breed () {
		color[0] = 0;
		color[1] = 0;
		coat[0] = 0;
		coat[1] = 0;
		shine[0] = false;
		shine[1] = false;
		twotone[0] = true;
		twotone[1] = true;
	} //end constructor

	//Creates a new Purebreed from the given traits.
	Breed::Breed(int colorIn, int coatIn, bool shiny, bool twotoneIn) {
		color[0] = colorIn;
		color[1] = colorIn;
		coat[0] = coatIn;
		coat[1] = coatIn;
		shine[0] = shiny;
		shine[1] = shiny;
		twotone[0] =twotoneIn;
		twotone[1] = twotoneIn;
	} //end constructor

	//Creates a new breed based on two parent breeds.
	Breed::Breed(Breed parent0, Breed parent1) {
		Breed parent[2];
		parent[0] = parent0;
		parent[1] = parent1;
		int random = rand() % 2;
		color[0] = parent[random].getColor(rand() % 2);
		color[1] = parent[(random + 1) % 2].getColor(rand() % 2);
		random = rand() % 2;
		coat[0] = parent[random].getCoat(rand() % 2);
		coat[1] = parent[(random + 1) % 2].getCoat(rand() % 2);
		random = rand() % 2;
		shine[0] = parent[random].getShine(rand() % 2);
		shine[1] = parent[(random + 1) % 2].getShine(rand() % 2);
		random = rand() % 2;
		twotone[0] = parent[random].getTwotone(rand() % 2);
		twotone[1] = parent[(random + 1) % 2].getTwotone(rand() % 2);
	} //end constructor

	//Loads Breed from given ifstream.
	Breed::Breed(ifstream &in) {
		string labels;
		in >> labels >> color[0] >> color[1];
		in >> labels >> coat[0] >> coat[1];
		in >> labels >> shine[0] >> shine[1];
		in >> labels >> twotone[0] >> twotone[1];
		validate();
	} //end constructor

	//Accessors:
	int Breed::getColor(int i) {return color[i];}
	int Breed::getCoat(int i) {return coat[i];}
	bool Breed::getShine(int i) {return shine[i];}
	bool Breed::getTwotone(int i) {return twotone[i];}

//validates data members.
void Breed::validate() {
	for (int i = 0; i < 2; i++) {
		if (color[i] < 0 || color[i] > 14) {color[i] = 0;}
		if (coat[i] < 0 || coat[i] > 17) {coat[i] = 0;}
		if (shine[i] < 0 || shine[i] > 1) {shine[i] = 0;}
		if (twotone[i] < 0 || twotone[i] > 1) {twotone[i] = 0;}
	} //end for
} //end function

	//Parses a Chao's Genetic information and prints its phenotype.
	//Note: If present, Jewel Coat overwrites skin color.
void Breed::printPhenotype() {
		if (shine[0] == true) {
			cout << "Shiny ";
		} //end if
		if (coat[0] != 0) {
		cout << COATS[coat[0]] << endl;
 	} else {
			if (twotone[0] == true) {
				cout << "Two-Tone ";
		} else {
				cout << "monotone ";
			} //end if
			cout << CHAO_COLORS[color[0]] << endl;
	} //end if
	} //end function

	//Prints the Chao's Genotype.
	void Breed::printGenotype() {
		cout << "Color: " << CHAO_COLORS[color[0]] << " " << CHAO_COLORS[color[1]] << endl;
		cout << "Coat: " << COATS[coat[0]] << " " << COATS[coat[1]] << endl;
		cout << "Shine: " << shine[0] << " " << shine[1] << endl;
		cout << "Two-Tone: " << twotone[0] << " " << twotone[1] << endl;
	} //end function
	
	//Determines if the Chao is Purebred.
	bool Breed::isPure() {return (color[0] == color[1] && coat[0] == coat[1] && shine[0] == shine[1] && twotone[0] == twotone[1]);}

	//Saves the Breed.
	void Breed::save(ofstream &out) {
		validate();
		out << "Color: " << color[0] << " " << color[1] << endl;
		out << "Coat: " << coat[0] << " " << coat[1] << endl;
		out << "Shine: " << shine[0] << " " << shine[1] << endl;
		out << "Two-tone: " << twotone[0] << " " << twotone[1] << endl;
	} //end function

	//Prints list of valid Colors.
	void printColors() {
		for (int i = 0; i < 15; i++) {
		cout << i << ") " << CHAO_COLORS[i] << endl;
		} //end for
	} //end function

	//Prints list of valid Jewel Coats.
	void printCoats() {
		for (int i = 0; i < 18; i++) {
		cout << i << ") " << COATS[i] << endl;
		} //end for
	} //end function
