package net.halbear.Terrain4J.EngineCore.Input;

import org.lwjgl.glfw.GLFWCharCallbackI;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWKeyCallbackI;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class KeyboardInput {

    private long window;
    public List<Integer> keysPressed = new ArrayList<>();
    public List<Integer> keysSinglePressed = new ArrayList<>();

    public KeyboardInput(long Window){
        window = Window;
        glfwSetKeyCallback(window, this::KeyCallback);
    }
    public void input() {
        glfwPollEvents();
    }
    public void KeyCallback(long Window, int key, int scancode, int action, int mods){
        if (!keysPressed.contains(key) && action == GLFW_PRESS){
            keysPressed.add(key);
        } else if (keysPressed.contains(key) && action == GLFW_RELEASE){
            if (keysPressed.indexOf(key) >= 0 && keysPressed.indexOf(key) <keysPressed.size()){
            keysPressed.remove(keysPressed.indexOf(key));
            }
        }
        if (keysSinglePressed.contains(key)&& action == GLFW_RELEASE) keysSinglePressed.remove(keysSinglePressed.indexOf(key));
    }
    public boolean keyPressed(int keyCode) {
        if (keysPressed.contains(keyCode)) return true;
        else return false;
    }

    public boolean keySinglePress(int keyCode) {
        if (keysPressed.contains(keyCode)) {
            if (keysSinglePressed.contains(keyCode)) return false;
            else {
                keysSinglePressed.add(keyCode);
                return true;
            }
        }
        else {
            return false;
        }
    }

    public void setCharCallBack(GLFWCharCallbackI charCallback) {
        glfwSetCharCallback(window, charCallback);
    }
}
