package main{

	import flash.display.MovieClip;
	import flash.events.*;

	//classes for PaperVision
	import PaperBase;
	import org.papervision3d.objects.primitives.Cone;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.Collada;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.WireframeMaterial;
	//import org.papervision3d.lights.PointLight3D;
	//import org.papervision3d.materials.shadematerials.FlatShadeMaterial;


	//classes for AIR
	import flash.desktop.NativeDragManager;
	import flash.events.NativeDragEvent;
	import flash.desktop.ClipboardFormats;

	public class Main extends PaperBase {

		public var model = "";//http://papervision2.com/wp-content/downloads/dae/cow.dae
		public var dynMaterial = "";//http://papervision2.com/wp-content/downloads/ourtex.jpg
		public var Obj3d:DisplayObject3D;
		public var distance:Number = 1000;
		private var mat:WireframeMaterial = new WireframeMaterial(0xfdbc15);
		//private var mat2:FlatShadeMaterial = new FlatShadeMaterial(new PointLight3D(), 0xFFFFFF, 0xFF0000);


		public function Main() {

			input_mc.load_btn.label = "Load";
			input_mc.load_btn.addEventListener(MouseEvent.MOUSE_UP, loadFiles);
			input_mc.daeModel_txt.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragEnterDae);
			input_mc.daeModel_txt.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDropDae);
			//input_mc.daeTexture_txt.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragEnterJpg);
			//input_mc.daeTexture_txt.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDropJpg);

			modifier_mc.visible = false;
			modifier_mc.back_btn.addEventListener(MouseEvent.MOUSE_UP, backToFiles);
			modifier_mc.scaleNumber_ns.minimum = 1;
			modifier_mc.scaleNumber_ns.maximum = 10;
			modifier_mc.scaleNumber_ns.value = 0;

			modifier_mc.xPosition_ns.minimum = -20;
			modifier_mc.xPosition_ns.maximum = 20;
			modifier_mc.xPosition_ns.value = 0;

			modifier_mc.yPosition_ns.minimum = -5;
			modifier_mc.yPosition_ns.maximum = 5;
			modifier_mc.yPosition_ns.value = 0;

			syphonSmall_mc.visible = false;

		}
		public function onDragEnterDae(e:NativeDragEvent):void {
			var faDae:Object = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT);
			if (faDae[0].extension == "dae") {
				NativeDragManager.acceptDragDrop(input_mc.daeModel_txt);
			}
		}
		public function onDragDropDae(e:NativeDragEvent):void {
			var faDae:Object = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT);
			input_mc.daeModel_txt.text = faDae[0].url;
		}
		public function onDragEnterJpg(e:NativeDragEvent):void {
			var faJpg:Object = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT);
			if (faJpg[0].extension == "jpg") {
				NativeDragManager.acceptDragDrop(input_mc.daeTexture_txt);
			}
		}
		public function onDragDropJpg(e:NativeDragEvent):void {
			var faJpg:Object = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT);
			//input_mc.daeTexture_txt.text = faJpg[0].url;
		}
		public function loadFiles(e:Event):void {
			model = input_mc.daeModel_txt.text;//loads the model equaled to the input field
			//dynMaterial = mat2//loads the texture equaled to the input field
			input_mc.visible = false;// hides input box
			syphon_mc.visible = false;
			syphonSmall_mc.visible = true;
			modifier_mc.visible = true;// shows modifier box
			modifier_mc.back_btn.label = "Back";

			init();// runs the init function within PaperBase that loads the model
		}
		public function backToFiles(e:Event):void {
			input_mc.visible = true;// hides input box
			syphon_mc.visible = true;
			syphonSmall_mc.visible = false;
			modifier_mc.visible = false;// shows modifier box
			default_scene.removeChild(Obj3d);
			modifier_mc.scaleNumber_ns.value = 0;
			modifier_mc.xPosition_ns.value = 0;
			modifier_mc.yPosition_ns.value = 0;

		}
		override protected function init3d():void {
			Obj3d = new Collada(model,new MaterialsList( { all: mat }));//puts the model inside of a Collada object
			//Obj3d.moveDown(100);
			//Obj3d.x = 400;//position in x.
			//Obj3d.y = -200;//position in y.
			//Obj3d.pitch(-30);
			default_scene.addChild(Obj3d);// adds it to the scene

		}
		override protected function processFrame():void {
			//Obj3d.yaw(5);
			Obj3d.scale = modifier_mc.scaleNumber_ns.value;// scales model to the number in the stepper
			Obj3d.x = modifier_mc.xPosition_ns.value * 100;
			Obj3d.y = modifier_mc.yPosition_ns.value * 100;


			// The next line is a bit of a mouthful, but it's quite simple really.
			// It just gets the Y position of your mouse, and sets the camera.y to a
			// value between -800 and 800, depending on the Y position of your mouse
			// (If mouseY = 0 then camera.y = 800. If mouseY = stage.height, camera.y = -800)
			default_camera.y = -(((mouseY - (stage.height / 2)) / stage.height) * 1600);


			// We don't want the camera to move further away from the model, so we do this:
			default_camera.moveForward(default_camera.distanceTo(Obj3d) - distance);

			// We now want to rotate the object depending on
			// the Xmouse position. This is really simple.
			Obj3d.rotationY = ((mouseX / stage.width) * 360);//Rotation

			// NOTE: This will only work if you only have one object in your scene.
			// I'll write a more advanced movement tutorial soon
		}
	}//closes class
}//closes package