package {
	import flash.events.*;
	import flash.utils.getTimer;
	import mx.core.UIComponent;
	import org.papervision3d.core.math.Matrix3D;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.*;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.view.layer.ViewportLayer;
	import org.papervision3d.materials.*;

	public class Logo extends UIComponent {
		public var camera:Camera3D;
		public var scene:Scene3D;
		public var viewport:Viewport3D;
		public var renderer:BasicRenderEngine;

		public var turtle:DisplayObject3D;
		public var cursor:PaperPlane;
		public var material:ColorMaterial;
		public var color:uint = 0xFFCC88;
		public var drawAlpha:Number = 1;
		public var radius:Number = .5;
		public var segmentsW:Number = 1;
		public var segmentsH:Number = 1;

		public var draw:Boolean = true;

		override protected function createChildren():void {
			camera = new Camera3D;
			camera.z = -1000;
			camera.focus = 100;
			camera.zoom = 10;
			renderer = new BasicRenderEngine();
			viewport = new Viewport3D( 
				0, 0, // width, height
				true, // scale to stage
				false ); // interactive

			cs();

			addChild( viewport );
			addEventListener( Event.ENTER_FRAME, frame );
		}

		public function cs():void {
			scene = new Scene3D();
			var m:ColorMaterial = new ColorMaterial( 0x0088FF );
			m.doubleSided = true;
			cursor = new PaperPlane( m );
			cursor.scale = .1;
			cursor.alpha = .75;

			turtle = new DisplayObject3D;
			home();
			turtle.addChild( cursor );
			scene.addChild( turtle );
			

			scene.addChild( camera );
		}

		public function home():void {
			turtle.copyTransform( Matrix3D.IDENTITY );
			turtle.pitch( -90 );
		}
			
		public function frame( event:Event ):void {
			camera.x = (viewport.mouseX/viewport.width-0.5)*500;
			camera.y = -(viewport.mouseY/viewport.height-0.5)*500;
			camera.lookAt( DisplayObject3D.ZERO );
			renderer.renderScene( scene, camera, viewport );
		}

		public function fd( distance:Number ):void {
			if( distance != 0 ) {
				turtle.moveForward( distance/2 );
				if( draw ) {
					addCylinder( distance );
				}
				turtle.moveForward( distance/2 );
			}
		}

		public function setColor( r:Number, g:Number, b:Number, a:Number ):void {
			color = 
			r * 255 * 256 * 256 +
			g * 255 * 256 +
			b * 255;
			drawAlpha = a;
		}

		public function repeat( times:Number, func:Function ):void {
			while( times-- ) {
				func();
			}
		}

		public function addCylinder( length:Number ):void {
			var m:ColorMaterial = new ColorMaterial( color, drawAlpha );
			//var m:WireframeMaterial = new WireframeMaterial( color, 0.25 );
			m.doubleSided = true;
			var cyl:Cylinder = new Cylinder(
				m,
				radius,
				length,
				segmentsW,
				segmentsH,
				radius, // top radius
				false, // top face
				false // bottom face
			);
			scene.addChild( cyl );
			cyl.copyTransform( turtle );
			cyl.pitch(-90);
		}
	}
}