using ImGuiNET;
using Nerfed.Runtime;
using Nerfed.Runtime.Graphics;
using Nerfed.Runtime.Gui;

namespace Nerfed.Editor
{
    internal static class EditorGui
    {
        private static GuiController guiController;

        internal static void Initialize()
        {
            // Create GuiController.
            guiController = new GuiController(Engine.GraphicsDevice, Engine.MainWindow, Color.DimGray);
            // Subscribe to GUI update.
            // GuiController.OnGui call => UpdateDock;
            // GuiController.OnGui call => UpdateEditorWindows;
            // GuiController.OnGui call => ...;
            guiController.OnGui += HandleOnGui;
        }

        internal static void Update()
        {
            // Update GuiController.
            guiController.Update(Engine.Timestep.TotalSeconds);
        }

        internal static void Render()
        {
            // Reneder GuiController.
            guiController.Render();
        }

        internal static void Quit()
        {
            guiController.Dispose();
        }

        private static void UpdateDock()
        {
            // Setup default dockspace for the main window.
            uint id = ImGui.GetID("MainDockSpace");
            ImGui.DockSpaceOverViewport(id, ImGui.GetMainViewport(), ImGuiDockNodeFlags.None);
        }

        private static void UpdateMainMenu()
        {
            if (ImGui.BeginMainMenuBar())
            {
                if (ImGui.BeginMenu("File"))
                {
                    if (ImGui.MenuItem("Exit"))
                    {
                        Engine.Quit();
                    }
                    ImGui.EndMenu();
                }
                ImGui.EndMainMenuBar();
            }
        }

        private static void HandleOnGui()
        {
            UpdateMainMenu();
            UpdateDock();

            ImGui.ShowDemoWindow();
        }
    }
}