package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Input;
	import net.flashpunk.Sfx;
	
	/**
	 * ...
	 * @author Benn
	 */
	public class Block extends Entity 
	{
		
		[Embed(source = '../assets/graphics/block.png')] private static const BLOCK_GRAPHIC:Class;
		[Embed(source ='../assets/graphics/blocklarge.png')] private static const BLOCK_GRAPHIC_LARGE:Class;
		[Embed(source = '../assets/graphics/blockwide.png')] private static const BLOCK_GRAPHIC_WIDE:Class;
		[Embed(source='../assets/graphics/blocktall.png')] private static const BLOCK_GRAPHIC_TALL:Class;
		
		[Embed(source = '../assets/sound/thud.mp3')] private static const THUD:Class;
		
		private var thud:Sfx = new Sfx(THUD);
		
		private var PlayerImage:Image;
		
		private var Speed:Number = 0;
		
		private var IsBeingHeld:Boolean = false;
		
		private var HeldPositionX:int = 0;
		
		private var HeldPositionY:int = 0;
		
		public var CanBeDropped:Boolean = true;
		
		private var HasCollidedYet:Boolean = true;
		
		public function SetBeingHeld(value:Boolean):void
		{
			
			trace("held");
			
			IsBeingHeld = value;
			
			if (value == true)
			{
				type = "notblock";
				PlayerImage.alpha = 0.5;
				HasCollidedYet = false;
			}
			else
			{
				type = "block";
				PlayerImage.alpha = 1.0;
			}
			
		}
		
		public function SetHeldLocation(valuex:int, valuey:int):void
		{
			HeldPositionX = valuex - x;
			HeldPositionY = valuey - y;
		}
		
		public function Block(Type:int) 
		{
			
			if (Type == 0)
			{
				PlayerImage = new Image(BLOCK_GRAPHIC);
			}
			else if (Type == 1)
			{
				PlayerImage = new Image(BLOCK_GRAPHIC_LARGE);
			}
			else if (Type == 2)
			{
				PlayerImage = new Image(BLOCK_GRAPHIC_WIDE);
			}
			else
			{
				PlayerImage = new Image(BLOCK_GRAPHIC_TALL);
			}
			
			graphic = PlayerImage;
			setHitboxTo(PlayerImage);
			
			type = "block";
			
		}
		
		override public function update():void 
		{
			
			if (IsBeingHeld)
			{
				
				var tempx:int = x;
				var tempy:int = y;
				
				var newx:int = Input.mouseX - HeldPositionX;
				
				if (newx > x)
				{
					if (newx - x > 64)
					{
						SetBeingHeld(false);
						return;
					}	
				}
				else 
				{
					if (x - newx > 64)
					{
						SetBeingHeld(false);
						return;
					}
				}
				
				x = newx;
				
				if (collide("level", x, y) || collide("blockstopper", x, y))
				{
										
					if (x > tempx)
					{
						x = Math.floor(x / 32) * 32;
					}
					else
					{
						x = Math.floor(x / 32) * 32 + 32;
					}
					
					if (collide("level", x, y) || collide("blockstopper", x, y))
					{
						
						if (x > tempx)
						{
							if (x - tempx > 15)
							{
								if (!CanBeDropped)
								{
									x = tempx;
									PlayerImage.color = 0xFFFFFF;
								}
				
								SetBeingHeld(false);
								
							}
						}
						else if (x < tempx)
						{
							if (tempx - x > 15)
							{
								
								if (!CanBeDropped)
								{
									x = tempx;
									PlayerImage.color = 0xFFFFFF;
								}
				
								SetBeingHeld(false);
								
							}
						}
						
						x = tempx;
					}
					
				}
				else if (collide("block", x, y) || collide("player", x, y))
				{
					CanBeDropped = false;
					PlayerImage.color = 0xFF0000;
				}
				else
				{
					CanBeDropped = true;
					PlayerImage.color = 0xFFFFFF;
				}
				
				var newy:int = Input.mouseY - HeldPositionY;
				
				if (newy > y)
				{
					if (newy - y > 64)
					{
						SetBeingHeld(false);
						return;
					}
				}
				else 
				{
					if (y - newy > 64)
					{
						SetBeingHeld(false);
						return;
					}
				}
				
				y = newy;
				
				if (collide("level", x, y) || collide("blockstopper", x, y))
				{
					
					if (y > tempy)
					{
						y = Math.floor(y / 32) * 32;
					}
					else
					{
						y = Math.floor(y / 32) * 32 + 32;
					}
					
					if (collide("level", x, y) || collide("blockstopper", x, y))
					{
						
						if (y > tempy)
						{
							if (y - tempy > 15)
							{
								if (!CanBeDropped)
								{
									y = tempy;
									PlayerImage.color = 0xFFFFFF;
								}
				
								SetBeingHeld(false);
								
							}
						}
						else if (y < tempy)
						{
							if (tempy - y > 15)
							{
								
								if (!CanBeDropped)
								{
									y = tempy;
									PlayerImage.color = 0xFFFFFF;
								}
				
								SetBeingHeld(false);
							}
						}
						
						y = tempy;
					}
					
				}
				else if (collide("block", x, y) || collide("player", x, y) )
				{
					CanBeDropped = false;
					PlayerImage.color = 0xFF0000;
				}
				else
				{
					CanBeDropped = true;
					PlayerImage.color = 0xFFFFFF;
				}
				
			}
			else
			{
				
				y += Speed;
				
				if (collide("level", x, y) || collide("player", x, y) || collide("block", x, y)|| collide("blockstopper", x, y))
				{
					
					if (!HasCollidedYet)
					{
						thud.play();
						HasCollidedYet = true;
					}
					
					y = Math.floor(y / 32) * 32;
					Speed = 0;
				}
				else
				{
					Speed += 50 * FP.elapsed;
				}
				
			}
			
			super.update();
		}
		
	}

}