package me.hammerle.supersnuvi.javafx;

import java.util.EnumMap;
import javafx.scene.input.KeyCode;

public class KeyHandler
{
    private KeyHandler()
    {
    }
    
    private final static EnumMap<KeyCode, KeyBinding> BINDINGS = new EnumMap(KeyCode.class);
    
    private static KeyBinding registerBinding(KeyCode key)
    {
        KeyBinding binding = new KeyBinding();
        BINDINGS.put(key, binding);
        return binding;
    }
    
    public final static IKeyBinding UP = registerBinding(KeyCode.UP);
    public final static IKeyBinding DOWN = registerBinding(KeyCode.DOWN);
    public final static IKeyBinding LEFT = registerBinding(KeyCode.LEFT);
    public final static IKeyBinding RIGHT = registerBinding(KeyCode.RIGHT);
    public final static IKeyBinding JUMP = registerBinding(KeyCode.SPACE);
    public final static IKeyBinding RUN = registerBinding(KeyCode.SHIFT);
    public final static IKeyBinding ESCAPE = registerBinding(KeyCode.ESCAPE);
    public final static IKeyBinding ENTER = registerBinding(KeyCode.ENTER);
           
    protected static void pressKey(KeyCode key) 
    {
        KeyBinding binding = BINDINGS.get(key);
        if(binding != null)
        {
            binding.press();
        }
    }

    protected static void releaseKey(KeyCode key) 
    {
        KeyBinding binding = BINDINGS.get(key);
        if(binding != null)
        {
            binding.release();
        }
    }
    
    protected static void tick()
    {
        BINDINGS.values().forEach(binding -> binding.tick());
    }
}

