package ae {

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;

import flash.events.Event;
import flash.events.MouseEvent;

import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.Point;

import flash.net.navigateToURL;
import flash.net.URLRequest;

import flash.system.Security;
import flash.system.LoaderContext;

// this class is used as the document class
// holds the bitmap, and handles all drawing commands to it
public class CircleSplit extends MovieClip 
{
	private static var bgb:BitmapData;
	public static var bg:Bitmap;
	protected var bgHolder:Sprite;
	private var win:Boolean = false;
	public var sp:Boolean = false;
	
	public var picLoader:Loader;
	public var pic:BitmapData = null;
	
	public var rootNode:CircleNode;

	public function CircleSplit()
	{
		bgb = new BitmapData(1024, 1024, false, 0x000000);
		bg = new Bitmap(bgb);
		bg.smoothing = true;
		bgHolder = new Sprite();
		addChild(bgHolder);
		bgHolder.addChild(bg);
		
		Security.allowDomain('*');
		
		//check for a "p" parameter to the swf
		var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
		
		if(paramObj["p"] != null)
		{
			//if we have a param, load the image
			var loaderContext:LoaderContext = new LoaderContext();
			loaderContext.checkPolicyFile = true;

			picLoader = new Loader();
			picLoader.load( new URLRequest(paramObj["p"]), loaderContext );
			picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, handlePicLoaded);
		}
		createCircle(1024,0,0);
		
	}
	
	public function handlePicLoaded(e:Event) : void
	{
		// once the image loads, pic will no longer will be null,
		// and colors from the image will be used instead of random colors
		var b:Bitmap = picLoader.content as Bitmap;
		pic = b.bitmapData;
	}
	
	public function createCircle(size:Number, x:Number, y:Number) : void
	{
		//you can vary the width of the starting circle
		var w = 1024;
		var circle:Circle = new Circle(w, w, this);
		circle.setColor();
		
		//center the circle
		circle.x = (1024-w) / 2;
		circle.y = (1024-w) / 2;
		
		//add it and draw it
		this.addChild(circle);
		drawCircle(circle);
		
		//set up the tree root node
		rootNode = new CircleNode(circle);
		rootNode.xThresh = 512;
		rootNode.yThresh = 512;
		
		addEventListener(MouseEvent.MOUSE_MOVE, handleMouseMove);
	}
	
	public function handleMouseMove(e:MouseEvent)
	{
		// start a search of the quad-tree
		rootNode.findTarget(e);
		if(rootNode.isEmpty() && !win && sp)
		{
			win = true;
			// do fun stuff
		}
	}
	
	//draw a black rectangle where a circle used to be
	public function drawSquare(c:Circle, color:int = 0x00000000)
	{
		var s:Sprite = new Sprite();
		s.graphics.beginFill(color);
		s.graphics.drawRect(0, 0, c.w, c.w)
		s.graphics.endFill();
		
		var m:Matrix = new Matrix();
		m.tx = c.x; m.ty = c.y;
		
		bg.bitmapData.draw(s, m);
	}
	
	public function copyPic(c:Circle)
	{
		bg.bitmapData.copyPixels(pic, new Rectangle(c.x, c.y, c.w, c.h), new Point(c.x, c.y));
	}
	
	// draw a colored circle
	public function drawCircle(c:Circle)
	{
		var s:Sprite = new Sprite();
		s.graphics.beginFill(c.c);
		s.graphics.drawCircle(c.r, c.r, c.r);
		//s.graphics.drawRect(0, 0, c.w, c.w)
		s.graphics.endFill();
		
		var m:Matrix = new Matrix();
		m.tx = c.x; m.ty = c.y;
		
		bg.bitmapData.draw(s, m);
	}
	
	// draw 4 circles
	// adds them as children if they're above a certain size
	// to allow future recursion
	public function addCircles(a:Array)
	{
		sp = true;
		var obj:Object;
		var c:Circle;
		if(a[0].w > 4)
		{
			// we still want to recurse deeper
			for each(obj in a)
			{
				c = obj as Circle;
				addChild(c);
				drawCircle(c);
			}
		}
		else
		{
			// don't want to recurse deeper, delete the node
			// to start a chain reaction up
			for each(obj in a)
			{
				c = obj as Circle;
				c.node.deleteMe();
				if(pic == null)
					drawCircle(c);
				else
					copyPic(c);
			}
		}
		
		
	}

	

}
}