#include "Player.hpp"

Player::Player(const std::string& name, unsigned int seed, short color):
	name(name), seed(seed), color(color), pawnsFinished(0) {}

const std::string& Player::getName() const {
	return name;
}

unsigned int Player::getSeed() const {
	return seed;
}

void Player::initializePawns(int totalPawns) {
	pawns.resize(totalPawns);
}

int Player::getRollCount() const {
	return this->rollCount;
}

void Player::incrementRollCount() {
	this->rollCount++;
}

short Player::getColor() const {
	return this->color;
}

short Player::tryMovingPawn(short pawnID, short fields) {
	return 0;
}

void Player::movePawn(int pawnIndex, int steps) {
	return;
}

bool Player::hasWon() const {
	short lastPosition = 43;
	short totalPawnsAtHome = 0;
	// szuka wartości pomiędzy 40-43
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (this->relativePawns[j] == lastPosition - i) totalPawnsAtHome++;
		}
	}
	#ifdef DEBUG
		std::cout << "(Player.cpp) totalPawnsAtHome: " << totalPawnsAtHome << "\n";
	#endif
	return totalPawnsAtHome == 4;
}

short* Player::getRelativePawns() {
	return this->relativePawns;
}

void Player::recalculatePawnStates() {
	short temp_atBase   =  0;
	short temp_Active   =  0;
	short temp_atHome   =  0;
	short temp_finished =  0;
	short lastPosition  = 44;
	for (int i = 0; i < 4; i++) {
		if (this->relativePawns[i] == -1) {
			temp_atBase++;
			continue;
		}
		// niedokończone...
	}
}

void Player::sendToBase(short pawnNumber) {
	this->relativePawns[pawnNumber] = -1;
	this->pawnsActive--;
	this->pawnsAtBase++;
}

void Player::sendToBoard(short pawnNumber) {
	this->hasLeftBase = true;
	this->relativePawns[pawnNumber] = 0;
	this->pawnsActive++;
	this->pawnsAtBase--;
}

void Player::unsafeMovePiece(short pawnNumber, short steps) {
	// to dopełnia obecną w Engine.cpp movePiece()
	// metoda jest "unsafe" ponieważ nie sprawdza danych, tylko ufa movePiece()
	if (this->relativePawns[pawnNumber] >= 39 && !this->isPawnAtHome(pawnNumber)) { 
		this->pawnsActive--;
		this->pawnsAtHome++;
	}
	this->relativePawns[pawnNumber] += steps;
}

/**
 * @brief      Determines if pawn is at home.
 *
 * @param[in]  color   Player's color
 * @param[in]  pawnId  The pawn's unique identifier
 *
 * @return     True if pawn at home, false otherwise.
 */
bool Player::isPawnAtHome(short pawnId) {
	return this->relativePawns[pawnId] >= 44 - 4;
}