using System;
using System.Drawing;

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

namespace TuxHunter.UI
{
	/// <summary>
	/// This is your annoying paperclip-remover assistant.
	/// </summary>
	public class Snappy: AbstractComponent
	{
		private ImageComponent mSnappy;
		private ThoughtBubble mThoughtBubble;

		public Snappy()
		{
			mSnappy = new ImageComponent(FramesetManager.getInstance().loadImageFromId("ui/Snappy/Placeholder.gif"));
			mSnappy.IsVisible = true;
			this.AddChild(mSnappy);
			mSnappy.Click += new ClickCallback(mSnappy_Click);

			//Register for resize events:
			this.Resized += new ResizedCallback(Snappy_Resized);
		}

		/// <summary>
		/// If snappy is saying something, this function will shut him up.
		/// </summary>
		public void StopTalking()
		{
			if (mThoughtBubble != null)
			{
				this.RemoveChild(mThoughtBubble);
				mThoughtBubble = null;
			}
		}

		/// <summary>
		/// Calling this function will have snappy say the words passed in.
		/// </summary>
		/// <param name="text"></param>
		public void Say(string text)
		{
			//Check the global switch and stfu if necessary:
			if (!TuxHunterSettings.Instance.ShowSnappy)
			{
				this.IsVisible = false;
				return;
			}

			// First, unsay whatever we are already saying:
			this.StopTalking();

			// Make a new thought bubble:
			mThoughtBubble = new ThoughtBubble(text);
			this.AddChild(mThoughtBubble);
			mThoughtBubble.IsVisible = true;
			mThoughtBubble.Location = new Point(this.Size.Width - mThoughtBubble.Size.Width, this.Size.Height - mThoughtBubble.Size.Height - mSnappy.Size.Height);

			// Now, if Snappy is invisible, pop up! (That's the proper annoying thing to do. :))
			if (!this.IsVisible)
			{
				this.IsVisible = true;
			}
		}

		protected override void RenderSelf(Rectangle coordinates, Icarus.Graphics.Animation.IRenderParameters parameters)
		{
			//Nothing special to do...
		}

		private void Snappy_Resized(AbstractComponent sender, Size oldSize)
		{
			//Move snappy to the lower-right corner:
			mSnappy.Location = new Point(this.Size.Width - mSnappy.Size.Width, this.Size.Height - mSnappy.Size.Height);

			//Move thought bubble, too:
			if (mThoughtBubble != null)
			{
				mThoughtBubble.Location = new Point(this.Size.Width - mThoughtBubble.Size.Width, this.Size.Height - mThoughtBubble.Size.Height - mSnappy.Size.Height);
			}
		}

		private void mSnappy_Click(AbstractComponent sender)
		{
			this.Say(TuxHunter.Resources.TextManager.Instance.GetRandomSnappyQuote());
		}
	}
}
