/*
 * Player
 * 
 * Display the player and handle the keyboard events.
 */
 
/**
 * Constructor.
 *
 * @param game instance of the Game class
 * @param gameContainer html element where to add the player
 * @param map object
 */
function Player(game, gameContainer, map)
{
	//Constants
	this.TIMER_INTERVAL = 30;
	this.PLAYER_SPRITES_FOLDER = 'game/images/sprites/mario_map';

	//Attributes
	this.game = game;
	this.map = map;
	this.timer = null;
	this.motionManager = new MotionManager(this); // Handle the player movements
	
	//Get the player position. The origin is the first sprite of the tilemap.
	var gridPosition = this.getInitialGridPosition();
	this.position = {"x":gridPosition.x*map.SPRITE_SIZE, "y":gridPosition.y*map.SPRITE_SIZE};
	
	//Add the player in the game container
	$(gameContainer).append('<div id="player">&nbsp;</div>');
	$("#player").css('width', map.SPRITE_SIZE);
	$("#player").css('height', map.SPRITE_SIZE);
	$("#player").css('position', 'relative');
	$("#player").append('<img class="player" id="player_down" alt="D" src="'+this.PLAYER_SPRITES_FOLDER+'/down.gif"/>');
	$("#player").append('<img class="player" id="player_left" alt="L" src="'+this.PLAYER_SPRITES_FOLDER+'/left.gif"/>');
	$("#player").append('<img class="player" id="player_right" alt="R" src="'+this.PLAYER_SPRITES_FOLDER+'/right.gif"/>');
	$("#player").append('<img class="player" id="player_up" alt="U" src="'+this.PLAYER_SPRITES_FOLDER+'/up.gif"/>');
	$("#player img.player").css('position', 'relative');
	$("#player img.player").css('top', '-60');
	$("#player img.player").css('left', '-16');
	$("#player img.player").css('display', 'none');
	$("#player img#player_down").css('display', 'block');
	
	//Draw the player and move the map in order to show him in the center
	this.drawPlayerOnMap();
	map.showPlayerInTheCenter(this);
}

/**
 * Get the initial position of the player on the map.
 * The origin is the first sprite of the tilemap.
 *
 * @return point object {x,y}
 */
Player.prototype.getInitialGridPosition = function()
{
	//Get the initial position: look for the teleporter number 1.
	var teleporters = this.map.getTeleporters();
	for(var rowIndex in teleporters)
	{
		var line = teleporters[rowIndex];
		for(var lineIndex in line)
		{
			if(line[lineIndex] == 1) return {"x":Number(lineIndex), "y":Number(rowIndex)};
		}
	}
	
	return {"x":0, "y":0};
}

/**
 * Get the position of the player in the grid.
 *
 * @return point object {x,y}
 */
Player.prototype.getGridPosition = function()
{
	return {"x":Math.floor(this.position.x/this.map.SPRITE_SIZE), "y":Math.floor(this.position.y/this.map.SPRITE_SIZE)};
}

/**
 * Draw the player on the map. Move the map if the player is not in his area.
 */
Player.prototype.drawPlayerOnMap = function()
{
	//Set the div#player position
	$("#player").css('left', this.map.paddingH*this.map.SPRITE_SIZE+this.position.x);
	$("#player").css('top', (-this.map.paddingV-this.map.mapHeight)*this.map.SPRITE_SIZE+this.position.y);
	
	//Diplay the right image
	$("#player img.player").css('display', 'none');
	switch(this.motionManager.direction)
	{
		case this.motionManager.KEY_LEFT:
			$("#player img#player_left").css('display', 'block');
			break;
		case this.motionManager.KEY_RIGHT:
			$("#player img#player_right").css('display', 'block');
			break;
		case this.motionManager.KEY_UP:
			$("#player img#player_up").css('display', 'block');
			break;
		case 0:
		case this.motionManager.KEY_DOWN:
		default:
			$("#player img#player_down").css('display', 'block');
			break;
	}
	
	//Check if the player is not underground
	$("#player").css('visibility', (this.motionManager.isUnderground ? 'hidden' : 'visible') );
	
	//Show the player on the map
	this.map.showPlayer(this);
}

/**
 * Start the player movements.
 */
Player.prototype.start = function()
{
	//Setup the keyboard events
	this.setupKeyboardEvent();
	
	//Start the timer
	var player = this;
	this.timer = setInterval(function(){player.action()}, this.TIMER_INTERVAL);
}

/**
 * Setup the keyboard events which move the player.
 */
Player.prototype.setupKeyboardEvent = function()
{
	var player = this;
	var motionManager = this.motionManager;
	
	$(document).keydown(function(event)
	{
		if(event.keyCode == motionManager.KEY_LEFT || event.keyCode == motionManager.KEY_RIGHT ||
		   event.keyCode == motionManager.KEY_UP || event.keyCode == motionManager.KEY_DOWN)
		{
			motionManager.setWishedDirection(event.keyCode);
		}
		else if(event.keyCode == motionManager.KEY_SPACE)
		{
			player.openCurrentStation();
		}
	});
}

/**
 * Fonction called automatically after a certain interval.
 * Move and refresh the display.
 */
Player.prototype.action = function()
{
	//Move the player if necessary
	this.motionManager.move();
	
	//Refresh the view
	this.drawPlayerOnMap();
}

/**
 * Go to a station automatically.
 *
 * @param station from the map_data
 */
Player.prototype.goToStation = function(station)
{
	this.motionManager.setWishedPosition({"x":station.x, "y":station.y});
}

/**
 * Open the station dialog where the player is located.
 */
Player.prototype.openCurrentStation = function()
{
	//Search if we are on a station
	var stations = this.map.getStations();
	var gridPosition = this.getGridPosition();
	
	for(var i in stations)
	{
		if(stations[i].x == gridPosition.x && stations[i].y == gridPosition.y)
		{
			this.game.openDialog(stations[i].action, true);
			break;
		}
	}
}


