package org.hirw.game;

import static org.lwjgl.glfw.GLFW.*;

import java.util.HashMap;

public class Keyboard {
  private static Keyboard instance;
  private HashMap<Integer, Boolean> buttons;

  private Keyboard() {
    this.buttons = new HashMap<Integer, Boolean>();
  }

  public static Keyboard getInstance() {
    if (instance == null) {
      synchronized (Keyboard.class) {
        if (instance == null) {
          instance = new Keyboard();
        }
      }
    }
    return instance;
  }

  public static void keyCallback(long window, int keyCode, int _scan, int action, int _modifiers) {
    if (action == GLFW_PRESS) {
      getInstance().buttons.put(keyCode, true);
    } else if (action == GLFW_RELEASE) {
      getInstance().buttons.put(keyCode, false);
    }
  }

  public static boolean isPressed(int keyCode) {
    return getInstance().buttons.getOrDefault(keyCode, false);
  }
}
