package me.hammerle.supersnuvi.javafx;

import java.util.EnumMap;
import javafx.scene.input.KeyCode;
import me.hammerle.supersnuvi.gamelogic.StateRenderer;

public class KeyHandler
{
    private KeyHandler()
    {
    }
    
    private final static EnumMap<KeyCode, KeyBinding> BINDINGS = new EnumMap(KeyCode.class);
    
    private static KeyBinding registerBinding(String name, KeyCode error)
    {
        String s = StateRenderer.CONFIG.getString(name);
        KeyCode code;
        if(s == null)
        {
            code = error;
        }
        else
        {
            code = KeyCode.getKeyCode(s);
            if(code == null)
            {
                System.out.println("cant find key '" + s + "'");
                code = error;
            }
        }
        KeyBinding binding = new KeyBinding(name, code);
        BINDINGS.put(code, binding);
        return binding;
    }
    
    public final static IKeyBinding UP = registerBinding("key.up", KeyCode.UP);
    public final static IKeyBinding DOWN = registerBinding("key.down", KeyCode.DOWN);
    public final static IKeyBinding LEFT = registerBinding("key.left", KeyCode.LEFT);
    public final static IKeyBinding RIGHT = registerBinding("key.right", KeyCode.RIGHT);
    public final static IKeyBinding JUMP = registerBinding("key.jump", KeyCode.SPACE);
    public final static IKeyBinding RUN = registerBinding("key.run", KeyCode.SHIFT);
    public final static IKeyBinding ESCAPE = registerBinding("key.escape", KeyCode.ESCAPE);
    public final static IKeyBinding ENTER = registerBinding("key.enter", KeyCode.ENTER);
    
    public final static IKeyBinding COMBAT = registerBinding("key.combat", KeyCode.A);
    public final static IKeyBinding COMBAT_SWITCH_FACE = registerBinding("key.combat.switchface", KeyCode.S);
    public final static IKeyBinding COMBAT_DASH = registerBinding("key.combat.dash", KeyCode.D);
    public final static IKeyBinding COMBAT_DODGE = registerBinding("key.combat.dodge", KeyCode.F);
    public final static IKeyBinding COMBAT_BLOCK = registerBinding("key.combat.block", KeyCode.G);
    public final static IKeyBinding COMBAT_ATTACK = registerBinding("key.combat.attack", KeyCode.H);
    
    public final static IKeyBinding[] ARRAY = new IKeyBinding[] 
    {
        UP, DOWN, LEFT,  RIGHT, JUMP, RUN, ESCAPE, ENTER, COMBAT, COMBAT_SWITCH_FACE, COMBAT_DASH, COMBAT_DODGE, COMBAT_BLOCK, COMBAT_ATTACK
    };
    
    private static KeyBinding rebind = null;
    
    public static void rebindKey(IKeyBinding binding)
    {
        rebind = (KeyBinding) binding;
        rebind.setEditStatus(true);
    }
           
    protected static void pressKey(KeyCode key) 
    {
        if(rebind == null)
        {
            KeyBinding binding = BINDINGS.get(key);
            if(binding != null)
            {
                binding.press();
            }
        }
        else
        {
            for(IKeyBinding b : ARRAY)
            {
                if(((KeyBinding) b).getKeyCode() == key)
                {
                    rebind.setEditStatus(false);
                    rebind = null;
                    return;
                }
            }
                
            BINDINGS.remove(rebind.getKeyCode());
            rebind.setKeyCode(key);
            BINDINGS.put(key, rebind);
            rebind.setEditStatus(false);
            rebind = null;
        }
    }

    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());
    }
}

