using System;
using System.Drawing;

using Microsoft.DirectX.Direct3D;

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

namespace ComponentTests
{
	/// <summary>
	/// This is the main class for testing the components.
	/// </summary>
	public class MainClass
	{
		private D3DGameWindow mMainWindow;
		private RootComponent mUserInterface;
		private HighlightGrid hgrid;

		/// <summary>
		/// Here, we have the test components to display.
		/// </summary>
		public void SetupComponents()
		{
			Icarus.Resources.ResourceManager.Instance.BasePath = "data\\";

			mUserInterface = new RootComponent(mMainWindow);
			mUserInterface.IsVisible = true;

			ImageComponent testImage;
			Bitmap image = (Bitmap) Bitmap.FromFile("data\\tux.png");
			testImage = new ImageComponent(image);
			mUserInterface.AddChild(testImage);

			//Set new position:
			testImage.Location = new Point(100, 100);

			//Show:
			testImage.IsVisible = true;

			//Test the clicking action:
			testImage.Click += new ClickCallback(testImage_Click);

			//Test text:
			Label lbl = new Label("Tux Hunter!", Color.Red);
			mUserInterface.AddChild(lbl);
			lbl.Location = new Point(200, 200);
			lbl.IsVisible = true;

			//Grid of weapon things:
			Bitmap fshot = (Bitmap) Bitmap.FromFile("data\\FreezeShot.gif");
			Bitmap nshot = (Bitmap) Bitmap.FromFile("data\\NormalShot.gif");
			Frameset selector = FramesetManager.getInstance().getFrameset("WeaponSelector.gif");
			IAnimation wesel = new AbstractFramesetAnimation(selector, AnimationType.None, 1);
			
			ImageComponent freeze = new ImageComponent(fshot);
			ImageComponent normal = new ImageComponent(nshot);
			freeze.IsVisible = true;
			normal.IsVisible = true;

			freeze.Click += new ClickCallback(weapon_Click);
			normal.Click += new ClickCallback(weapon_Click);

			hgrid = new HighlightGrid(Layout.Row, wesel);
			hgrid.AddChild(freeze);
			hgrid.AddChild(normal);
			hgrid.Location = new Point(100, 500);
			hgrid.IsVisible = true;
			hgrid.HighlightedComponent = freeze;

			mUserInterface.AddChild(hgrid);
		}

		public void MyRenderFunction(IRenderParameters parameters)
		{
			D3DRenderParameters p = (D3DRenderParameters) parameters;

			p.TargetSprite.Begin(SpriteFlags.DoNotSaveState | SpriteFlags.AlphaBlend);

			mUserInterface.Render(new Point(0, 0), p);

			p.TargetSprite.Flush();
			p.TargetSprite.End();
		}

		public void Run()
		{
			//Tell Animation to use D3D:
			Icarus.Graphics.Animation.AnimationFactory.setInstance(new Icarus.Renderer.Animation.D3DAnimationFactory());

			//Set up the game window:
			mMainWindow = new D3DGameWindow("LUCIE Tests", true);
			mMainWindow.CapFrameRate = true;
			mMainWindow.FrameRate = 60;
			mMainWindow.RenderCallback = new RenderFunction(MyRenderFunction);

			//Set up the UI components to display:
			SetupComponents();

			//Run, window, run!
			System.Windows.Forms.Application.Run(mMainWindow);
		}

		[STAThread]
		public static void Main(string[] args) 
		{
			new MainClass().Run();
		}

		private void testImage_Click(AbstractComponent sender)
		{
			System.Windows.Forms.MessageBox.Show(mMainWindow, "You clicked tux!");
		}

		private void weapon_Click(AbstractComponent sender)
		{
			hgrid.HighlightedComponent = sender;
		}
	}
}
