using System;
using Gizmox.WebGUI.Forms;
using Habanero.Faces.Base;
using Habanero.Faces.VWG;
using <<UIPROJECT_NAME>>;

namespace <<NAMESPACE>>
{
    public class MainForm : FormVWG
    {
        public MainForm()
        {
            IControlHabanero panel = GlobalUIRegistry.ControlFactory.CreatePanel();
            panel.Dock = Habanero.Faces.Base.DockStyle.Fill;
            this.Controls.Add((Control)panel);
            IMainMenuHabanero mainMenuHabanero = SetupMainMenu(this, GlobalUIRegistry.ControlFactory, new MenuBuilderVWG(GlobalUIRegistry.ControlFactory));
            Menu = (MainMenu)mainMenuHabanero;
        }

		public static IMainMenuHabanero SetupMainMenu(IFormHabanero programForm, IControlFactory controlFactory, IMenuBuilder menuBuilder)
		{
			HabaneroMenu mainMenu = new HabaneroMenu("Main", programForm, GlobalUIRegistry.ControlFactory);

			HabaneroMenu currentMenu = mainMenu.AddSubMenu("&File");
			HabaneroMenu.Item currentMenuItem = currentMenu.AddMenuItem("E&xit");
			currentMenuItem.CustomHandler += delegate { programForm.Close(); };

			AddGeneratedFormsMenu(mainMenu, controlFactory);

			return menuBuilder.BuildMainMenu(mainMenu);
		}

		private static void AddGeneratedFormsMenu(HabaneroMenu menu, IControlFactory controlFactory)
		{
			HabaneroMenu dataMenu = menu.AddSubMenu("&Generated Forms");
			foreach (Type type in typeof (MainForm).Assembly.GetTypes())
			{
				if (!type.IsSubclassOf(typeof (MenuItemCreator))) continue;
				MenuItemCreator menuItemCreator = (MenuItemCreator) Activator.CreateInstance(type, new object[] {});
				menuItemCreator.AddToMenu(dataMenu, controlFactory);
			}
		}
	}
}