#include "Krampus.h"
#include "scGame.h"
#include "Wumbo-Sprite.h"
#include "Wumbo-Program.h"
#include "Wumbo-InputManager.h"
#include "Wumbo-KeyboardInput.h"
#include "Elf.h"
#include "CandyCane.h"

extern Wumbo::Sprite *sprKrampus;
extern Wumbo::Sprite *sprAttacks[9];
extern Wumbo::Sprite *sprAttacksVERT[9];
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 distance(int x, int y, int x2, int y2);
extern std::vector<Elf*> elves;
extern int screen[BOARD_SIZE][BOARD_SIZE];
extern Wumbo::Sprite *sprIce;

extern int CHOSEN_REINDEER;
extern int CANDYCANES;

void Krampus::think()
{
}
Krampus::Krampus(scGame *g) : Thinker(g)
{
	//Thinker::Thinker(g);
	specialMove = false;
	hp = 9;
	PRANCER_POWERS_ACTIVATED = false;
	specialAttack = false;
}
bool Krampus::isMyTurn()
{
	return myTurn;
}
void Krampus::setMyTurn(bool turn)
{
	if (turn)
	{
		myTurn = true;
		movesRemaining = 1;
		//movesRemaining = maxMoves;
	}
	else
		myTurn = false;
}
void Krampus::move(int dx, int dy)
{
	Elf *enemy = getElfInPath(targetGridX+dx,targetGridY+dy);
	if (enemy == NULL)
	{
		for(int i=1;i<2+PRANCER_POWERS_ACTIVATED;i++)
		{
			if (screen[targetGridX+dx*i][targetGridY+dy*i] == 0 && noElvesAt(targetGridX+dx*i,targetGridY+dy*i))
			{
				screen[targetGridX][targetGridY] = 0;
				newTargetGridX += dx*i;
				newTargetGridY += dy*i;
				movesRemaining--;
				screen[newTargetGridX][newTargetGridY] = 20;
				if (i == 2)
				{
					printf("i = 2\n");
					if (PRANCER_POWERS_ACTIVATED)
					{
						printf("DEACTIVATE PRANCER\n");
						PRANCER_POWERS_ACTIVATED = false;
						specialMove = true;
					}
				}
				i = 10;
			}
		}
	}
	else
	{
		enemy->hp--;
		if (dx == 1 && dy == 0)
			attackDir = 0;
		if (dx == 0 && dy == -1)
			attackDir = 1;
		if (dx == -1 && dy == 0)
			attackDir = 2;
		if (dx == 0 && dy == 1)
			attackDir = 3;
		specialAttack = false;
		attacking = 10;
		movesRemaining--;
	}
	/*if (targetGridX+dx >= 0 && targetGridY+dy >= 0 && targetGridX+dx < BOARD_SIZE && targetGridY+dy < BOARD_SIZE)
	{
		bool shouldMove = true;
		if (CHOSEN_REINDEER == COMET || CHOSEN_REINDEER == BLITZEN)
		{
			for(int i=1;i<BOARD_SIZE;i++)
			{
				Elf *theElf = NULL;
				if (targetGridX+dx*i < 0 || targetGridY+dy*i < 0 || targetGridX+dx*i >= BOARD_SIZE || targetGridY+dy*i >= BOARD_SIZE)
					i = BOARD_SIZE;
				else if (screen[targetGridX+dx*i][targetGridY+dy*i] == 1 || screen[targetGridX+dx*i][targetGridY+dy*i] == 2)
					i = BOARD_SIZE;
				else if ((theElf = getElfInPath(targetGridX+dx*i,targetGridY+dy*i))) // if there are hazardous elves
				{
					if ((!theElf->isParalyzed() && !theElf->isFrozen()) || i == 1)
					{
						shouldMove = false;
						movesRemaining--;
						attacking = 10;
						distanceToEnemy = i;
						if (dx > 0 && dy == 0)
							attackDir = 0;
						if (dx == 0 && dy < 0)
							attackDir = 1;
						if (dx < 0 && dy == 0)
							attackDir = 2;
						if (dx == 0 && dy > 0)
							attackDir = 3;
						if (CHOSEN_REINDEER == COMET)
							theElf->freezeMe();
						if (CHOSEN_REINDEER == BLITZEN)
							theElf->paralyzeMe();
					}
					i = BOARD_SIZE;
				}
			}
		}
		else if (CHOSEN_REINDEER == VIXEN)
		{
			for(int i=1;i<BOARD_SIZE;i++)
			{
				Elf *theElf = NULL;
				if (targetGridX+dx*i < 0 || targetGridY+dy*i < 0 || targetGridX+dx*i >= BOARD_SIZE || targetGridY+dy*i >= BOARD_SIZE)
					i = BOARD_SIZE;
				else if (screen[targetGridX+dx*i][targetGridY+dy*i] == 1 || screen[targetGridX+dx*i][targetGridY+dy*i] == 2)
				{
					
					int atme = screen[targetGridX][targetGridY];
					screen[targetGridX][targetGridY] = screen[targetGridX+dx*i][targetGridY+dy*i];
					screen[targetGridX+dx*i][targetGridY+dy*i] = atme;
					targetGridX = newTargetGridX = targetGridX+dx*i;
					targetGridY = newTargetGridY = targetGridY+dy*i;
					attacking = 10;
					distanceToEnemy = i;
					i = BOARD_SIZE;
				}
				else if ((theElf = getElfInPath(targetGridX+dx*i,targetGridY+dy*i))) // if there are hazardous elves
				{
					shouldMove = false;
					movesRemaining--;
					attacking = 10;
					distanceToEnemy = i;
					if (dx > 0 && dy == 0)
						attackDir = 0;
					if (dx == 0 && dy < 0)
						attackDir = 1;
					if (dx < 0 && dy == 0)
						attackDir = 2;
					if (dx == 0 && dy > 0)
						attackDir = 3;
					screen[theElf->targetGridX][theElf->targetGridY] = 0;
					screen[targetGridX][targetGridY] = 0;
					theElf->newTargetGridX = targetGridX;
					theElf->newTargetGridY = targetGridY;
					newTargetGridX = theElf->targetGridX;
					newTargetGridY = theElf->targetGridY;
					i = BOARD_SIZE;
				}
			}
			/*int vixX, vixY;
			for(int vixDir=0;vixDir<4;vixDir++)
			{
				if (vixDir == 0)
				{
					vixX = 1;
					vixY = -1;
				}
				if (vixDir == 1)
				{
					vixX = -1;
					vixY = -1;
				}
				if (vixDir == 2)
				{
					vixX = -1;
					vixY = 1;
				}
				if (vixDir == 3)
				{
					vixX = 1;
					vixY = 1;
				}
				for(int i=1;i<BOARD_SIZE;i++)
				{
					Elf *theElf = NULL;
					bool foundOne = false;
					if (screen[targetGridX+dx*i][targetGridY+dy*i] == 1 || screen[targetGridX+dx*i][targetGridY+dy*i] == 2)
						i = BOARD_SIZE;
					else if ((theElf = getElfInPath(targetGridX+vixX*i,targetGridY+vixY*i))) // if there are hazardous elves
					{
						if (vixDir == 0)
							vix45 = theElf;
						if (vixDir == 1)
							vix135 = theElf;
						if (vixDir == 2)
							vix225 = theElf;
						if (vixDir == 3)
							vix315 = theElf;
						shouldMove = false;
						foundOne = true;
						movesRemaining--;
						attacking = 10;
						//distanceToEnemy = i;
						if (vixX == 1 && vixY == -1)
							vixAttackDist45 = i;
						if (vixX == -1 && vixY == -1)
							vixAttackDist135 = i;
						if (vixX == -1 && vixY == 1)
							vixAttackDist225 = i;
						if (vixX == 1 && vixY == 1)
							vixAttackDist315 = i;
						i = BOARD_SIZE;
					}
					if (!foundOne)
					{
						if (vixX == 1 && vixY == -1)
							vixAttackDist45 = -1;
						if (vixX == -1 && vixY == -1)
							vixAttackDist135 = -1;
						if (vixX == -1 && vixY == 1)
							vixAttackDist225 = -1;
						if (vixX == 1 && vixY == 1)
							vixAttackDist315 = -1;
					}
				} // end distance iteration
			} // end direction iteration
			
		}// end vixen
		if (shouldMove)
		{
			if (screen[targetGridX+dx][targetGridY+dy] == 0 && noElvesAt(targetGridX+dx,targetGridY+dy))
			{
				screen[targetGridX][targetGridY] = 0;
				newTargetGridX += dx;
				newTargetGridY += dy;
				movesRemaining--;
				screen[newTargetGridX][newTargetGridY] = 20;
			}
			else if (DASHER_POWERS_ENABLED)
			{
				if (targetGridX+dx*2 >= 0 && targetGridY+dy*2 >= 0 && targetGridX+dx*2 < BOARD_SIZE && targetGridY+dy*2 < BOARD_SIZE)
					if (!noElvesAt(targetGridX+dx,targetGridY+dy)
						&& screen[targetGridX+dx*2][targetGridY+dy*2] == 0 && noElvesAt(targetGridX+dx*2,targetGridY+dy*2))
					{
						screen[targetGridX][targetGridY] = 0;
						newTargetGridX += dx*2;
						newTargetGridY += dy*2;
						oldTargetGridX = targetGridX;
						oldTargetGridY = targetGridY;
						movesRemaining -= 1;
						screen[newTargetGridX][newTargetGridY] = 20;
						specialMove = true;
					}
			}
			else if (PRANCER_POWERS_ENABLED)
			{
				screen[targetGridX][targetGridY] = 0;
				do
				{
					newTargetGridX = rand() % BOARD_SIZE;
					newTargetGridY = rand() % BOARD_SIZE;
				}
				while(screen[newTargetGridX][newTargetGridY] != 0 || !noElvesAt(newTargetGridX,newTargetGridY));
				oldTargetGridX = targetGridX;
				oldTargetGridY = targetGridY;
				screen[newTargetGridX][newTargetGridY] = 20;
				movesRemaining -= 1;
				specialMove = true;
			}
			else if (DANCER_POWERS_ENABLED)
			{
				if (targetGridX+dx*2 >= 0 && targetGridY+dy*2 >= 0 && targetGridX+dx*2 < BOARD_SIZE && targetGridY+dy*2 < BOARD_SIZE)
					if ((screen[targetGridX+dx][targetGridY+dy] != 0 || noElvesAt(targetGridX+dx,targetGridY+dy))
						&& screen[targetGridX+dx*2][targetGridY+dy*2] == 0 && noElvesAt(targetGridX+dx*2,targetGridY+dy*2))
					{
						screen[targetGridX][targetGridY] = 0;
						newTargetGridX += dx*2;
						newTargetGridY += dy*2;
						movesRemaining -= 1;
						screen[newTargetGridX][newTargetGridY] = 20;
						specialMove = true;
					}
			}
		}
	}*/
}
void Krampus::doMyTurn()
{
	if (!myTurn)
		return;
	Wumbo::KeyboardInput *keyboard = game->getProgram()->getInputManager()->getKeyboardState();
	/*if (CHOSEN_REINDEER == VIXEN)
	{
		if (((keyboard->isKeyPressed(sf::Keyboard::Right) && keyboard->isKeyDown(sf::Keyboard::Up)) ||
			(keyboard->isKeyDown(sf::Keyboard::Right) && keyboard->isKeyPressed(sf::Keyboard::Up))) 
			&& movesRemaining > 0 && isAtTarget())
			move(1,-1);
		if (((keyboard->isKeyPressed(sf::Keyboard::Left) && keyboard->isKeyDown(sf::Keyboard::Up)) ||
			(keyboard->isKeyDown(sf::Keyboard::Left) && keyboard->isKeyPressed(sf::Keyboard::Up))) 
			&& movesRemaining > 0 && isAtTarget())
			move(-1,-1);
		if (((keyboard->isKeyPressed(sf::Keyboard::Left) && keyboard->isKeyDown(sf::Keyboard::Down)) ||
			(keyboard->isKeyDown(sf::Keyboard::Left) && keyboard->isKeyPressed(sf::Keyboard::Down))) 
			&& movesRemaining > 0 && isAtTarget())
			move(-1,1);
		if (((keyboard->isKeyPressed(sf::Keyboard::Right) && keyboard->isKeyDown(sf::Keyboard::Down)) ||
			(keyboard->isKeyDown(sf::Keyboard::Right) && keyboard->isKeyPressed(sf::Keyboard::Down))) 
			&& movesRemaining > 0 && isAtTarget())
			move(1,1);
	}
	else*/
	if (keyboard->isKeyPressed(sf::Keyboard::Left) && movesRemaining > 0 && isAtTarget())
		move(-1,0);
	if (keyboard->isKeyPressed(sf::Keyboard::Right) && movesRemaining > 0 && isAtTarget())
		move(1,0);
	if (keyboard->isKeyPressed(sf::Keyboard::Up) && movesRemaining > 0 && isAtTarget())
		move(0,-1);
	if (keyboard->isKeyPressed(sf::Keyboard::Down) && movesRemaining > 0 && isAtTarget())
		move(0,1);
	if (keyboard->isKeyPressed(sf::Keyboard::Space) && movesRemaining > 0 && isAtTarget())
	{
		if (CANDYCANES > 0)
		{
			if ((CHOSEN_REINDEER == COMET || CHOSEN_REINDEER == BLITZEN) && elves.size() > 0)
			{
				for(int DIR=0;DIR<4;DIR++)
				{
					attackDist[DIR] = -1;
					for(int i=1;i<BOARD_SIZE;i++)
					{
						int dx, dy;
						if (DIR == 0)
						{
							dx = 1;
							dy = 0;
						}
						if (DIR == 1)
						{
							dx = 0;
							dy = -1;
						}
						if (DIR == 2)
						{
							dx = -1;
							dy = 0;
						}
						if (DIR == 3)
						{
							dx = 0;
							dy = 1;
						}
						Elf *theElf = NULL;
						if (targetGridX+dx*i < 0 || targetGridY+dy*i < 0 || targetGridX+dx*i >= BOARD_SIZE || targetGridY+dy*i >= BOARD_SIZE)
							i = BOARD_SIZE;
						else if (screen[targetGridX+dx*i][targetGridY+dy*i] == 1 || screen[targetGridX+dx*i][targetGridY+dy*i] == 2)
							i = BOARD_SIZE;
						else if ((theElf = getElfInPath(targetGridX+dx*i,targetGridY+dy*i))) // if there are hazardous elves
						{
							if ((!theElf->isParalyzed() && !theElf->isFrozen()) || i == 1)
							{
								attacking = 10;
								attackDist[DIR] = i;
								if (CHOSEN_REINDEER == COMET)
									theElf->freezeMe();
								if (CHOSEN_REINDEER == BLITZEN)
									theElf->paralyzeMe();
							}
							i = BOARD_SIZE;
						}
					}
				}
				if (attacking == 10)
				{
					specialAttack = true;
					CANDYCANES--;
				}
			}
			if (CHOSEN_REINDEER == DANCER)
			{
				screen[targetGridX][targetGridY] = 0;
				do
				{
					newTargetGridX = rand() % BOARD_SIZE;
					newTargetGridY = rand() % BOARD_SIZE;
				}
				while(screen[newTargetGridX][newTargetGridY] != 0 || !noElvesAt(newTargetGridX,newTargetGridY));
				oldTargetGridX = targetGridX;
				oldTargetGridY = targetGridY;
				screen[newTargetGridX][newTargetGridY] = 20;
				specialMove = true;
				CANDYCANES--;
			}
			if (CHOSEN_REINDEER == PRANCER && !PRANCER_POWERS_ACTIVATED)
			{
				printf("ACTIVATE PRANCER\n");
				PRANCER_POWERS_ACTIVATED = true;
				CANDYCANES--;
			}
			if (CHOSEN_REINDEER == DASHER)
			{
				CANDYCANES--;
				movesRemaining++;
			}
		}// end candycanes
	} // end keyboard
	if ((targetGridX != newTargetGridX || targetGridY != newTargetGridY) && noElvesAt(newTargetGridX,newTargetGridY))
	{
		targetGridX = newTargetGridX;
		targetGridY = newTargetGridY;
	}
	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)
			specialMove = false;

	if (isAtTarget())
	{
		CandyCane *candy = candyAt(targetGridX,targetGridY);
		if (candy != NULL)
		{
			removeCandy(candy);
			CANDYCANES++;
		}
	}
}
bool Krampus::isEndOfTurn()
{
	return (movesRemaining <= 0 && isAtTarget() && attacking == 0);
}
void Krampus::setMoveCount(int count)
{
	maxMoves = count;
}
void Krampus::render()
{
	if (specialMove || PRANCER_POWERS_ACTIVATED)
	{
		if (CHOSEN_REINDEER == DANCER)
		{
			drawSprite(sprAttacks[DANCER],oldTargetGridX*TILE_SIZE,oldTargetGridY*TILE_SIZE);
			drawSprite(sprAttacks[DANCER],newTargetGridX*TILE_SIZE,newTargetGridY*TILE_SIZE);
		}
		drawSprite(sprAttacks[PRANCER],x,y);
		drawSprite(sprKrampus,x,y-TILE_SIZE/8);	
		if (CHOSEN_REINDEER == DASHER)
		{
			float distSoFar = distance(x,y,newTargetGridX*TILE_SIZE,newTargetGridY*TILE_SIZE);
			float distTotal = distance(oldTargetGridX*TILE_SIZE,oldTargetGridY*TILE_SIZE,newTargetGridX*TILE_SIZE,newTargetGridY*TILE_SIZE);
			drawSprite(sprAttacks[DASHER],oldTargetGridX*TILE_SIZE,oldTargetGridY*TILE_SIZE-TILE_SIZE*2*(1-distSoFar/distTotal),1,1,255,255,255,255*(1-distSoFar/distTotal));
		}
	}
	else
		drawSprite(sprKrampus,x,y);	
	if (movesRemaining > 1)
		drawSprite(sprAttacks[DASHER],x,y);
	if (attacking)
	{
		if (specialAttack)
		{
			if (CHOSEN_REINDEER == COMET|| CHOSEN_REINDEER == BLITZEN)
			{
				for(attackDir=0;attackDir<4;attackDir++)
				{
					if (attackDist[attackDir] == -1)
						continue;
					float XS, YS;
					float XO, YO;
					if (attackDir == 0)//right
					{
						XS = attackDist[attackDir]*(1-(float)attacking/10.f);
						YS = 1;
						XO = -TILE_SIZE/2;
						YO = 0;
					}
					if (attackDir == 2)//left
					{
						XS = -attackDist[attackDir]*(1-(float)attacking/10.f);
						YS = 1;
						XO = TILE_SIZE/2;
						YO = 0;
					}
					if (attackDir == 1)//up
					{
						XS = 1;
						YS = -attackDist[attackDir]*(1-(float)attacking/10.f);
						XO = 0;
						YO = TILE_SIZE/2;
					}
					if (attackDir == 3)//down
					{
						XS = 1;
						YS = attackDist[attackDir]*(1-(float)attacking/10.f);
						XO = 0;
						YO = -TILE_SIZE/2;
					}
					//sprAttacks[COMET]->setTexCoordRotation(90*attackDir);
					if (attackDir % 2 == 0)
						drawSprite(sprAttacks[CHOSEN_REINDEER],x+(attackDir == 0?TILE_SIZE:0)+XO,y+(attackDir == 3?TILE_SIZE:0)+YO,XS,YS);
					else
						drawSprite(sprAttacksVERT[CHOSEN_REINDEER],x+(attackDir == 0?TILE_SIZE:0)+XO,y+(attackDir == 3?TILE_SIZE:0)+YO,XS,YS);
				}//end look direction
			}//end if REINDEER
		} // end if special
		else
		{
			int o = TILE_SIZE*(float)(10-attacking)/10.f;
			if (attackDir == 0)
				drawSprite(sprIce,x+o,y);
			if (attackDir == 1)
				drawSprite(sprIce,x,y-o);
			if (attackDir == 2)
				drawSprite(sprIce,x-o,y);
			if (attackDir == 3)
				drawSprite(sprIce,x,y+o);
		}
	}// end if ATTACKING
	if (attacking > 0)
		attacking--;
}