package net.halbear.Terrain4J.EngineCore.Input;

import org.joml.Vector2f;

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

public class MouseListener {
    private final Vector2f currentPos;
    private final Vector2f deltaPos;
    private final Vector2f previousPos;
    private boolean FocusWindow;
    private boolean leftButtonPressed;
    private boolean rightButtonPressed;
    private boolean middleButtonPressed;
    public MouseListener(long window) {
        previousPos = new Vector2f(-1,-1);
        currentPos = new Vector2f();
        deltaPos = new Vector2f();
        leftButtonPressed = false;
        rightButtonPressed = false;
        FocusWindow = false;
        glfwSetCursorPosCallback(window, (handle, xpos, ypos) -> {
            currentPos.x = (float) xpos;
            currentPos.y = (float) ypos;
        });
        glfwSetCursorEnterCallback(window, (handle, entered) -> FocusWindow = entered);
        glfwSetMouseButtonCallback(window, (handle, button, action, mode) -> {
            leftButtonPressed = button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS;
            rightButtonPressed = button == GLFW_MOUSE_BUTTON_2 && action == GLFW_PRESS;
            middleButtonPressed = button == GLFW_MOUSE_BUTTON_3 && action == GLFW_PRESS;
        });
    }
    public Vector2f getCurrentPos() {
        return currentPos;
    }
    public Vector2f getDeltaPos() {
        return deltaPos;
    }
    public void input() {
        deltaPos.x = 0;
        deltaPos.y = 0;
        if (previousPos.x >= 0 && previousPos.y >= 0 && FocusWindow) {
            deltaPos.x = currentPos.x - previousPos.x;
            deltaPos.y = currentPos.y - previousPos.y;
        }
        previousPos.x = currentPos.x;
        previousPos.y = currentPos.y;
    }
    public boolean isLeftButtonPressed() {
        return leftButtonPressed;
    }
    public boolean isRightButtonPressed() {
        return rightButtonPressed;
    }
    public boolean isMiddleButtonPressed() {
        return rightButtonPressed;
    }
}
