package  
{
	import net.flashpunk.Entity;	
	import net.flashpunk.FP;	
	import net.flashpunk.graphics.Graphiclist;
	import net.flashpunk.tweens.misc.ColorTween;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.utils.Input; 
	import net.flashpunk.utils.Key;
	
	import net.flashpunk.masks.Pixelmask;

	/**
	 * ...
	 * @author AntoineGuerchais
	 */
	public class Branch  extends Entity
	{
		[Embed(source = '../assets/graphics/trunk.png')] 
		private const BranchImage:Class;
		[Embed(source = '../assets/graphics/collide.png')] 
		private const BranchMask:Class;
		
		[Embed(source = '../assets/graphics/croissance_feuilles/feuilles_claire.png')] 
		private const Leaf:Class;
		[Embed(source = '../assets/graphics/croissance_feuilles/feuilles_sombre.png')] 
		private const DeadLeaf:Class;
		
		//-----------------------------------
		private var parent:Branch;
		public var childs:Array;
		private var bud:Bud;
		//-----------------------------------
		
		public var inLight:Boolean;
		public var angle:Number;
		//public var ancestor:Node;
		//public var successor:Node;
		private var age:Number;
		private var weight:Number;
		private var length:Number;
		public var image:Image;
		public var imageLeaf:Image;
		public var imageDeadLeaf:Image;
		
		public var endX:Number;
		public var endY:Number;
		
		//----------------------------------
		private var maxDepth:int;
		private var minDepth:int;
		//----------------------------------
		public	var pMask:Pixelmask;
		//public var pMask:PreRotatedMask;
		
		private var canBuds:Boolean;
		public var dead:Boolean;
		
		//-RENDER TREE--------------------------
		private var color:ColorTween;
		private var colorDebut:uint=0x64be2f;
		private var colorEnd:uint=0x5a3b27;
		//--------------------------------------
		
		public function Branch(parent:Branch=null, angle:Number=0.0) 
		{
			this.parent = parent;
			this.angle = angle;
			mask = pMask =  new Pixelmask(BranchMask);
			
			
			childs = new Array();
			//this.ancestor = ancestor;
			//this.successor = new Node(this);
			//ancestor.addBranch(this);
			var gl:Graphiclist = new Graphiclist();
			image = new Image(BranchImage);
			
			
			image.originX = image.width / 2.0;
			image.originY = image.height;
			image.scaleX = 0.1;
			image.scaleY = 0.0;
			
			imageLeaf = new Image(Leaf);
			imageLeaf.visible = false;
			imageLeaf.originX = imageLeaf.width * 0.5;
			imageLeaf.originY = imageLeaf.height * 0.5;
			imageDeadLeaf = new Image(DeadLeaf);
			imageDeadLeaf.visible = false;
			imageDeadLeaf.originX = imageDeadLeaf.width * 0.5;
			imageDeadLeaf.originY = imageDeadLeaf.height * 0.5;
			//pMask.frameAngle = image.angle;
			//image.updateBuffer(true);
			gl.add(image);
			gl.add(imageLeaf);
			gl.add(imageDeadLeaf);
			graphic = gl;
			age = 0;
			FP.world.add(this);
			bud = new Bud(this);
			FP.world.add(bud);
			//FP.world.add(successor);
			//angle = Math.random() * 90 - 45;//0.0;
			
			maxDepth = 0;
			minDepth = 0;
			if (parent != null)
			{
				x = parent.endX;
				y = parent.endY;
			}
			
			color = new ColorTween();
			color.tween(500, colorDebut, colorEnd);
			color.start();
			
			
			if (parent != null)
			{
				image.angle = parent.angle + angle;
			}else
			{
				image.angle = 0;
			}
			
			canBuds = true;
			dead = false;
			inLight = false;
		}
		
		override public function removed():void
		{
			canBuds = false;
			
			FP.world.remove(bud);
			if (parent != null)
			{
				//FP.console.log("Nb branch before:" + parent.childs.length + "\n");;
				for (var i:int = 0; i < childs.length;++i)
				{
					if(parent.childs[i]==this)parent.childs.splice(i, 1);
				}
				//FP.console.log("Nb branch after:" + parent.childs.length + "\n");
				for each(var b:Branch in childs)
				{
					FP.world.remove(b);
				}
			}
			super.removed();
			delete this;
		}
		
		override public function update():void
		{
			
			if (!(FP.world as TreeWorld).paused)
			{
				age += FP.elapsed;
				
				//if (canBuds)
				{
					if (canBuds && childs.length == 0)
					{
						//image.scaleX = 0.03*age;
						image.scaleY += 0.15 * FP.elapsed;// age;
					}
					else
					{
						image.scaleX += 0.04 * FP.elapsed;
						if (parent != null) image.scaleX = Math.min(image.scaleX, parent.image.scaleX * 0.9);
						else  image.scaleX = Math.min(image.scaleX, image.scaleY);
						
					}
				
					image.color = color.color;
					
					//Calculate the end of the branch
					if (childs.length == 0)
					{
						endX = x + Math.sin(image.angle * FP.RAD) * image.scaledHeight;
						endY = y - Math.cos(image.angle * FP.RAD) * image.scaledHeight;
						
						pMask.x = endX - x;
						pMask.y = endY - y;
						
						if (Math.abs(FP.world.mouseX - (endX + x)*0.5) < 10 && Math.abs(FP.world.mouseY - (endY + y)*0.5) < 10)
						{
							Clipper.over = true;
							if (Input.mousePressed)
							{
								SoundManager.playCut(x,y);
								FP.world.remove(this);
							}
						}
					}
				}
				//-------------------------------			
				color.update();
				//-------------------------------
			}
		}
		
		override public function render():void
		{
			//DEBUG-----------------------
			if (childs.length==0 && Math.abs(FP.world.mouseX - (endX+x)*0.5) < 10 && Math.abs(FP.world.mouseY - (endY+y)*0.5) < 10)
			{
				//Draw.circlePlus(FP.world.mouseX, FP.world.mouseY, 4);
				image.color = FP.getColorRGB(255, 0, 0);
			}
			
			super.render();
			
			
			//if(childs.length==0)Draw.hitbox(this, true,0xFF0000);
			//Draw.circlePlus(endX, endY, 20);
		}
		
		public function buds():void
		{
			for each(var b:Branch in childs)
			{
				b.buds();
			}
			
			if (canBuds && childs.length == 0)
			{
				canBuds = false;
				var rand:int = Math.random() * 3 + 1;
				var randAngle:Number = Math.random() * 90-90;
				for (var i:int = 0; i < rand; ++i)
				{
					var teta:Number = randAngle + ((i+1) -rand/2) * 45;
					var branch:Branch = new Branch(this,teta);
					//branch.angle = 
					
					childs[childs.length] = branch;
					//FP.console.log("New Branch : Pos[" + branch.x + "," + branch.y + "]");
				}
			}
		}
		
		public function stop():void
		{
			for each(var b:Branch in childs)
			{
				b.stop();
			}
			if ((FP.world as TreeWorld).atEnd && childs.length == 0)
			{
				imageLeaf.visible = inLight;
				imageLeaf.x = endX -x;				
				imageLeaf.y = endY - y;
				imageDeadLeaf.visible = !inLight;				
				imageDeadLeaf.x = endX - x;
				imageDeadLeaf.y = endY - y;
			}
			canBuds = false;
			dead = true;
		}
		
	}

}