using System;
using System.Drawing;
using System.Threading;

using Icarus.Graphics.Animation;
using Icarus.LUCIE;
using Icarus.Logic;
using Icarus.SoundEngine;

namespace TuxHunter.GameStates
{
	/// <summary>
	/// This is just a screen that entertains the user while the level is being loaded. :)
	/// </summary>
	public class LevelLoadingState : MenuState
	{
		private string mLevelId;

		/// <summary>
		/// Creates a new level loading state.
		/// </summary>
		/// <param name="levelId">The id of the level to load.</param>
		public LevelLoadingState(string levelId)
		{
			//Get the image of the background:
			Bitmap bgimage = FramesetManager.getInstance().loadImageFromId("ui/MenuBackground.gif");

			//Make an image component:
			ImageComponent bg = new ImageComponent(bgimage);
			bg.Location = new Point(0, 0);
			bg.IsVisible = true;

			// Make loading label:
			Label lblLoading = new Label(String.Format("Please wait while the level is loading...{0}{1}", "\n", levelId));
			lblLoading.Location = new Point(300, 200);
			lblLoading.IsVisible = true;

			//Add the children to the UI:
			this.UserInterface.AddChild(bg);
			this.UserInterface.AddChild(lblLoading);

			this.mLevelId = levelId;
		}

		public override void Unpause()
		{
			base.Unpause();
			SoundEngine.Instance.PlayMusic();

			new Thread(new ThreadStart(LoadLevel)).Start();
		}

		public override void Pause()
		{
			base.Pause();
			SoundEngine.Instance.PauseMusic();
		}


		public void LoadLevel()
		{
			Level lvl = Icarus.Logic.GameEngine.LevelLoader.Instance.LoadLevelXML(mLevelId);

			// Pop self:
			Icarus.Logic.GameEngine.GameEngine.Instance.PopState();

			// Start game:
			Icarus.Logic.GameEngine.GameEngine.Instance.PushState(new GameplayState(lvl));
		}
	}
}
