package de.blight.game.config;

import com.jme3.input.KeyInput;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/** Bildet KeyInput-Codes auf lesbare Namen ab (via Reflection auf KeyInput-Konstanten). */
public final class KeyNames {

    private static final Map<Integer, String> NAMES = new HashMap<>();

    static {
        for (Field f : KeyInput.class.getFields()) {
            if (f.getName().startsWith("KEY_") && f.getType() == int.class) {
                try {
                    NAMES.put(f.getInt(null), pretty(f.getName().substring(4)));
                } catch (IllegalAccessException ignored) {}
            }
        }
    }

    public static String of(int keyCode) {
        return NAMES.getOrDefault(keyCode, "Key#" + keyCode);
    }

    private static String pretty(String raw) {
        // "LSHIFT" → "L-Shift", "SPACE" → "Space", "W" → "W"
        return switch (raw) {
            case "SPACE"   -> "Space";
            case "LSHIFT"  -> "L-Shift";
            case "RSHIFT"  -> "R-Shift";
            case "LCONTROL"-> "L-Ctrl";
            case "RCONTROL"-> "R-Ctrl";
            case "LMENU"   -> "L-Alt";
            case "RMENU"   -> "R-Alt";
            case "RETURN"  -> "Enter";
            case "BACK"    -> "Backspace";
            case "UP"      -> "Pfeil-Hoch";
            case "DOWN"    -> "Pfeil-Runter";
            case "LEFT"    -> "Pfeil-Links";
            case "RIGHT"   -> "Pfeil-Rechts";
            default        -> raw.length() == 1 ? raw : capitalize(raw);
        };
    }

    private static String capitalize(String s) {
        return s.isEmpty() ? s : s.charAt(0) + s.substring(1).toLowerCase();
    }
}
