#include "ClapTrap.hpp"

/*
 * default/parametric constructor
 */

ClapTrap::ClapTrap( void ) {
	std::cout << "claptrap created without name\n";
	return;
}

/*
 * destructor
 */

ClapTrap::~ClapTrap( void ) {
	std::cout << "claptrap " << _name << " destructed\n";
	return;
}

/*
 * copy constructor
 */

ClapTrap::ClapTrap( ClapTrap const & src ) {
	std::cout << "claptrap " << _name << " copied\n";
	*this = src;
	return;
}

/*
 * assignement operator
 */

ClapTrap &	ClapTrap::operator=( ClapTrap const & rhs ) {
	std::cout << "claptrap " << _name << " assigned\n";

	if ( this != &rhs )
	{
		this->_hit = rhs.getHit();
		this->_energy = rhs.getEnergy();
		this->_attack = rhs.getAttack();
	}

	return *this;

}

/*
 * constructor
 */

ClapTrap::ClapTrap( std::string name ) : _name(name) {
	std::cout << "claptrap " << _name << " created\n";
	_hit = 10;
	_energy = 10;
	_attack = 1;
	return;
}

/*
 * getters
 */

std::string	ClapTrap::getName() const {return _name;}
int			ClapTrap::getHit() const {return _hit;}
int			ClapTrap::getEnergy() const {return _energy;}
int			ClapTrap::getAttack() const {return _attack;}

/*
 * robots
 */

void	ClapTrap::attack(const std::string & target) {

	std::ostringstream action;
	std::ostringstream state;

	state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
	action << " ClapTrap " << _name;

	if (_energy && _hit)
	{
		_energy--;
		action << " attacked " << target << ", causing " B_YELLOW << _attack << RESET << " points of damage!" << '\n';
	}
	else
	{
		_attack = 0;
		if (!_energy)
			action << "cannot attack because " B_RED " is out of energy\n" RESET;
		else if (!_hit)
			action << "cannot attack because " B_RED " is out of hit\n" RESET;
	}

	state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
	std::cout << state.str() << action.str();
}

void	ClapTrap::takeDamage(unsigned int amount) {

	std::ostringstream action;
	std::ostringstream state;

	state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
	action << " ClapTrap " << _name;

	if (_energy && _hit)
	{
		_hit -= amount;
		action << " looses " B_YELLOW << amount << RESET << " points of damage :/" << '\n';
	}
	else
	{
		if (!_energy)
			action << "cannot take damage because " B_RED " is out of energy\n" RESET;
		else if (!_hit)
			action << "cannot take damage because " B_RED " is out of hit\n" RESET;
	}

	state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
	std::cout << state.str() << action.str();
}

void	ClapTrap::beRepaired(unsigned int amount) {

	std::ostringstream action;
	std::ostringstream state;

	state << B_CYAN "[" B_PURPLE "h,e,a" B_CYAN ":" B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "->";
	action << " ClapTrap " << _name;

	if (_energy && _hit)
	{
		_energy--;
		_hit += amount;
		action << " repaired itself and gained " B_YELLOW << amount << RESET << " points of life :)" << '\n';
	}
	else
	{
		if (!_energy)
			action << "cannot repair itself because " B_RED " is out of energy\n" RESET;
		else if (!_hit)
			action << "cannot repair itself because " B_RED " is out of hit\n" RESET;
	}

	state << B_BLUE << _hit << "," << _energy << "," << _attack << B_CYAN "]" RESET;
	std::cout << state.str() << action.str();
}
