package classes.gustavo.midia.camera.detector
{
	
	import classes.com.jp.maaash.ObjectDetection.*;
	import classes.com.jp.quasimondo.bitmapdata.*;
	import classes.matuto.utils.display.DisplayObjectUtils;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.media.Camera;
	import flash.text.TextField;
	
	//[SWF( width='500',height='375',backgroundColor='#333333',frameRate='120' )]
	
	public class Detector extends Sprite
	{
		
		private var detector			:ObjectDetector;
		private var options				:ObjectDetectorOptions;
		
		private var view				:Sprite;
		private var faceRectContainer	:Sprite;
		
		private var camera				:CameraBitmap;
		private var detectionMap		:BitmapData;
		private var drawMatrix			:Matrix;
		private var scaleFactor			:int = 6;
		private var w					:int;
		private var h					:int;
		
		private var lastTimer			:int = 0;
				
		
		private var masksArray			:Array = new Array();
		private var masksArrayAux		:Array = new Array();
		
		private var dados				:Object = new Object();
		
		
		public var podeRender			:Boolean = false;
		public var POSSO_RENDER_THIS	:Boolean = true;
		
		public function Detector(_w:int=800,_h:int=600)
		{
			w = _w;
			h = _h;
			
			view 		= new Sprite;
			
			addChild( view );
			init();
		}
		
		private function init():void
		{
			initDetector();
			initCamera();
		}
		
		private function initCamera():void
		{
			if ( Camera.getCamera())
			{
				camera 					= new CameraBitmap(w,h, 30, w, h );
				camera.addEventListener( Event.RENDER, cameraReadyHandler );
				
				var bitmap:Bitmap 		= new Bitmap( camera.bitmapData );
				bitmap.scaleX     		= 1;
				bitmap.x          		= 0;
				view.addChild(bitmap);
				
				detectionMap 			= new BitmapData( w / scaleFactor, h / scaleFactor, false, 0 );
				drawMatrix 				= new Matrix( 1 / scaleFactor, 0, 0, 1 / scaleFactor );
				
				faceRectContainer 		= new Sprite;
				view.addChild(faceRectContainer);
			}		
			
		
		}
		
		private function cameraReadyHandler( event:Event ):void
		{
			detectionMap.draw( camera.bitmapData, drawMatrix, null, "normal", null, true );
			detector.detect( detectionMap );
			var rect:Rectangle 			= camera.bitmapData.getColorBoundsRect( 0xffffff, 0x000000, true );
		}
		
		private function initDetector():void
		{
			detector = new ObjectDetector();
			
			var options:ObjectDetectorOptions 		= new ObjectDetectorOptions();
			options.min_size				  		= 50;
			detector.options 				  		= options;
			detector.addEventListener( ObjectDetectorEvent.DETECTION_COMPLETE, detectionHandler );
		}
		
		private function detectionHandler( e:ObjectDetectorEvent ):void
		{
			RENDER_PHOTO(e);	
		}
		
		//#################################################################################//
		//EVENTS
		//#################################################################################//

		public function addEvents():void
		{
			detector.addEventListener( ObjectDetectorEvent.DETECTION_COMPLETE, detectionHandler );
		}
		
		public function removeEvents():void
		{
			detector.removeEventListener( ObjectDetectorEvent.DETECTION_COMPLETE, detectionHandler );
		}
		
		
		//#################################################################################//
		//#################################################################################//

		
		//#################################################################################//
	 	// DETECTOR FOTO
		//#################################################################################//
				
		private function RENDER_PHOTO(e:ObjectDetectorEvent):void
		{
			//var g:Graphics  = faceRectContainer.graphics;
			//g.clear();
			
			if(POSSO_RENDER_THIS)
			{
				for ( var i:int = masksArray.length - 1; i > -1; i-- )
				{
					masksArray[i].visible = false;
				}
							
				if ( e.rects )
				{
					//g.lineStyle( 2 ); // black 2pix
					e.rects.forEach( function( r:Rectangle, idx:int, arr:Array ):void
					{
					
						//trace(idx);
						if (!masksArray[ idx ])
						{
							masksArray[ idx ] = getRef;
							addChild( masksArray[ idx ]);
						}
						
						masksArrayAux = masksArray;
			
						//g.drawRect( r.x * scaleFactor, r.y * scaleFactor, r.width * scaleFactor, r.height * scaleFactor );
						
						masksArray[ idx ].visible 		= true;
						masksArray[ idx ].alpha			= 0;			
						masksArray[ idx ].width 		= r.width * scaleFactor;
						masksArray[ idx ].scaleY 		= masksArray[ idx ].scaleX;
						masksArray[ idx ].x 			= r.x * scaleFactor;
						masksArray[ idx ].y 			= (r.y - ( r.height / 3 )) * scaleFactor;
	
						dados.width 					= r.width * scaleFactor;
						dados.scaleY 					= masksArray[ idx ].scaleX;
						dados.x 						= (r.x * scaleFactor);
						dados.y 						= (r.y - ( r.height / 3 )) * scaleFactor;					
						
						dispatchEvent(new Event(Event.RENDER));
						
					});
	
				}
				
				if(masksArrayAux[0])
					podeRender = true
				else
					podeRender = false;
				
				
				dispatchEvent(new Event("validation"));
				
				masksArrayAux = [];
			
			}

				
		}
		
		public function get getDados():Object
		{
		   return dados;	
		}
		
		public function get getRef():Sprite
		{
		   return DisplayObjectUtils.getRecShape(0x66FF00,30,30);
		}
		
		
		public function getCamera():CameraBitmap
		{
			return camera;
		}
		
		
		//#################################################################################//
		//#################################################################################//

		
	}//end class
}
