/*
 *
 * ShortcutManager
 *
 * This is the client-side shortcut keybind manager
 *
 *
*/

package jesse.keeblarcraft.Shortcuts;

import org.lwjgl.glfw.GLFW;

// import jesse.keeblarcraft.Keeblarcraft;
// import jesse.keeblarcraft.gui.ScreenManager;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;

public class ShortcutManager {
    private static KeyBinding skillTreeShortcut;
    private static KeyBinding globalConfig;
    private static Boolean toggleTreeGui = true; // Initialized to true for first time pressing keybind to actually display gui
    // private static ScreenManager treeGui = null;

    /////////////////////////////////////////////////////////////////////////////
    /// @fn         RegisterKeybinds
    ///
    /// @brief      Registers all player keybinds and callbacks to them
    /////////////////////////////////////////////////////////////////////////////
    // All callbacks for keyboard shortcuts are put here
    public static void RegisterKeybinds() {
        // Keybindings are registered here
        skillTreeShortcut = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.keeblarcraft.treemap",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_K,
            "category.keeblarcraft.test"
        ));

        globalConfig = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.keeblarcraft.globalconfig",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_M,
            "category.keeblarcraft.test"
        ));

        /// Callbacks are down here

        ClientTickEvents.END_CLIENT_TICK.register(client -> {
            // The keybinding util does 2 things in one, if I check the state of the key press it flips a bit and deducts from
            // the actual pressed amount - which we don't want to do. We will call the callbacks for keybinding functions here,
            // and let them check the keybind themselves. NOTE: These functions are called at the end of every tick! Future us
            // should see if there is a less expensive way to check this on just the keybind itself being flipped
            DrawTreeGui(client);

            if (globalConfig.wasPressed()) {
                client.player.sendMessage(Text.of("Keeblarcraft recognizes you pressed the config button, but this option doesn't do anything yet"), false);
            }
        });
    }

    /////////////////////////////////////////////////////////////////////////////
    /// @fn         DrawTreeGui
    ///
    /// @arg[in]    client is the minecraft client instance that is passed in
    ///             so we have context of the client itself
    ///
    /// @brief      Draws the attribute tree GUI
    /////////////////////////////////////////////////////////////////////////////
    @SuppressWarnings("resource")
    public static void DrawTreeGui(MinecraftClient client) {
        while (skillTreeShortcut.wasPressed()) { // Constant loop required to redraw screen continuously
            System.out.println("Key pressed detected"); /// TODO: When we figure out how to get key presses to still work when a screen opens; then we can toggle menus with buttons...
            if (toggleTreeGui) {
                // There is a technical resource leak that current screen is never closed when defining it here; which is ok I think because we want to return to it anyways
                Screen currentScreen = MinecraftClient.getInstance().currentScreen;
                client.player.sendMessage(Text.of("You pressed the GUI key!"));
                // treeGui = new ScreenManager(Keeblarcraft.TREE_HANDLER.create(0, client.player.getInventory()), client.player.getInventory(), Text.of("Test"));
                // treeGui.AddParent(currentScreen); ///TODO: Put this in the constructor when you figure out how the hell the magic is happening with registration

                // ScreenManager treeGui = ScreenManager.GetInstance();
                // treeGui.AddParent(currentScreen);

                // MinecraftClient.getInstance().setScreen(treeGui);

                // toggleTreeGui intentionally never bit-flipped to false as a slight implementation bug exists such that pressing the keybind again
                // does NOT call this callback function until the previous screen is CLOSED (which is why this isn't resource leaking...). This will
                // need a slight rework or an extra subcall - but is fine for now since this is just how screens work.
            }
        }
    }
}