using System;
using System.Drawing;

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

using TuxHunter.UI;

namespace TuxHunter.GameStates
{
	/// <summary>
	/// This thing displays a level list and allows us to load a level.
	/// </summary>
	public class LevelListState : MenuState
	{
		private Snappy mSnappy;
		private LevelList mLevelList;

		public LevelListState()
		{
			//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;
			bg.Click += new ClickCallback(bg_Click);

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

			//OMFG! Teh Clippie!
			mSnappy = new Snappy();
			mSnappy.Location = new Point(400, 100);
			mSnappy.Size = new Size(380, 280);
			mSnappy.IsVisible = false;
			this.UserInterface.AddChild(mSnappy);

			//Add the list of levels:
			mLevelList = new LevelList();
			mLevelList.Location = new Point(100, 100);
			mLevelList.IsVisible = true;
			this.UserInterface.AddChild(mLevelList);
			mLevelList.SelectionChanged += new SelectionChangedCallback(mLevelList_SelectionChanged);

			//Add the gogogo button:
			Label btnGogogo = new Label("GO!", Color.Red, new Font(FontFamily.GenericSerif.Name, 16, FontStyle.Bold));
			btnGogogo.Location = new Point(300, 400);
			btnGogogo.IsVisible = true;
			btnGogogo.Click += new ClickCallback(btnGogogo_Click);
			this.UserInterface.AddChild(btnGogogo);
		}

		public override void Enter()
		{
			SoundEngine.Instance.SetMusicFromId("menu/Creep Walking.mp3");
			base.Enter();
		}

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

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

		public override void Exit()
		{
			base.Exit();
			SoundEngine.Instance.StopMusic();
		}

		private void btnGogogo_Click(AbstractComponent sender)
		{
			if (mLevelList.HighlightedComponent == null)
			{
				mSnappy.Say("Ummm... You might want to pick a level\nfrom the list before clicking that.");
			}
			else
			{
				Icarus.Logic.GameEngine.GameEngine.Instance.PushState(new LevelLoadingState(((Label) mLevelList.HighlightedComponent).Text));
			}
		}

		private void bg_Click(AbstractComponent sender)
		{
			mSnappy.Say(
@"Looks like you are
trying to start a
game! LOL You might
want to, ya know,
click on one of the
the levels in the list.
       ;)");
		}

		private void mLevelList_SelectionChanged(LevelList sender)
		{
			if (mSnappy.IsVisible)
			{
				mSnappy.Say(
@"There ya go! You've selected a level! Here,
have a cookie. No, no, don't be shy, you
deserve it. :)");
			}
			else
			{
				mSnappy.Say(
@"Looks like you selected a level!
Now click the big red GO! button to
start playing the game!");
			}
		}
	}
}
