using System;
using System.Drawing;

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

namespace TuxHunter.UI
{
	/// <summary>
	/// Delegate for receiving information when the level selection has changed.
	/// </summary>
	public delegate void SelectionChangedCallback(LevelList sender);

	/// <summary>
	/// This be a list of levels.
	/// </summary>
	public class LevelList : HighlightGrid
	{
		/// <summary>
		/// The names of the levels available.
		/// </summary>
		private string[] mLevelNames;

		/// <summary>
		/// Initializes the level list.
		/// </summary>
		public LevelList() : base(Layout.Column, null)
		{
			//Get the list of levels:
			mLevelNames = LevelLoader.Instance.GetLevelIDList();

			//Add these level things to the grid:
			foreach (string name in mLevelNames)
			{
				Label lbl = new Label(name);
				lbl.IsVisible = true;
				lbl.Click += new ClickCallback(lblLevel_Click);

				this.AddChild(lbl);
			}

			//Create a highlight animation thing:
			Bitmap highlight = new Bitmap(this.Size.Width, this.Size.Height / mLevelNames.Length);
			Graphics g = Graphics.FromImage(highlight);
			g.DrawRectangle(new Pen(Color.Black, 3), 0, 0, highlight.Width, highlight.Height);
			Frameset f = new Frameset();
			f.addFrame(highlight, new Point(highlight.Width / 2, highlight.Height / 2));
			this.HighlightAnimation = new FramesetAnimation(f, AnimationType.None, 1);
		}

		/// <summary>
		/// This event gets fired when the level selection changes.
		/// </summary>
		public event SelectionChangedCallback SelectionChanged;

		/// <summary>
		/// Fires off the SelectionChanged event.
		/// </summary>
		public void OnSelectionChanged()
		{
			if (this.SelectionChanged != null)
			{
				this.SelectionChanged(this);
			}
		}

		/// <summary>
		/// Called when one of the items in the list gets clicked.
		/// </summary>
		/// <param name="sender"></param>
		private void lblLevel_Click(AbstractComponent sender)
		{
			this.HighlightedComponent = sender;
			this.OnSelectionChanged();
		}
	}
}
