package
{	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.display.IsoView;
	import as3isolib.geom.Pt;
	import eDpLib.events.ProxyEvent;
	import flash.filters.GlowFilter;
	import fl.transitions.Tween;
	import fl.transitions.easing.Elastic;
	import as3isolib.graphics.SolidColorFill;
	import as3isolib.display.primitive.IsoRectangle;
	import as3isolib.core.IsoDisplayObject;
	import as3isolib.geom.IsoMath;
	import as3isolib.display.IsoSprite;
	
	public class CDoc extends MovieClip
	{

		private var viewPort:IsoView;
		
		private var scene:IsoScene;
		
		private var grid:IsoGrid;
		
		private var objectScene:IsoScene;
		
		private var objectView:IsoView;
						
		// Instantiating The Tabs
		private var Solid:CTab;
		private var Bricks:CTab;
		private var Grass:CTab;

		public function CDoc()
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false, 0, true);
		}
		
		private function onAddedToStage(e:Event):void
		{
			viewPort = new IsoView();
			viewPort.setSize(800, 600);
			viewPort.centerOnPt(new Pt(-100, 100, 0), false);
			addChildAt(viewPort, 0);
			viewPort.addEventListener(MouseEvent.MOUSE_DOWN, onStartPan, false, 0, true);
			viewPort.addEventListener(MouseEvent.MOUSE_WHEEL, onZoom, false, 0, true);
			
			scene = new IsoScene();
			viewPort.addScene(scene);
			
			grid = new IsoGrid();
			grid.setGridSize(10, 10, 1);
			grid.cellSize = 30;
			scene.addChild(grid);
								  
			// Creating the tabs
			Solid = new CTab("SOLID");
			Solid.y = 469;
			Solid.addEventListener(MouseEvent.CLICK, onSolidClick, false, 0, true);
			addChild(Solid);
			
			Bricks = new CTab("BRICKS");
			Bricks.y = 494;
			Bricks.addEventListener(MouseEvent.CLICK, onBricksClick, false, 0, true);
			addChild(Bricks);
			
			Grass = new CTab("GRASS");
			Grass.y = 521;
			Grass.addEventListener(MouseEvent.CLICK, onGrassClick, false, 0, true);
			addChild(Grass);
			
			objectScene = new IsoScene();
			
			objectView = new IsoView();
			objectView.setSize(215, 468);
			objectView.centerOnPt(new Pt(40, 80, 0), false);
			objectView.addScene(objectScene);
			addChild(objectView);
			
			// Bottom panel buttons click events
			this.getChildByName("btnClear").addEventListener(MouseEvent.CLICK, onbtnClearClick, false, 0, true);
			this.getChildByName("btnFit").addEventListener(MouseEvent.CLICK, onbtnFitClick, false, 0, true);
			this.getChildByName("btnShow").addEventListener(MouseEvent.CLICK, onbtnShowClick, false, 0, true);

            addEventListener(Event.ENTER_FRAME, onRender, false, 0, true);
		}
		
		private function onRender(e:Event):void
        {
            scene.render();
        }
		
		private function onbtnClearClick(e:MouseEvent):void
		{
			scene.removeAllChildren();
			scene.addChild(grid);
		}
		
		private function onbtnFitClick(e:MouseEvent):void
		{
			viewPort.currentZoom = 1;
			viewPort.panTo(-100, 100);
		}
		
		private function onbtnShowClick(e:MouseEvent):void
		{
			if (grid.showOrigin)
				grid.showOrigin = false;
			else
				grid.showOrigin = true;
		}
		
		private var panPt:Pt;
		private function onStartPan(e:MouseEvent):void
        {
            panPt = new Pt(stage.mouseX, stage.mouseY);
 
            viewPort.removeEventListener(MouseEvent.MOUSE_DOWN, onStartPan);
 
            viewPort.addEventListener(MouseEvent.MOUSE_MOVE, onPan, false, 0, true);
            viewPort.addEventListener(MouseEvent.MOUSE_UP, onStopPan, false, 0, true);
        }
 
        private function onPan(e:MouseEvent):void
        {
            viewPort.panBy(panPt.x - stage.mouseX, panPt.y - stage.mouseY);

            panPt.x = stage.mouseX;
            panPt.y = stage.mouseY;
        }
 
        private function onStopPan(e:MouseEvent):void
        {
            viewPort.removeEventListener(MouseEvent.MOUSE_MOVE, onPan);
            viewPort.removeEventListener(MouseEvent.MOUSE_UP, onStopPan);
 
            viewPort.addEventListener(MouseEvent.MOUSE_DOWN, onStartPan, false, 0, true);
        }
		
		private var zoomValue:Number = 1;
		private function onZoom(e:MouseEvent):void
        {
			
			if(e.delta > 0)
				zoomValue +=  0.10;
				
			if(e.delta < 0)
				zoomValue -=  0.10;
			
			viewPort.currentZoom = zoomValue;
        }
		
		private function onSolidClick(e:MouseEvent):void
		{
			objectScene.removeAllChildren();
			var p:Pt = new Pt(0, 0);
			var solidColors:Array = [0xD15415, 0xFF6600, 0xFFCC00, 0x66FF00, 0xFF6699, 0x6699FF, 0x99FF00, 0xFF0066];
			for (var i:int = 0; i < 8; i++)
			{
				if (i % 2 == 0)
				{
					p.x = 0;
					p.y += 50;
				}
				var obj:IsoRectangle = new IsoRectangle(); // edit the name and type
				obj.setSize(30, 30, 0);
				obj.fill = new SolidColorFill(solidColors[i], 1);
				IsoMath.screenToIso(p);
				obj.moveTo(p.x, p.y, 0);
				IsoMath.isoToScreen(p);
				p.x += 80;
				obj.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
				obj.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
				obj.addEventListener(MouseEvent.CLICK, onObjClick, false, 0, true);
				
				objectScene.addChild(obj);
				objectScene.render();
			}
		}
		
		private function onBricksClick(e:MouseEvent):void
		{
			objectScene.removeAllChildren();
			var p:Pt = new Pt(-20, -10);
			var bricks:Array = [Bricks1, Bricks2, Bricks3, Bricks4, Bricks5, Bricks6, Bricks7, Bricks8];
			for (var i:int = 0; i < 8; i++)
			{
				if (i % 2 == 0)
				{
					p.x = -30;
					p.y += 50;
				}
			var sprite:IsoSprite = new IsoSprite();
			IsoMath.screenToIso(p);
			sprite.moveTo(p.x, p.y, 0);
			IsoMath.isoToScreen(p);
			p.x += 80;
			sprite.sprites = [bricks[i]];
			sprite.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
			sprite.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
			sprite.addEventListener(MouseEvent.CLICK, onObjClick, false, 0, true);
			objectScene.addChild(sprite);
			objectScene.render();
			}
		}
		
		private function onGrassClick(e:MouseEvent):void
		{
			objectScene.removeAllChildren();
			var grass:Array = [Grass1, Grass2, Grass3, Grass4, Grass5, Grass6, Grass7, Grass8] ;
			var p:Pt = new Pt(-20, -90);
			for (var i:int = 0; i < 8; i++)
			{
				if (i % 2 == 0)
				{
					p.x = -30;
					p.y += 80;
				}
				var sprite:IsoSprite = new IsoSprite();
				sprite.sprites = [grass[i]];
				IsoMath.screenToIso(p);
				sprite.moveTo(p.x, p.y, 0);
				IsoMath.isoToScreen(p);
				p.x += 80;
				sprite.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
				sprite.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
				sprite.addEventListener(MouseEvent.CLICK, onObjClick, false, 0, true);
				
				objectScene.addChild(sprite);
				objectScene.render();
			}
		}
 
        private function onRollOverHandler(e:ProxyEvent):void
        {
			var glow:GlowFilter = new GlowFilter(0xC24704, 1, 6, 6, 64);
            (e.target as IsoDisplayObject).container.filters = [glow];
        }
 
        private function onRollOutHandler(e:ProxyEvent):void
        {
            (e.target as IsoDisplayObject).container.filters = [];
        }
		
		private function onObjClick(e:ProxyEvent):void
		{
			var obj:IsoDisplayObject = e.target as IsoDisplayObject;
			obj = obj.clone();
			if(obj is IsoSprite && (obj as IsoSprite).sprites != null)
			{
				(obj as IsoSprite).sprites = (e.target as IsoSprite).sprites;
			}
			obj.moveTo(0, 0, 50);
			obj.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
			obj.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
			obj.addEventListener(MouseEvent.MOUSE_DOWN, onPickup, false, 0, true);
			
			scene.addChild(obj);
			scene.render();
		}
		
		private var dragPt:Pt;
		private var dragObject:IsoDisplayObject;
		private function onPickup(e:ProxyEvent):void
        {
            dragObject = e.target as IsoDisplayObject;
            dragPt = viewPort.localToIso(new Pt(stage.mouseX, stage.mouseY));
            dragPt.x -= dragObject.x;
            dragPt.y -= dragObject.y;
 
            dragObject.removeEventListener(MouseEvent.MOUSE_DOWN, onPickup);
 
            dragObject.addEventListener(MouseEvent.MOUSE_UP, onDrop, false, 0, true);
            viewPort.addEventListener(MouseEvent.MOUSE_UP, onDrop, false, 0, true);
            viewPort.addEventListener(MouseEvent.MOUSE_MOVE, onMoveObject, false, 0, true);
         }
 
        private function onDrop(e:Event):void
        {
            dragObject.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
            viewPort.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
            viewPort.removeEventListener(MouseEvent.MOUSE_MOVE, onMoveObject);
 
            dragObject.addEventListener(MouseEvent.MOUSE_DOWN, onPickup, false, 0, true);
         }
 
        private function onMoveObject(e:MouseEvent):void
        {
            var pt:Pt = viewPort.localToIso(new Pt(stage.mouseX, stage.mouseY));
 
            dragObject.moveTo(pt.x - dragPt.x, pt.y - dragPt.y, dragObject.z);
        }
	}
}