/*
 * Gimbal
 * Copyright (C) 2024 moonleay
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package net.moonleay.gimbal.client.keybindings

import net.minecraft.client.option.KeyBinding
import net.minecraft.client.util.InputUtil
import net.moonleay.gimbal.build.BuildConstants
import net.moonleay.gimbal.client.keybindings.impl.editormode.EnableInsertModeShortcut
import net.moonleay.gimbal.client.keybindings.impl.editormode.EnableReplaceModeShortcut
import net.moonleay.gimbal.client.keybindings.impl.editormodemodifier.ToggleBulldozerModifierShortcut
import net.moonleay.gimbal.client.keybindings.impl.editormodemodifier.ToggleForcePlaceModifierShortcut
import net.moonleay.gimbal.client.keybindings.impl.editormodemodifier.ToggleNoClipModifierShortcut
import net.moonleay.gimbal.client.keybindings.impl.editormodemodifier.ToggleNoUpdatesModifierShortcut
import net.moonleay.gimbal.client.keybindings.impl.gamemode.CreativeModeShortcut
import net.moonleay.gimbal.client.keybindings.impl.gamemode.SpectatorModeShortcut
import net.moonleay.gimbal.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(
            "gimbal.key.editor.modifier.bulldozer",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimbal.category.editormodifier"
        )
        val toggleForcePlaceModifierShortcut = KeyBinding(
            "gimbal.key.editor.modifier.forceplace",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimbal.category.editormodifier"
        )
        val toggleNoUpdatesModifierShortcut = KeyBinding(
            "gimbal.key.editor.modifier.noupdates",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimbal.category.editormodifier"
        )
        val toggleNoClipModifierShortcut = KeyBinding(
            "gimbal.key.editor.modifier.noclip",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_UNKNOWN,
            "gimbal.category.editormodifier"
        )

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

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

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