package net.moonleay.gimble.client.keybindings

import net.minecraft.client.option.KeyBinding
import net.minecraft.client.util.InputUtil
import net.moonleay.gimble.build.BuildConstants
import net.moonleay.gimble.client.keybindings.impl.editormode.EnableInsertModeShortcut
import net.moonleay.gimble.client.keybindings.impl.editormode.EnableReplaceModeShortcut
import net.moonleay.gimble.client.keybindings.impl.editormodemodifier.ToggleBulldozerModifierShortcut
import net.moonleay.gimble.client.keybindings.impl.editormodemodifier.ToggleForcePlaceModifierShortcut
import net.moonleay.gimble.client.keybindings.impl.editormodemodifier.ToggleNoClipModifierShortcut
import net.moonleay.gimble.client.keybindings.impl.editormodemodifier.ToggleNoUpdatesModifierShortcut
import net.moonleay.gimble.client.keybindings.impl.gamemode.CreativeModeShortcut
import net.moonleay.gimble.client.keybindings.impl.gamemode.SpectatorModeShortcut
import net.moonleay.gimble.client.keybindings.impl.gamemode.SurvivalModeShortcut
import org.apache.logging.log4j.LogManager
import org.lwjgl.glfw.GLFW

object KeybindingRegistrar {

    private val LOGGER = LogManager.getLogger(BuildConstants.modName)

    fun registerKeybindings() {
        LOGGER.info("Registering keybindings...")
        registerSetGameModeKeybindings()
        registerSetEditorModeKeybindings()
        registerSetEditorModeModifierKeybindings()
        LOGGER.info("Keybindings have been registered.")
    }

    private fun registerSetEditorModeModifierKeybindings() {
        val toggleBulldozerModifierShortcut = KeyBinding(
            "gimble.key.editor.modifier.bulldozer",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimble.category.editormodifier"
        )
        val toggleForcePlaceModifierShortcut = KeyBinding(
            "gimble.key.editor.modifier.forceplace",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimble.category.editormodifier"
        )
        val toggleNoUpdatesModifierShortcut = KeyBinding(
            "gimble.key.editor.modifier.noupdates",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimble.category.editormodifier"
        )
        val toggleNoClipModifierShortcut = KeyBinding(
            "gimble.key.editor.modifier.noclip",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimble.category.editormodifier"
        )

        KeybindingManager.registerShortcut(ToggleBulldozerModifierShortcut(toggleBulldozerModifierShortcut))
        KeybindingManager.registerShortcut(ToggleForcePlaceModifierShortcut(toggleForcePlaceModifierShortcut))
        KeybindingManager.registerShortcut(ToggleNoUpdatesModifierShortcut(toggleNoUpdatesModifierShortcut))
        KeybindingManager.registerShortcut(ToggleNoClipModifierShortcut(toggleNoClipModifierShortcut))
    }

    private fun registerSetEditorModeKeybindings() {
        val insertKeyBinding = KeyBinding(
            "gimble.key.editor.mode.insert",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_I,
            "gimble.category.editormode"
        )
        val replaceKeyBinding = KeyBinding(
            "gimble.key.editor.mode.replace",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_R,
            "gimble.category.editormode"
        )
//        val visualKeyBinding = KeyBinding(
//            "gimble.key.editor.mode.visual",
//            InputUtil.Type.KEYSYM,
//            GLFW.GLFW_KEY_V,
//            "gimble.category.editormode"
//        )
        KeybindingManager.registerShortcut(EnableInsertModeShortcut(insertKeyBinding))
        KeybindingManager.registerShortcut(EnableReplaceModeShortcut(replaceKeyBinding))
//        KeybindingManager.registerShortcut(EnableVisualModeShortcut(visualKeyBinding))
    }

    private fun registerSetGameModeKeybindings() {
        val survivalKeyBinding = KeyBinding(
            "gimble.key.game.mode.survival",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimble.category.gamemode"
        )
        val creativeKeyBinding = KeyBinding(
            "gimble.key.game.mode.creative",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimble.category.gamemode"
        )
        val spectatorKeyBinding = KeyBinding(
            "gimble.key.game.mode.spectator",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimble.category.gamemode"
        )
        KeybindingManager.registerShortcut(SurvivalModeShortcut(survivalKeyBinding))
        KeybindingManager.registerShortcut(CreativeModeShortcut(creativeKeyBinding))
        KeybindingManager.registerShortcut(SpectatorModeShortcut(spectatorKeyBinding))
    }
}
