//Copyright (C) Ben Parbury - All Rights Reserved. Unauthorized copying of this file, via any medium is strictly prohibited. Written by Ben Parbury <ben@gridlockinteractive.com> 2014
var gPlayerSystem = new PlayerSystem();

function MouseHistory(x, y)
{
	this.__proto__ = new Vec2(x, y);
	
	this.GetPrevMouseHistory = function()
	{
		var pos = (gPlayerSystem.mouseHistoryPosition - 1) % gPlayerSystem.kMouseHistoryLength;
		return gPlayerSystem.mouseHistory[pos];
	}
	
	this.GetAngle = function()
	{
		var prev = this.GetPrevMouseHistory();
		if(prev)
		{
			return Math.atan2(this.y-prev.y,this.x-prev.x);
		}
		return null;
	}
	
	this.GetKeyCode = function()
	{
		var angle = this.GetAngle();
		if(angle != null)
		{
			if(angle > -Math.PI * 0.75 && angle <= -Math.PI * 0.25)
			{
				return EKeyCode.up;
			}
			else if(angle > -Math.PI * 0.25 && angle <= Math.PI * 0.25)
			{
				return EKeyCode.right;
			}
			else if(angle > Math.PI * 0.25 && angle < Math.PI * 0.75)
			{
				return EKeyCode.down;
			}
			else
			{
				return EKeyCode.left;
			}
		}
		
		return null;
	}
	
	this.time = gStatsSystem.GetCurrentTimeMs();
	this.keyCode = this.GetKeyCode();
}

function PlayerSystem()
{
	this.players = new Array();
	
	this.ghost = undefined;
	this.ghostPos = new Vec2();
	
	this.kMouseHistoryLength = 5;
	this.mouseHistoryPosition = 0;
	this.mouseHistory = new Array(this.kMouseHistoryLength)
	this.mouseKeyDown = null;
	
	this.Init = function()
	{
	}

	this.LoadPlayers = function(playerArray)
	{
		this.players = new Array();

		gConsole.Log(3, "[LVL] Loading upto " + playerArray.length + " Players");
		
		var count = playerArray.length;
		for(var i = 0; i < count; i++)
		{
			var player = new Player();
			player.Load(playerArray[i]);
			this.players.push(player);

			if(i == 0)
			{
				this.ghostPos.x = player.GetX();
				this.ghostPos.y = player.GetY();
			}
			
		}
	}
	
	this.AddGhost = function()
	{
		if(!this.ghost)
		{
			var ghost = new Player(true);
			ghost.LoadToPosition(this.ghostPos.x, this.ghostPos.y);
			this.ghost = ghost;
		}
	}
	
	this.RemoveGhost = function()
	{
		if(this.ghost)
		{
			this.ghost = undefined;
		}
	}
	
	this.GetPlayerAtIndex = function(index)
	{
		if(index >= 0 && index < this.players.length)
		{
			return this.players[index];
		}

		return null;
	}
	
	this.GetPlayerAt = function(x, y)
	{
		for(var i = 0; i < this.players.length; i++)
		{
			var player = this.players[i];
			if(player.IsPlayerAt(x, y))
			{
				return player;
			}
		}
		
		return null;
	}
	
	this.UpdateMouseControls = function()
	{
		if(gInteraction.mouseDown)
		{
			var keyCode = this.GetPlayerMovementFromMouseHistory();
			if(keyCode != null)
			{
				this.MouseKeyDown(keyCode);
			}
			else
			{
				this.MouseKeyUp();
			}
		}
		else
		{
			this.MouseKeyUp();
		}
	}
	
	this.MouseKeyDown = function(key)
	{
		if(this.mouseKeyDown != key)
		{
			gKeyboard.OnMappedKeyDown(key);
			this.mouseKeyDown = key;
		}
	}
	
	this.MouseKeyUp = function()
	{
		if(this.mouseKeyDown)
		{
			gKeyboard.OnMappedKeyUp(this.mouseKeyDown);
			this.mouseKeyDown = null;
		}
	}
	
	this.Update = function()
	{
		this.UpdateMouseControls();
		
		for(var i = 0; i < this.players.length; i++)
		{
			this.players[i].Update();
		}
		
		if(this.ghost)
		{
			this.ghost.Update();
		}
	}
	
	this.MouseDown = function(x, y)
	{
		if(!gEditor)
		{
			//handles mouse controls for player
		}
	}

	this.MouseUp = function(x, y)
	{
		if(!gEditor)
		{
			//handles mouse controls for player
		}
	}
	
	this.InteractMove = function(x, y)
	{
		if(!gEditor && gMobile)
		{
			//determine key presses
			this.mouseHistory[this.mouseHistoryPosition % this.kMouseHistoryLength] = new MouseHistory(x, y);
			this.mouseHistoryPosition++;
		}
	}
	
	this.GetPlayerMovementFromMouseHistory = function()
	{
		var now = gStatsSystem.GetCurrentTimeMs();
		
		var count = 0;
		var keyCode = null;
		var start = this.GetMouseHistoryStartPosition();
		for(var i = start; i < this.mouseHistoryPosition; i++)
		{
			var hist = this.mouseHistory[i % this.kMouseHistoryLength];
			var timeSinceMs = now - hist.time;
			if(timeSinceMs < 500)
			{
				if(hist.keyCode != null && (keyCode == null || keyCode == hist.keyCode))
				{
					keyCode = hist.keyCode;
					count++;
				}
			}
		}
		if(count > 3)
		{
			return keyCode;
		}
		
		return null;
	}
	
	this.GetMouseHistoryStartPosition = function()
	{
		return Math.max(0, this.mouseHistoryPosition - this.kMouseHistoryLength);
	}
	
	this.Debug = function()
	{
		for(var i = 0; i < this.players.length; i++)
		{
			this.players[i].Debug();
		}
		
		if(this.mouseHistoryPosition > 0)
		{
			gDraw.debug.beginPath();
			
			var startedLine = false;
			var now = gStatsSystem.GetCurrentTimeMs();
			
			var start = this.GetMouseHistoryStartPosition();
			for(var i = start; i < this.mouseHistoryPosition; i++)
			{
				var hist = this.mouseHistory[i % this.kMouseHistoryLength];
				var timeSinceMs = now - hist.time;
				if(timeSinceMs < 100)
				{
					if(hist.keyCode != null)
					{
						if(!startedLine)
						{
							gDraw.debug.moveTo(hist.x, hist.y);
							startedLine = true;
						}
						else
						{
							gDraw.debug.lineTo(hist.x, hist.y);
						}
						
						gDraw.Watch("KeyCode " + hist.keyCode);
					}
				}
			}
			gDraw.debug.strokeStyle = "rgba(255, 255, 255, 0.2)";
			gDraw.debug.stroke();
		}
	}
	
	this.KeyDown = function(key)
	{
		for(var i = 0; i < this.players.length; i++)
		{
			this.players[i].KeyDown(key);
		}

		if(this.ghost)
		{
			this.ghost.KeyDown(key);
		}
	}

	this.KeyUp = function(key)
	{
		for(var i = 0; i < this.players.length; i++)
		{
			this.players[i].KeyUp(key);
		}
		
		if(this.ghost)
		{
			this.ghost.KeyUp(key);
		}
	}
	
	this.DrawClear = function()
	{
		for(var i = 0; i < this.players.length; i++)
		{
			this.players[i].DrawClear();
		}
	}
	
	this.Draw = function()
	{
		for(var i = 0; i < this.players.length; i++)
		{
			this.players[i].Draw();
		}
		
		if(this.ghost)
		{
			this.ghost.Draw();
		}
	}
	
	this.Resize = function()
	{
		for(var i = 0; i < this.players.length; i++)
		{
			this.players[i].Resize();
		}
	}
}