
Engine.SceneModeController = class
{

	constructor()
	{
		this.zoom = 1;
		this.positionX = 0;
		this.positionY = 0;
		this.dragging = false;
		this.draggingDrawable = null;
		this.active = false;
	}

	update(timeElapsed)
	{
		
	}


	toggle()
	{
		if (this.active)
		{
			this.deactivate();
		} 
		else
		{
			this.activate();
		}
	}

	activate()
	{
		//Stop the EnterFrameManager from updating

		GameManagerStatics.DrawableUpdatePaused = true;

		//Itterate over all drawables and take over thier even handling
		this.addDrawableEvents(Engine.rootDrawable);

		//Add camera controller events to the mouse, for pan/zoom.
		this.oldMouseEventHandlers = Engine.Mouse.eventHandlers;
		Engine.Mouse.eventHandlers = {};
		Engine.Mouse.eventHandlers["mousedown"] = new Map();
		Engine.Mouse.eventHandlers["mouseup"] = new Map();
		Engine.Mouse.eventHandlers["mousewheel"] = new Map();
		Engine.Mouse.eventHandlers["mousemove"] = new Map();
		Engine.Mouse.eventHandlers["mouseflick"] = new Map();
		Engine.Mouse.eventHandlers["mouseclick"] = new Map();

		Engine.Mouse.addEventListener("mousedown", this, (e) => { this.mouseDown(e); });
		Engine.Mouse.addEventListener("mouseup", this, (e) => { this.mouseUp(e); });
		Engine.Mouse.addEventListener("mousewheel", this, (e) => { this.mouseWheel(e); });
		Engine.Mouse.addEventListener("mousemove", this, (e) => { this.mouseMove(e); });

		this.oldEnterFrameQueue = Engine.EnterFrameManagerInstance.enterFrameQueue;
        this.oldPostFrameQueue = Engine.EnterFrameManagerInstance.postFrameQueue;

		Engine.EnterFrameManagerInstance.enterFrameQueue = new Engine.CallbackQueue(this);
        Engine.EnterFrameManagerInstance.postFrameQueue = new Engine.CallbackQueue(this);

        this.active = true;
	}


	deactivate()
	{
		GameManagerStatics.DrawableUpdatePaused = false;
		Engine.Mouse.eventHandlers = this.oldMouseEventHandlers;
		this.removeDrawableEvents(Engine.rootDrawable);

		Engine.EnterFrameManagerInstance.enterFrameQueue = this.oldEnterFrameQueue;
        Engine.EnterFrameManagerInstance.postFrameQueue = this.oldPostFrameQueue;

        this.active = false;
	}

	mouseDown(drawable)
	{

		if (this.draggingDrawable != null && !Engine.Mouse.mouseButtonDown[1]) return;

		this.draggingDrawable = drawable;

		if (Engine.Mouse.mouseButtonDown[1])
		{
			this.draggingDrawable = Engine.rootDrawable;
		}
		
		this.startPosX = Engine.Mouse.position.x;
		this.startPosY = Engine.Mouse.position.y;
		this.drawableStartX = this.draggingDrawable.x;
		this.drawableStartY = this.draggingDrawable.y;

	}

	mouseUp(pos)
	{
		this.dragging = false;
		this.draggingDrawable = null;
	}

	mouseMove(pos)
	{
		if (this.draggingDrawable != null)
		{
			var deltaX = this.startPosX - pos.x;
			var deltaY = this.startPosY - pos.y;

			this.draggingDrawable.x = this.drawableStartX - deltaX;
			this.draggingDrawable.y = this.drawableStartY - deltaY;
		}
	}

	mouseWheel(velocity)
	{
		this.zoom -= velocity * 0.1;

		Engine.rootDrawable.scale.x = this.zoom;
		Engine.rootDrawable.scale.y = this.zoom;
	}

	addDrawableEvents(drawable)
	{

		if (drawable.events === undefined) return;
		if (drawable.sceneModeData === undefined) drawable.sceneModeData = {};

		drawable.sceneModeData.oldEventData = {};
		drawable.sceneModeData.oldEventData.onClick = drawable.events.onClick;
		drawable.sceneModeData.oldEventData.onMouseOver = drawable.events.onMouseOver;
		drawable.sceneModeData.oldEventData.onMouseOut = drawable.events.onMouseOut;
		drawable.sceneModeData.oldEventData.onMouseDown = drawable.events.onMouseDown;
		drawable.sceneModeData.oldEventData.onMouseUp = drawable.events.onMouseUp;
		drawable.sceneModeData.oldEventData.eventsActive = drawable.events.mouseEnabled;

		drawable.events.onClick = new Action();
		drawable.events.onMouseOver = new Action();
		drawable.events.onMouseOut = new Action();
		drawable.events.onMouseDown = new Action();
		drawable.events.onMouseUp = new Action();

		if (!drawable.events.mouseEnabled)
		{
			drawable.events.enableMouseEvents();
		}

		drawable.events.onMouseDown.addListener(this, () => { this.mouseDown(drawable); } );

		for (var i = 0; i < drawable.children.length; i++)
		{
			this.addDrawableEvents(drawable.children[i]);
		}

	}

	removeDrawableEvents(drawable)
	{
		if (drawable.events === undefined) return;

		drawable.events.onClick = drawable.sceneModeData.oldEventData.onClick;
		drawable.events.onMouseOver = drawable.sceneModeData.oldEventData.onMouseOver;
		drawable.events.onMouseOut = drawable.sceneModeData.oldEventData.onMouseOut;
		drawable.events.onMouseDown = drawable.sceneModeData.oldEventData.onMouseDown;
		drawable.events.onMouseUp = drawable.sceneModeData.oldEventData.onMouseUp;

		if (!drawable.sceneModeData.oldEventData.eventsActive)
		{
			drawable.events.disableMouseEvents();
		}



		for (var i = 0; i < drawable.children.length; i++)
		{
			this.removeDrawableEvents(drawable.children[i]);
		}

	}




}