Engine.ScreenController = class
{
	
	constructor(parentGame /*GameBase*/)
	{
		this.openScreens = [];
		this.activeScreen = null;
		this.onScreenShown = null;
		this.transition = new Engine.ScreenTransition();
		this.oldScreen = null;
		this.skipScreens = [];
		
		this.parentGame = parentGame;
		
		this.screenClip = new Engine.Drawable();
		this.screenClip.setGameObjectName("Screen Controller");
		parentGame.root.addChild(this.screenClip);
		
	}

	getScreenClip() { return screenClip; }

	resize(width /*int*/, height /*int*/) { }

	update(timeElapsed /*float*/)
	{
		if (this.activeScreen != null) this.activeScreen.update(timeElapsed);
	}

	getScreensList() { return this.openScreens; }

	screenShown(gameScreen /*GameScreen*/)
	{
		
		this.screenClip.addChild(gameScreen);
		
		if (this.activeScreen != null)
		{
			this.openScreens.push(this.activeScreen);
			
			if (this.skipScreens.length > 0)
			{
				for (var i = 0; i < this.skipScreens.length; i++ ) { this.openScreens.push(this.skipScreens[i]) };
				this.skipScreens = [];
			}
			
			this.oldScreen = this.activeScreen;
			this.activeScreen.beginHide();
			
			this.transition.init(this.activeScreen, gameScreen);
			this.transition.transitionIn(() => { this.screenTransitioned() });
		}
		else
		{
			gameScreen.x = 0;
			gameScreen.y = 0;
			gameScreen.scale.x = 1;
			gameScreen.scale.y = 1;
			gameScreen.alpha = 1;
		}
		
		this.activeScreen = gameScreen;
		if (this.onScreenShown != null) this.onScreenShown(gameScreen);
		

	}

	screenTransitioned()
	{
		this.screenClip.removeChild(this.oldScreen);
		this.activeScreen.transitionComplete();
		if (this.oldScreen != null)
		{
			this.oldScreen.hideTransitionComplete();
			this.oldScreen = null;
		}
	}

	screenHidden(gameScreen /*GameScreen*/)
	{
		
		gameScreen.beginHide();
		this.oldScreen = gameScreen;
		var screenIndex = this.openScreens.indexOf(gameScreen);
		if (screenIndex != -1)
		{
			this.openScreens.remove(gameScreen);
		}
		
		this.activeScreen = null;
		if (this.openScreens.length > 0)
		{
			this.activeScreen = this.openScreens.pop();
			
			this.activeScreen.reShow();
			
			this.transition.init(gameScreen, this.activeScreen);
			this.transition.transitionOut(() => { this.screenTransitioned(); } );
			
			this.screenClip.addChild(gameScreen);
			this.screenClip.addChild(this.activeScreen);
		}
	}

	skipScreenInStack(screen /*GameScreen*/)
	{
		this.skipScreens.push(screen);
	}

}