#include <iostream>
#include <iomanip>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include <string>
using namespace std;

#include "common.h"
#include "weapon.h"
#include "player.h"


// the player constructor
player::player()
{
	myMonies=0;
	runningShoes=false;
	lives=STARTING_LIVES;

	// start out in the center of the grid
	alive=true;
	myPosition.row = int(GRID_ROWS/2);
	myPosition.col = int(GRID_COLS/2);
	setIcon();

	// set up weapons
	myWeapons[weapon_pistol]	= new pistol();
	myWeapons[weapon_rifle]		= new rifle();
	myWeapons[weapon_mine]		= new mine();
	activeWeapon=0;
}

player::~player()
{
	for ( int i=0; i < NUM_WEAPONS; i++ )
	{
		delete myWeapons[i];
	}
}

bool player::respawn()
{
	if ( lives > 1 ) {
		lives--;
		// start out in the center of the grid
		alive=true;
		myPosition.row = int(GRID_ROWS/2);
		myPosition.col = int(GRID_COLS/2);
		setIcon();
		return true;
	} else
		return false;
}

void player::die()
{
	square *sq=gridSquare(myPosition);

	sq->sqIcon=CORPSE_ICON;
	sq->BGcolor=CORPSE_COLOR;
	sq->FGcolor=BKG_COLOR;
	alive=false;
}

void player::move(int direction)
{
	square *sq=gridSquare(myPosition);

	if ( sq->sqIcon == PLAYER_ICON ) {
		sq->sqIcon=EMPTY_ICON;
		sq->BGcolor=black;
		sq->FGcolor=BKG_COLOR;
		updateGridSquare(myPosition);
	}

	switch ( direction )
	{
		case DIR_UP:
			myPosition.row--;
			if ( myPosition.row < 0 ) myPosition.row=0;
			break;
		case DIR_DOWN:
			myPosition.row++;
			if ( myPosition.row >= GRID_ROWS ) myPosition.row=GRID_ROWS-1;
			break;
		case DIR_LEFT:
			myPosition.col--;
			if ( myPosition.col < 0 ) myPosition.col=0;
			break;
		case DIR_RIGHT:
			myPosition.col++;
			if ( myPosition.col >= GRID_COLS ) myPosition.col=GRID_COLS-1;
			break;
	}

	sq=gridSquare(myPosition);

	// did I just step on a zombie?
	if ( sq->sqIcon == ZOMBIE_ICON )
		die();
	else if ( sq->sqIcon == MINE_ICON )
	{	// did I step onto one of my own mines?
		die();
		sq->BGcolor=BRyellow;
		sq->FGcolor=CORPSE_COLOR;
	} else if ( sq->sqIcon == CORPSE_ICON )
	{
		tryLoot();
		setIcon();
	} else if ( sq->sqIcon == EXIT_ICON )
	{	// DONT SET ICON!
	} else {
		setIcon();
	}
} // end move

void player::tryLoot()
{
	myMonies += rand()%11;
}

void player::toggleWeapon()
{
	activeWeapon++;
	if ( activeWeapon >= NUM_WEAPONS ) activeWeapon=0;
}

void player::displayWeapon() { 
	assert (activeWeapon >=0 && activeWeapon < NUM_WEAPONS );
	myWeapons[activeWeapon]->display(); 
}

void player::useWeapon(int direction)
{
	myWeapons[activeWeapon]->fire(direction,myPosition);
}

void player::setIcon() 
{
	square *sq=gridSquare(myPosition);

	sq->sqIcon=PLAYER_ICON;
	sq->BGcolor=BRwhite;
	sq->FGcolor=BKG_COLOR;

	updateGridSquare(myPosition);
}

void player::shop(int gameLevel)
{
	char choice=NULL;
	do {
		setColor(black,green);
		system("cls");

		cout << "Welcome to the level " << gameLevel << " shop.  A word of advice; \n";
		cout << "Ammo is ammo no matter what level you buy it on, \n";
		cout << "but at higher levels there are more zombies.\n";
		cout << "Because of the law supply & demand that means ammo will cost more.\n";
		cout << "So if you can afford it, buy it from me, now.\n";

		setColor(black,BRyellow);
		cout << "\nYou have " << myMonies << " monies\n";
		setColor(black,BRwhite);

		for ( int w=0; w < NUM_WEAPONS; w++ )
		{
			cout << (w+1) << ": " ;
			myWeapons[w]->display();
			cout << "Ammo Cost: " << myWeapons[w]->getAmmoCost(gameLevel) << endl;				
		}

		cout << left << setw(10) << "L: Extra Life" << "    : " << setw(4) << lives ;
		cout << "     Cost: " << EXTRA_LIFE_COST << endl;

		if ( !runningShoes ) cout << "R: Running shoes\t    Cost: " << RUNNING_SHOES_COST << endl;

		cout << "Q: Quit\n";
		setColor(black,BRcyan);
		cout << "Choice: ";

		fflush(stdin);
		choice=_getch();
		choice=toupper(choice);

		if ( choice == 'R' && !runningShoes)
		{
			if ( myMonies >= RUNNING_SHOES_COST )
			{
				myMonies-=RUNNING_SHOES_COST;
				runningShoes=true;
				cout << "\nTo use your running shoes run with keypad (with numlock on)\nAlso allows diagonal movement.\n";
				system("pause");
			}
		} else if ( choice == 'L' )
		{
			if ( myMonies >= EXTRA_LIFE_COST )
			{
				lives++;
				myMonies-=EXTRA_LIFE_COST;
			}
		}

		int intChoice = choice-49;
		if ( intChoice >= 0 && intChoice < NUM_WEAPONS )
		{
			int quant;
			cout << "\nHow many?: ";
			cin >> quant;
			if ( quant < 0 ) quant=0;

			int cost=myWeapons[intChoice]->getAmmoCost(gameLevel) * quant;
			if ( myMonies >= cost )
			{
				myMonies-=cost;
				myWeapons[intChoice]->addAmmo(quant);
			}
		} 
	} while ( choice != 'Q' );
}

void player::checkStatus()
{
	square *sq=gridSquare(myPosition);
	// I may have been killed by a weapon and don't know it. check...
	if ( sq->sqIcon == CORPSE_ICON )	
		die();
}