package classes.matuto.utils.display
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class DisplayObjectUtils
	{
		
		
		//########################################
		//HIT TESTE
		//########################################

		
		public static function hitTest(object:DisplayObject, point:Point):Boolean
		{
			
			if(object is BitmapData)
			{
				return (object as BitmapData).hitTest(new Point(0,0), 0, object.globalToLocal(point));
			}
			else
			{
				
				if(!object.hitTestPoint(point.x, point.y, true))
				{
					return false;
				}
				else
				{
					
					var bmapData:BitmapData = new BitmapData(object.width, object.height, true, 0x00000000);
					bmapData.draw(object, new Matrix());
					var returnVal:Boolean = bmapData.hitTest(new Point(0,0), 0, object.globalToLocal(point));
					
					bmapData.dispose();
					
					return returnVal;
				}
			}
		}
		
		//########################################
		//########################################

		
		
		//########################################
		//RESIZE PROP
		//########################################
		
		public static function resizeProp(align:String, obj:DisplayObject):void
		{
			
			if (obj.stage != null)
			{
				
				var sH:Number = obj.stage.stageHeight;
				var sW:Number = obj.stage.stageWidth;
				
				switch(align)
				{
					
					case "T":   resizeTop(obj, sW, sH); break;
					case "TL":  resizeTopLeft(obj, sW, sH); break;
					case "TR":  resizeTopRight(obj, sW, sH); break;
					
					case "C":   resizeCenter(obj, sW, sH); break;
					case "CL":  resizeCenterLeft(obj, sW, sH); break;
					case "CR":  resizeCenterRight(obj, sW, sH); break;
					
					case "B":   resizeBottom(obj, sW, sH); break;
					case "BL":  resizeBottomLeft(obj, sW, sH); break;  
					case "BR":  resizeBottomRight(obj, sW, sH); break;
				}
				
			} else throw new Error("the object is not on the stage");
			
		}
		
		static public function resizeBottomRight(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}
			
			obj.x                       = int((obj.stage.stageWidth - obj.width));
			obj.y                       = int((obj.stage.stageHeight - obj.height));
			
		}
		
		static public function resizeBottom(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}
			
			obj.x                       = int((obj.stage.stageWidth - obj.width)/2);
			obj.y                       = int((obj.stage.stageHeight - obj.height));
			
		}
		
		static public function resizeBottomLeft(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}
			
			obj.x                       = 0;
			obj.y                       = int((obj.stage.stageHeight - obj.height));
			
		}
		
		static public function resizeCenterRight(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}
			
			obj.x                       = int((obj.stage.stageWidth - obj.width));
			obj.y                       = int((obj.stage.stageHeight - obj.height) / 2);
			
		}
		
		static public function resizeCenter(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}
			
			obj.x                       = int((obj.stage.stageWidth - obj.width) / 2);
			obj.y                       = int((obj.stage.stageHeight - obj.height) / 2);
			
		}
		
		static public function resizeCenterLeft(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}
			
			obj.x                       = 0;
			obj.y                       = int((obj.stage.stageHeight - obj.height) / 2);
			
		}
		
		static public function resizeTopRight(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}
			
			obj.x                       = int((obj.stage.stageWidth - obj.width));
			obj.y                       = 0;
			
		}
		
		static public function resizeTop(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}  
			
			obj.x                       = int((obj.stage.stageWidth - obj.width) / 2);
			obj.y                       = 0;
			
		}
		
		static public function resizeTopLeft(obj:DisplayObject, sW:Number, sH:Number):void
		{
			
			obj.width                   = sW;
			obj.scaleY                  = obj.scaleX;
			
			if (obj.height < sH)
			{
				
				obj.height              = sH;
				obj.scaleX              = obj.scaleY;
				
			}
			
		}
		
		
		public static function resizeProporcao(value:*,largura:Number,altura:Number):void
		{
			
			var loc2		:*=NaN;
			var loc3		:*=null;
			
			loc3			  = value;
			
			if (altura / largura > loc3.height / loc3.width)
			{
				
				loc2 			= loc3.width / loc3.height;
				loc3.height 	= altura;
				loc3.width 		= altura * loc2;
				loc3.y 			= altura/ 2 - loc3.height / 2;
				loc3.x 			= largura / 2 - loc3.width / 2;
				
			}
			else 
			{
				loc2 			= loc3.height / loc3.width;
				loc3.width 		= largura;
				loc3.height 	= largura * loc2;
				loc3.y 			= altura / 2 - loc3.height / 2;
				loc3.x 			= largura / 2 - loc3.width / 2;
			}
			
			
			
		}
		
		//########################################
		//########################################
		

		
		//########################################
		//DRAW
		//########################################
		
		
		public static function getRecShape(_color:uint,_width:Number,_height:Number,_x:Number=0,_y:Number=0):Sprite
		{
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill(_color);
			sprite.graphics.drawRect(_x,_y,_width,_height);
			sprite.graphics.endFill();
			return sprite;
		}
		
		
		public static function paintDisplay(value:DisplayObjectContainer,color:uint=0x00FF00):void
		{
			var rec:Sprite = getRecShape(color, value.width, value.height,value.x,value.y);
			value.addChild(rec);
			value.setChildIndex(rec, value.numChildren - 1);
		}
		
		/*public static function addColor(value:DisplayObjectContainer,color:uint=0x00FF00):void
		{
			var colorV:Color = new Color();
			colorV.setTint(color, 1);
			value.transform.colorTransform = colorV;
		}
		*/
		
		public static function getDrawMoveTo45(color:uint,_width:Number,_height:Number):Sprite
		{
			var s:Sprite = new Sprite();
			s.graphics.beginFill(0x000000);
			s.graphics.lineTo(_width,0);
			s.graphics.lineTo(_width,_height);
			s.graphics.endFill();
			
			return s;
		}
		
		public static function getDrawMoveTo45Netative(color:uint,_width:Number,_height:Number):Sprite
		{
			var s:Sprite = new Sprite();
			s.graphics.beginFill(0x000000);
			s.graphics.lineTo(0,_height);
			s.graphics.lineTo(_width,_height);
			s.graphics.endFill();
			
			return s;
		}
		
		
		public static function PhotoDisplay(movie:DisplayObject):Bitmap
		{
			var bitmapData	:BitmapData = new BitmapData(movie.width, movie.height,true,16777215);
			var bitMap		:Bitmap		= new Bitmap(bitmapData);
			bitmapData.draw(movie);
			
			return bitMap;
		}
		
		
		public static function PhotoDisplayCopy(movie:*,rec:Rectangle):Bitmap
		{
			var bmpMC:BitmapData;
			bmpMC = new BitmapData(movie.width,movie.height,true,16777215);
			bmpMC.draw(movie);
			
			var bitmapData	:BitmapData = new BitmapData(rec.width,rec.height,true,16777215);
			bitmapData.copyPixels(bmpMC,rec, new Point(0, 0));
			var bitMap		:Bitmap		= new Bitmap(bitmapData);
			
			return bitMap;
		}
	
		//########################################
		//########################################

		
		
	
		
		
	}//end class
}