#include "Elf.h"
#include "scGame.h"
#include "Wumbo-Sprite.h"
#include "Krampus.h"
#include <cmath>

extern Wumbo::Sprite *sprElf[4];
extern Wumbo::Sprite *sprParalyze;
extern Wumbo::Sprite *sprFrozen;
extern Wumbo::Sprite *sprCoal;
extern Krampus *krampus;
extern void drawSprite(Wumbo::Sprite *spr, int x, int y, float scaleX = 1.f, float scaleY = 1.f, int r = 255, int g = 255, int b = 255, int a = -1);
extern void drawRect(int x, int y, int width, int height, int r = 255, int g = 255, int b = 255, int a = -1);
extern void drawText(std::string text, int x, int y, float scale, int r = 255, int g = 255, int b = 255);

extern int screen[BOARD_SIZE][BOARD_SIZE];
extern bool allElvesAreAtTargets();

extern int PATHFIND_SUCCESS;
extern int PATHFIND_FAILURE;
extern int PATHFIND_ENDISADJACENT;
extern int findPath(int x, int y, int destX, int destY, int *savedX, int *savedY);

void Elf::tryToAttack()
{
	if (frozenCounter > 0)
		return;
	if (paralyzeCounter > 0)
		return;
	if (krampus->getGridWithAirQuotesX() == targetGridX-1 && krampus->getGridWithAirQuotesY() == targetGridY)
	{
		attacking = 10;
		attackDir = 2;
	}
	if (krampus->getGridWithAirQuotesX() == targetGridX+1 && krampus->getGridWithAirQuotesY() == targetGridY)
	{
		attacking = 10;
		attackDir = 0;
	}
	if (krampus->getGridWithAirQuotesX() == targetGridX && krampus->getGridWithAirQuotesY() == targetGridY-1)
	{
		attacking = 10;
		attackDir = 1;
	}
	if (krampus->getGridWithAirQuotesX() == targetGridX && krampus->getGridWithAirQuotesY() == targetGridY+1)
	{
		attacking = 10;
		attackDir = 3;
	}
}
void Elf::think()
{
	if (!allElvesAreAtTargets())
		return;
	if (movesRemaining == 0)
		return;
	if (frozenCounter > 0)
	{
		frozenCounter--;
		movesRemaining--;
		return;
	}
	if (paralyzeCounter > 0)
	{
		paralyzeCounter--;
		movesRemaining--;
		if (paralyzeCounter == 0)
			setMoveCount(2);
		return;
	}

	int toX, toY;
	int result = findPath(x/TILE_SIZE,y/TILE_SIZE,krampus->getGridWithAirQuotesX(),krampus->getGridWithAirQuotesY(),&toX,&toY);

	movesRemaining--;
	if (result == PATHFIND_SUCCESS)
	{
		newTargetGridX = toX;
		newTargetGridY = toY;
		screen[targetGridX][targetGridY] = 0;
		screen[newTargetGridX][newTargetGridY] = 11;
	}
	if (result == PATHFIND_ENDISADJACENT)
	{
		// attack
	}
}
Elf::Elf(scGame *g) : Thinker(g)
{
	//Thinker::Thinker(g);
	me = rand() % 4;
	hp = 2;
	frozenCounter = 0;
	paralyzeCounter = 0;
}
bool Elf::isMyTurn()
{
	return myTurn;
}
void Elf::setMyTurn(bool turn)
{
	if (turn)
	{
		myTurn = true;
		movesRemaining = maxMoves;
	}
	else
		myTurn = false;
}
void Elf::doMyTurn()
{
	if (!myTurn)
		return;
	if ((targetGridX != newTargetGridX || targetGridY != newTargetGridY) && noElvesAt(newTargetGridX,newTargetGridY))
	{
		targetGridX = newTargetGridX;
		targetGridY = newTargetGridY;
	}
		
	//printf("targetX: %i, targetY: %i\n",targetGridX,targetGridY);
	bool atTarget = false;
	if (x > targetGridX*TILE_SIZE)
		x -= 8*maxMoves;
	if (x < targetGridX*TILE_SIZE)
		x += 8*maxMoves;
	if (y > targetGridY*TILE_SIZE)
		y -= 8*maxMoves;
	if (y < targetGridY*TILE_SIZE)
		y += 8*maxMoves;

	/*if (x == targetGridX*TILE_SIZE)
		if (y == targetGridY*TILE_SIZE)
			screen[targetGridX][targetGridY] = 0;*/
}
bool Elf::isEndOfTurn()
{
	return (movesRemaining == 0 && x == targetGridX*TILE_SIZE && y == targetGridY*TILE_SIZE);
}
void Elf::setMoveCount(int count)
{
	maxMoves = count;
}
void Elf::render()
{
	drawSprite(sprElf[me],x,y);	
	if (paralyzeCounter > 0)
		drawSprite(sprParalyze,x,y);
	if (frozenCounter > 0)
		drawSprite(sprFrozen,x,y);
	if (attacking > 0)
	{
		int o = TILE_SIZE*(float)(10-attacking)/10.f;
		if (attackDir == 0)
			drawSprite(sprCoal,x+o,y);
		if (attackDir == 1)
			drawSprite(sprCoal,x,y-o);
		if (attackDir == 2)
			drawSprite(sprCoal,x-o,y);
		if (attackDir == 3)
			drawSprite(sprCoal,x,y+o);
		attacking--;
		if (attacking == 0)
			krampus->hp--;
	}
	if (hp <= 0)
	{
		screen[targetGridX][targetGridY] = 0;
		die();
	}
}
void Elf::paralyzeMe()
{
	if (paralyzeCounter == 0)
	{
		//hp--;
		paralyzeCounter = 3*maxMoves;
	}
	if (hp <= 0)
	{
		screen[targetGridX][targetGridY] = 0;
		die();
	}
}
void Elf::freezeMe()
{
	if (frozenCounter == 0)
	{
		//hp--;
		frozenCounter = 3;
	}
	if (hp <= 0)
	{
		screen[targetGridX][targetGridY] = 0;
		die();
	}
}
bool Elf::isParalyzed()
{
	return paralyzeCounter > 0;
}
bool Elf::isFrozen()
{
	return frozenCounter > 0;
}