package me.km.networking;

import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.lwjgl.glfw.GLFW;

@OnlyIn(Dist.CLIENT)
public class KeyManager {
    private final KeyBinding[] functionKeys;

    public KeyManager() {
        functionKeys = new KeyBinding[9];
        functionKeys[0] = register(1, GLFW.GLFW_KEY_G);
        functionKeys[1] = register(2, GLFW.GLFW_KEY_H);
        functionKeys[2] = register(3, GLFW.GLFW_KEY_J);
        functionKeys[3] = register(4, GLFW.GLFW_KEY_K);
        functionKeys[4] = register(5, GLFW.GLFW_KEY_V);
        functionKeys[5] = register(6, GLFW.GLFW_KEY_B);
        functionKeys[6] = register(7, GLFW.GLFW_KEY_N);
        functionKeys[7] = register(8, GLFW.GLFW_KEY_M);
        functionKeys[8] = register(9, GLFW.GLFW_KEY_COMMA);
    }

    public String getKeyDescription(int index) {
        return functionKeys[index].getLocalizedName();
    }

    private KeyBinding register(int id, int key) {
        KeyBinding keyBind = new KeyBinding("key.function." + id, key, "key.km.function");
        ClientRegistry.registerKeyBinding(keyBind);
        return keyBind;
    }

    @SubscribeEvent
    public void onRenderGui(TickEvent.ClientTickEvent e) {
        for(int i = 0; i < functionKeys.length; i++) {
            if(functionKeys[i].isPressed()) {
                ModPacketHandler.sendFunctionKey(i + 1);
            }
        }
    }
}
