//Program: Stat
//Author: Jeffery Wright
//Date: 2014/12/21
//Description: Defines behavior for a Chao's stats.

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

#include "Stat.h"

using namespace std;

//Constructs a new stat.
Stat::Stat() {
	value = 0;
	level = 0;
	experience = 0;
	rank[0] = rand() % 6;
	rank[1] = rank[0];
	rank[2] = rank[0];
} //end constructor
	
//Creates a new stat based on the stats from two parents.
Stat::Stat(Stat parent1, Stat parent2) {
	value = 0;
	level = 0;
	experience = 0;
	rank[1] = parent1.getRank(rand() % 3);
	rank[2] = parent2.getRank(rand() % 3);
	rank[0] = rank[rand() % 2 + 1];
} //end constructor
	
//Loads a Stat from the given ifstream.
Stat::Stat(ifstream &in) {
	in >> value >> level >> experience;
	in >> rank[0] >> rank[1] >> rank[2];
	validate();
} //end constructor
	
//The stat gains one experience level and its experience is reset.
void Stat::levelUp() {
	if (level < 99) {
		level++;
		experience -= 100;
		int variation = rand() % 5;
		value += (rank[0] * 3) + 11 + variation;
	} else {
		experience = 100;
	} //end if
} //end function

//Improves the stat's Rank by 1 provided it is not maxed.
void Stat::rankUp() {
	if (rank[0] < 5) {
		rank[0]++;
	} //end if
} //end function
	
//The stat is reset to level 1 and reduced to 10% for reincarnation.
void Stat::reincarnate() {
	level = 1;
	value = value/10;
	experience = 0;
} //end function
	
//Prints a String representing progress towards the next level.
void Stat::experienceBar() {
	int exp = experience/10;
	for (int i = 1; i <= 10; i++) {
		if (i <= exp) {
			cout << "+";
		} else {
			cout << "=";
		} //end if
	} //end for
} //end function

//Increases current experience by the given amount.
void Stat::experienceBoost(int exp) {
	experience += exp;
	if (experience >= 100) {
		levelUp();
	}  else if (experience < 0) {
		experience = 0;
	} //end if
} //end function
	
//validates the stat.
void Stat::validate() {
	if (value < 0) {value = 0;}
	if (level < 0) {level = 0;}
	if (level > 100) {level = 100;}
	if (experience < 0) {experience = 0;}
	if (experience > 100) {experience = 100;}
	for (int i = 0; i < 3; i++) {
		if (rank[i] < 0) {rank[i] = 0;}
		if (rank[i] > 5) {rank[i] = 5;}
	} //end for
} //end function

//Prints the Stat
void Stat::print() {
	cout << getRankLetter() << " " << level << " ";
	experienceBar();
	cout << " " << value << endl;
} //end function

//Saves the stat.
void Stat::save(ofstream &out) {
	validate();
	out << value << " " << level << " " << experience << endl;
	out << rank[0] << " " << rank[1] << " " << rank[2] << endl;
} //end function
