package electrosphere.controls;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWKeyCallbackI;

import electrosphere.logger.LoggerInterface;

public class ControlCallback implements GLFWKeyCallbackI {

    static final short KEY_VALUE_ARRAY_SIZE = 512;

    boolean[] keyValues = new boolean[KEY_VALUE_ARRAY_SIZE];

    @Override
    public void invoke(long window, int key, int scancode, int action, int mods) {
        
        if(key >= 0 && key < KEY_VALUE_ARRAY_SIZE){
            if(action == GLFW.GLFW_PRESS || action == GLFW.GLFW_REPEAT){
                keyValues[key] = true;
            } else {
                keyValues[key] = false;
            }
        }
        // if(key == GLFW.GLFW_KEY_D){
        //     System.out.println("[D]Action: " + action + " keyValues: " + keyValues[key]);
        // }
        // if(key == GLFW.GLFW_KEY_W){
        //     System.out.println("[W]Action: " + action + " keyValues: " + keyValues[key]);
        // }
    }

    /**
     * !!!WARNING!!!, will silently fail if opengl elementsn ot defined or keycode is outside of key value array size or is not in main rendering thread
     * @param keycode
     * @return
     */
    public boolean getKey(int keycode){
        if(keycode >= 0 && keycode < KEY_VALUE_ARRAY_SIZE){
            return keyValues[keycode];
        } else {
            LoggerInterface.loggerEngine.WARNING("Trying to get key state where keycode is undefined (<0  or >400)");
        }
        return false;
    }

}
