//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 gTileSystem = new TileSystem();

var gTileWidth = 16;
var gTileHeight = 12;

function TileSystem()
{
	this.redraw = true;
	this.tiles = new Array();
	this.overlayTiles = new Array();
	this.transitionDistance = 0;
	this.activeHintTilePos = new Vec2(-1, -1);
	
	this.Init = function()
	{
		gTileContext = gDraw.tiles;
		gTileBGContext = gDraw.tilebg;
	}

	this.AddTile = function(type, subType, x, y)
	{
		this.AddTileWithParam(type, subType, x, y, true, true);
	}
	
	this.AddTileWithParam = function(type, subType, x, y, blockMove, playerMove, hintText)
	{
		var tile = this.CreateTile(type, subType, x, y, blockMove, playerMove, hintText)
		this.InsertTile(tile);
	}
	
	this.CreateTile = function(type, subType, x, y, blockMove, playerMove, hintText)
	{
		switch(type)
		{
			case ETileType.Normal:
				var tile = new Tile();
				break;

			case ETileType.Conveyor:
				var tile = new ConveyorTile();
				break;
			
			case ETileType.Slider:
				var tile = new SliderTile();
				break;
				
			case ETileType.Hint:
				var tile = new HintTile();
				break;
			
			default:
				gConsole.AssertNow("Unknown tile type");
				break;
		}
		
		tile.Init(type, x, y, blockMove, playerMove);
		if(subType && tile.SetSubType)
		{
			tile.SetSubType(subType);
		}
		if(hintText && tile.SetHintText)
		{
			tile.SetHintText(hintText);
		}
		
		return tile;
	}
	
	this.InsertTile = function(tile)
	{
		this.tiles.push(tile);
		if(tile.DrawOverlay)
		{
			this.overlayTiles.push(tile);
		}
		
		this.redraw = true;
	}
	
	this.RemoveTile = function(tile)
	{
		var index = this.tiles.indexOf(tile);
		if(index != -1)
		{
			this.tiles.splice(index, 1);
		}
		
		var index = this.overlayTiles.indexOf(tile);
		if(index != -1)
		{
			this.overlayTiles.splice(index, 1);
		}
		this.redraw = true;
	}
	
	this.Clear = function()
	{
		this.tiles = new Array();
		this.overlayTiles = new Array();
		gDraw.tilebg.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		gDraw.tiles.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		this.redraw = true;
		this.activeHintTilePos = new Vec2(-1, -1);
	}
	
	this.GetTileAt = function(x, y)
	{
		for(var i = 0; i < this.tiles.length; i++)
		{
			var tile = this.tiles[i];
			if(tile.x == x && tile.y == y)
			{
				return tile;
			}
		}
		
		return null;
	}
	
	this.GetAdjacent = function(outArray, x, y)
	{
		for(var i = 0; i < this.tiles.length; i++)
		{
			var tile = this.tiles[i];
			var dist = Math.abs(tile.x - x) + Math.abs(tile.y - y);
			if(dist <= 3)
			{
				outArray.push(tile);
			}
		}
	}
	
	this.Redraw = function()
	{
		return this.redraw;
	}
	
	this.StartTransition = function()
	{
		var maxDistance = 0;
		for(var i = 0; i < this.tiles.length; i++)
		{
			var tile = this.tiles[i];
			var dist = distance(gLevelSystem.transitionX, gLevelSystem.transitionY, tile.x, tile.y);
			maxDistance = Math.max(maxDistance, dist);
		}

		this.transitionDistance = maxDistance;
	}
	
	this.DrawTransition = function(fraction)
	{
		gDraw.tilebg.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		
		for(var i = 0; i < this.tiles.length; i++)
		{
			var tile = this.tiles[i];
			var dist = distance(gLevelSystem.transitionX, gLevelSystem.transitionY, tile.x, tile.y);
			if(dist < this.transitionDistance * fraction)
			{
				this.tiles[i].DrawBackground();
			}
		}
		
		gDraw.tiles.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);

		for(var i = 0; i < this.tiles.length; i++)
		{
			var tile = this.tiles[i];
			var dist = distance(gLevelSystem.transitionX, gLevelSystem.transitionY, tile.x, tile.y);
			if(dist < this.transitionDistance * fraction)
			{
				this.tiles[i].Draw();
			}
		}
		
		for(var i = 0; i < this.overlayTiles.length; i++)
		{
			var tile = this.overlayTiles[i];
			var dist = distance(gLevelSystem.transitionX, gLevelSystem.transitionY, tile.x, tile.y);
			if(dist < this.transitionDistance * fraction)
			{
				tile.DrawOverlay();
			}
		}
	}
	
	this.DrawBackground = function()
	{
		gDraw.tilebg.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		
		for(var i = 0; i < this.tiles.length; i++)
		{
			this.tiles[i].DrawBackground();
		}
	}
	
	this.Draw = function()
	{
		gConsole.Assert(this.Redraw());

		this.DrawBackground();
		
		gDraw.tiles.clearRect(0, 0, gDraw.canvas.width, gDraw.canvas.height);
		
		for(var i = 0; i < this.tiles.length; i++)
		{
			this.tiles[i].Draw();
		}

		for(var i = 0; i < this.overlayTiles.length; i++)
		{
			var tile = this.overlayTiles[i];
			tile.DrawOverlay();
		}
		
		this.redraw = false;
	}
}