package com.squidder.flar {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Camera;
	import flash.media.Video;
	
	import org.libspark.flartoolkit.core.FLARCode;
	import org.libspark.flartoolkit.core.param.FLARParam;
	import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
	
	import com.squidder.base.baseLoader.BaseLoader;
	import com.squidder.flar.detector.FLARSquidderMarkerDetector;
	import com.squidder.flar.events.FLARDetectorEvent;		

	/**
	 * @author Jon Reiling
	 */
	public class FLARBaseApplication extends Sprite {

		protected var _flarParam : FLARParam;
		protected var _flarCodes : Array;
		protected var _flarRaster : FLARRgbRaster_BitmapData;
		protected var _flarDetector : FLARSquidderMarkerDetector;
		protected var _webcam : Camera;
		protected var _video : Video;
		protected var _flarBitmap : Bitmap;
		protected var _markers : Array;
		protected var _sizes : Array;
		protected var _baseLoader : BaseLoader;
		
		public function FLARBaseApplication() {
			
			super( );
						
			_loadMarkersAndCamera( );
		}

		protected function _loadMarkersAndCamera() : void {
			
			_baseLoader = new BaseLoader( );
			_baseLoader.addAsset( "assets/flar/camera_para.dat" , { id:"FLARCamera" , type:"Binary" } );

			for ( var i : int = 0 ; i < _markers.length ; i++ ) {
				
				var markerObj : FLARMarkerObj = FLARMarkerObj( _markers[ i ] );
				_baseLoader.addAsset( markerObj.markerSrc , { id:"FLARPattern" + i } );
			}
						
			_baseLoader.addEventListener( Event.COMPLETE , _init );			
			_baseLoader.load();
		}

		protected function _init( event : Event ) : void {

			_flarParam = new FLARParam( );
			_flarParam.loadARParam( _baseLoader.getAsset( "FLARCamera" ) );
			_flarParam.changeScreenSize( 320 , 240 );
			
			_setUpMarkers( );
			_attachWebCam( );
			
			_flarBitmap = new Bitmap( new BitmapData( 320 , 240 , false , 0 ) , PixelSnapping.AUTO , true );
			_flarBitmap.scaleX = _flarBitmap.scaleY = 2;
			addChild( _flarBitmap );
			
			_flarRaster = new FLARRgbRaster_BitmapData( _flarBitmap.bitmapData );
			_flarDetector = new FLARSquidderMarkerDetector( _flarParam , _flarCodes , _sizes , _sizes.length );
			_flarDetector.addEventListener( FLARDetectorEvent.MARKER_ADDED , _handleMarkerAdded );
			_flarDetector.addEventListener( FLARDetectorEvent.MARKER_REMOVED , _handleMarkerRemove );
		}

		protected function _handleMarkerAdded( event : FLARDetectorEvent ) : void {
		}

		protected function _handleMarkerRemove( event : FLARDetectorEvent ) : void {
		}


		protected function _setUpMarkers() : void {
			
			_sizes = new Array( );
			_flarCodes = new Array( );
			
			for ( var i : int = 0 ; i < _markers.length ; i++ ) {
				
				var markerObj : FLARMarkerObj = FLARMarkerObj( _markers[ i ] );
				
				var code : FLARCode = new FLARCode( markerObj.markerSegements , markerObj.markerSegements , markerObj.markerSize , markerObj.markerSize );
				code.loadARPatt( _baseLoader.getAsset( "FLARPattern" + i ) );
				_flarCodes.push( code );			
				_sizes.push( markerObj.markerWidth );
			}
		}

		
		protected function _attachWebCam() : void {

			var index : int = 0;
			
			//auto switch to built in camera on a mac.
			for ( var i : int = 0 ; i < Camera.names.length ; i++ ) {
				
				if ( Camera.names[ i ] == "USB Video Class Video" ) {
					index = i;
				}
			}
			_webcam = Camera.getCamera( String( index ) );
			_webcam.setMode( 320 , 240 , 30 );
			_video = new Video( 320 , 240 );
			_video.attachCamera( _webcam );			
		}
	}
}
