#include <iostream>
using namespace std;
#include "OSspec.h"
#include "Player.h"
#include "Monster.h"
#include "random.h"

Player::Player() {
	lvl = 1;
	skill = 10;
	health = 100;
	gold = 10;
}

Player::~Player() {
	cout << "You have a long and boring funeral." << endl;
	cout << "GAME OVER";
}
	
void Player::findMonster() {
	Monster monster(lvl, skill);
	char ans;
	cout << "You find a level " << monster.getLvl() << " monster with " << monster.getHealth() << "/" << 75 * monster.getLvl() << " in health and " << monster.getSkill() << " skill points." << endl;
	cout << "Attack [y/N]? ";
	ans = inputCh();
	cout << endl << endl;
	bool flee = false;
	if (tolower(ans) == 'y') {
		while((health > 0) && (monster.getHealth() > 0) && (!flee)) {
			const int NPHit = monster.sendAttack(lvl, skill);
			if (NPHit > 0) {
				cout << "The monster ";
				health -= NPHit;
				if (health > 0) {
					cout << "attacks you and decreases your health to " << health << "/" << 100 * lvl << ".";
				} else {
					health = 0;
					cout << "kills you!";
				}
			} else {
				cout << "You evade the monster's attack!";
			}
			cout << endl;
			if (health > 0) {
				const int FPHit = monster.recvAttack(lvl, skill);
				if (FPHit > 0) {
					cout << "You attack the monster and ";
					if (monster.getHealth() > 0) {
						cout << "decrease his health to " << monster.getHealth() << "/" << 75 * monster.getLvl() << ".";
					} else {
						cout << "kill it!";
					}
				} else {
					cout << "The monster evades your attack!";
				}
				cout << endl;
			}
			if ((health > 0) && (monster.getHealth() > 0)) {
				cout << "Attack again [Y/n]? ";
				ans = inputCh();
				cout << endl;
				if (tolower(ans) == 'n') {
					flee = true;
				}
			}
			cout << endl;
		}
		if (!flee) {
			if (health > 0) {
				const int lvl_inc = (monster.getLvl() - lvl) + 1;
				if (lvl_inc > 0) {
					lvl += lvl_inc;
					cout << "You gain " << lvl_inc << " level" << (lvl_inc > 1 ? "s" : "") << ", then you receive:" << endl;
					health += (lvl_inc * 100);
					cout << "* " << lvl_inc * 100 << " health points (New maximum is " << lvl * 100 << ")" << endl;
					const int skill_inc = (monster.getSkill() / 3) * (lvl_inc + 1);
					skill += skill_inc;
					cout << "* " << skill_inc << " skill points" << endl;
				} else {
					cout << "You don't gain any level, but you receive:" << endl;
					const int skill_inc = monster.getSkill() / 3;
					skill += skill_inc;
					cout << "* " << skill_inc << " skill points" << endl;
				}
				gold += monster.getGold();
				cout << "* " << monster.getGold() << " gold";
			}
		} else {
			cout << "You run away from the monster." << endl << "By running away from a fight, you loose ";
			if ((random(1,3) == 2) && (gold > 0)) {
				gold = 0;
				cout << "all your gold and ";
			}
			skill -= skill / 10;
			cout << "10% of your skill.";
		}
	} else {
		cout << "You quickly hide yourself from the monster.";
	}
}

void Player::findItem() {	
	cout << "You find an item that gives you:" << endl;
	if (health < lvl * 100) {
		int health_inc = random((lvl * 10), (lvl * 50));
		if ((health + health_inc) > (lvl * 100)) {
			health_inc = (lvl * 100) - health;
		}
		if (health_inc > 0) {
			health += health_inc;
			cout << "* " << health_inc << " health points" << endl;
		}
	}
	const int skill_inc = random(2, random(5,10)) * lvl;
	skill += skill_inc;
	cout << "* " << skill_inc << " skill points" << endl;
	const int gold_inc = random(1, random(5,50)) * lvl;
	gold += gold_inc;
	cout << "* " << gold_inc << " gold";
}

void Player::findTrainer() {
	const int priceCl = random(5,15), cost = priceCl * lvl;
	cout << "You find a trainer that can give you a training pass for " << cost << " gold." << endl;
	if (cost > gold) {
		cout << "Sadly, you can't afford it.";
	} else {
		cout << "Want to try [Y/n]? ";
		const char ans = inputCh();
		if (tolower(ans) != 'n') {
			const int skill_inc = (skill * random(priceCl - 4, 11)) / 30; //higher price class = higher chance for more skill...
			skill += skill_inc;
			gold -= cost;
			cout << endl << endl << "You gain " << skill_inc << " skill points.";
			if (health < lvl * 100) {
				cout << endl << "The trainer also gives you a health potion that restores ";
				if ((health > (lvl * 75)) || (random(priceCl, 15) == 15)) { //...and full-health potion
					health = lvl * 100;
					cout << "all";
				} else {
					health += lvl * 25;
					cout << "25% of";
				}
				cout << " your health.";
			}
		}
	}
}

const int Player::getHealth() {
	return health;
}

void Player::printData() {
	cout << "Level: " << lvl << endl;
	cout << "Health: " << health << "/" << 100 * lvl << endl;
	cout << "Skill: " << skill << endl;
	cout << "Gold: " << gold << endl;
	cout << endl;
}

