using ImGuiNET;
using Microsoft.Xna.Framework;
using skyscraper8.UI.MonoGame.Bridge;
using skyscraper8.UI.MonoGame.Forms;
using skyscraper8.UI.MonoGame.Screenhacks;

namespace skyscraper8.UI.MonoGame
{
	internal class SkyscraperGame : BaseGame
	{
		public SkyscraperGame(SkyscraperHandleCollection handles, Queue<string> errors)
		{
			this.Handles = handles;

			while (errors.Count > 0)
			{
				string dequeue = errors.Dequeue();
				EnqueueUpdateJob(() =>
				{
					_imGuiRenderables.Add(new MessageWindow(dequeue));
				});
			}
		}

		public SkyscraperHandleCollection Handles { get; set; }


		private ScreenhackManager screenhackManager;
		protected override void LoadContent()
		{
			screenhackManager = new ScreenhackManager();
			screenhackManager.WantedScreenhackId = 1;
			_gameObjects.Add(screenhackManager);
			base.LoadContent();
		}

		protected override void Draw(GameTime gameTime)
		{
			GraphicsDevice.Clear(Color.White);

			base.Draw(gameTime);
		}

		private void EnqueueUpdateJob(Action a)
		{
			if (updateJobs == null)
				updateJobs = new Queue<Action>();
			updateJobs.Enqueue(a);
		}

		private Queue<Action> updateJobs;

		protected override void Update(GameTime gameTime)
		{
			if (updateJobs != null)
			{
				while (updateJobs.Count > 0)
				{
					Action action = updateJobs.Dequeue();
					action();
				}
			}
			base.Update(gameTime);
		}

		private const int MENU_TEST_ABOUT = 1;
		private bool MenuTest(int opcode)
		{
			switch (opcode)
			{
				case MENU_TEST_ABOUT:	
					if (aboutWindow != null)
					{
						if (!aboutWindow.WasClosed())
							return false;
					}

					return true;
				default:
					Console.WriteLine("MenuTest Opcode {0} not implemented!");
					return false;
			}
		}

		private AboutWindow aboutWindow;

		

		protected override void ImGuiLayout()
		{
			
			ImGui.BeginMainMenuBar();
			if (ImGui.BeginMenu("Jobs"))
			{
				if (ImGui.MenuItem("Quit"))
				{
					Exit();
				}
				ImGui.EndMenu();
			}

			if (ImGui.BeginMenu("View"))
			{
				if (ImGui.MenuItem("Toggle Full Screen"))
				{
					_graphics.ToggleFullScreen();
				}
				ImGui.EndMenu();
			}

			if (ImGui.BeginMenu("Help"))
			{
				if (ImGui.MenuItem("About",MenuTest(MENU_TEST_ABOUT)))
				{
					EnqueueUpdateJob(() =>
					{
						aboutWindow = new AboutWindow();
						_imGuiRenderables.Add(aboutWindow);
					});
				}
				ImGui.EndMenu();
			}
			ImGui.EndMainMenuBar();

			base.ImGuiLayout();
		}
	}
}
