using System;
using System.Drawing;
using System.Drawing.Drawing2D;

using Icarus.LUCIE;
using Icarus.Utilities.Graphics;

namespace TuxHunter.UI
{
	/// <summary>
	/// This is a UI component for a thought bubble kind of thing. It's mainly used for Snappy.
	/// </summary>
	public class ThoughtBubble: AbstractComponent
	{
		/// <summary>
		/// Creates a new thought bubble.
		/// </summary>
		/// <param name="thought">What should be displayed in the bubble.</param>
		public ThoughtBubble(string thought)
		{
			Label lblThought = new Label(thought, Color.Black);
			ImageComponent imgBubble = new ImageComponent(ThoughtBubble.MakeThoughtBubbleImage(lblThought.Size));

			lblThought.IsVisible = true;
			imgBubble.IsVisible = true;

			lblThought.Location = new Point(10, 10);

			this.AddChild(imgBubble);
			this.AddChild(lblThought);

			this.Size = imgBubble.Size;
		}

		/// <summary>
		/// Creates a new bitmap of a thought bubble.
		/// </summary>
		/// <param name="size">The size of the contents of the bubble.</param>
		/// <returns></returns>
		public static Bitmap MakeThoughtBubbleImage(Size size)
		{
			Color inside = Color.FromArgb(255, 255, 204);
			Color outline = Color.DarkBlue;

			Brush brushInside = new SolidBrush(inside);
			Pen penOutside = new Pen(outline);

			Bitmap result = new Bitmap(size.Width + 25, size.Height + 50);
			Graphics graphics = Graphics.FromImage(result);
			ExtendedGraphics g = new ExtendedGraphics(graphics);

			//Render the little triangle pointing out:
			GraphicsPath pointer = ThoughtBubble.MakePointer(size.Width + 25, size.Height + 50);
			graphics.FillPath(brushInside, pointer);
			graphics.DrawPath(penOutside, pointer);

			//Render the thought bubble:
			g.FillRoundedRectangle(brushInside, 0, 0, size.Width + 20, size.Height + 20, 10);
			g.DrawRoundedRectangle(penOutside,  0, 0, size.Width + 20, size.Height + 20, 10);

			return result;
		}

		/// <summary>
		/// Given the width and height of the *thought bubble*, returns a path to make a little pointer from the bubble to the speaker.
		/// </summary>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <returns></returns>
		private static GraphicsPath MakePointer(int width, int height)
		{
			Point[] points = new Point[4];
			points[0] = new Point(2 * width / 5, height / 2);
			points[1] = new Point(width / 2, height / 2);
			points[2] = new Point(width / 2, height - 2);
			points[3] = new Point(2 * width / 5, height / 2);

			byte[] types = new byte[] { (byte) PathPointType.Start, (byte) PathPointType.Line, (byte) PathPointType.Line, (byte) PathPointType.Line };
			GraphicsPath result = new GraphicsPath(points, types);
			result.CloseFigure();

			return result;
		}

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

	}
}
