using System;
using GDI=System.Drawing;

using D3D=SharpDX.Direct3D9;

using Icarus.Graphics.Animation;
using Icarus.Renderer;

namespace Icarus.Renderer.Animation
{
	/// <summary>
	/// Text implementation for directx.
	/// </summary>
	public class D3DText : AbstractText
	{
		/// <summary>
		/// The directx font corresponding to the system font we are using.
		/// </summary>
		private D3D.Font mFont;

		public D3DText(string text, GDI.Color c, GDI.Font f) : base(text, c, f) { }

		private SharpDX.Direct3D9.FontDescription D3D9Font
		{
			get
			{
				var sysFont = GetFont();

				SharpDX.Direct3D9.FontDescription fd = new D3D.FontDescription();

				fd.Height = sysFont.Height;
				fd.FaceName = sysFont.Name;

				return fd;
			}
		}

		public override void UpdateText()
		{
			mFont = new D3D.Font(Direct3DSettings.Instance.RenderingDevice, this.D3D9Font);
		}

		public override void Render(GDI.Rectangle destination, IRenderParameters parameters)
		{
			D3DRenderParameters p = (D3DRenderParameters) parameters;

			SharpDX.Mathematics.Interop.RawColorBGRA color 
				= new SharpDX.Mathematics.Interop.RawColorBGRA(GetColor().B, GetColor().G, GetColor().R, GetColor().A);

			mFont.DrawText(p.TargetSprite, this.GetText(), destination.Location.X, destination.Location.Y, color);
		}
	}
}
