package dev.photosync.platform.impl;

import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.resources.Identifier;
import org.lwjgl.glfw.GLFW;

/**
 * The key that opens PhotoSync, bound to F6 until the player says otherwise.
 *
 * <p>F6 because it is unbound in vanilla and sits next to F2, which is the other
 * key this mod is about.
 */
public final class OpenKey {

    private final KeyMapping mapping;

    private OpenKey(KeyMapping mapping) {
        this.mapping = mapping;
    }

    /**
     * Registers the binding. Called once, from the client entrypoint.
     *
     * <p>1.21.11 turned the category from a loose translation key into a
     * registered id, and it derives its own label: {@code photosync:main}
     * becomes {@code key.category.photosync.main}, which is why the language
     * file carries that key alongside the older {@code key.categories.photosync}.
     *
     * <p>Fabric API followed the rename in 26.1: {@code fabric-key-binding-api-v1}
     * became {@code fabric-key-mapping-api-v1}, and its helper method with it.
     */
    public static OpenKey register() {
        KeyMapping.Category category =
                KeyMapping.Category.register(Identifier.fromNamespaceAndPath("photosync", "main"));
        return new OpenKey(KeyMappingHelper.registerKeyMapping(new KeyMapping(
                "key.photosync.open",
                InputConstants.Type.KEYSYM,
                GLFW.GLFW_KEY_F6,
                category)));
    }

    /** Takes one queued press, or false if there are none left. */
    public boolean wasPressed() {
        return mapping.consumeClick();
    }
}
