package speiger.src.coreengine.rendering.inputOld;

import org.lwjgl.glfw.GLFW;

import speiger.src.collections.ints.sets.IntLinkedOpenHashSet;
import speiger.src.collections.ints.sets.IntSet;
import speiger.src.coreengine.math.vector.ints.Vec2i;
import speiger.src.coreengine.rendering.inputOld.bindings.InputBinding;
import speiger.src.coreengine.rendering.inputOld.bindings.utils.BindingType;
import speiger.src.coreengine.rendering.inputOld.camera.Camera;
import speiger.src.coreengine.rendering.inputOld.events.MouseEvent;
import speiger.src.coreengine.rendering.inputOld.events.MouseEvent.MouseClickEvent;
import speiger.src.coreengine.rendering.inputOld.events.MouseEvent.MouseMoveEvent;
import speiger.src.coreengine.rendering.inputOld.events.MouseEvent.MouseScrollEvent;
import speiger.src.coreengine.rendering.inputOld.window.Window;
import speiger.src.coreengine.utils.eventbus.EventBus;

public class Mouse
{
	public static final Mouse INSTANCE = new Mouse();
	
	MouseRay ray;
	EventBus bus;
	IntSet pressedButton = new IntLinkedOpenHashSet();
	Vec2i position = Vec2i.mutable();
	Vec2i movement = Vec2i.mutable();
	Vec2i scroll = Vec2i.mutable();
	boolean active = true;
	
	public void init(EventBus bus, Window window, Camera cam)
	{
		this.bus = bus;
		ray = new MouseRay(cam, window);
		initSubWindow(window);
	}
	
	public void initSubWindow(Window window) {
		window.addCallback(T -> GLFW.glfwSetCursorPosCallback(T, Mouse.this::onDrag));
		window.addCallback(T -> GLFW.glfwSetMouseButtonCallback(T, Mouse.this::onClick));
		window.addCallback(T -> GLFW.glfwSetScrollCallback(T, Mouse.this::onScroll));
		window.addCallback(T -> GLFW.glfwSetCursorEnterCallback(T, (_, V) -> active = V));		
	}
	
	protected void onDrag(long window, double xpos, double ypos) {
		int xChange = (int)xpos - position.x();
		int yChange = (int)ypos - position.y();
		position.set((int)xpos, (int)ypos);
		pushEvent(new MouseMoveEvent(window, position.x(), position.y(), xChange, yChange));
	}
	
	protected void onScroll(long window, double xScroll, double yScroll)
	{
		pushEvent(new MouseScrollEvent(window, position.x(), position.y(), (int)xScroll, (int)yScroll));
	}
	
	protected void onClick(long window, int button, int action, int mods)
	{
		if(action == 1) pressedButton.add(button);
		else pressedButton.remove(button);
		pushEvent(new MouseClickEvent(window, position.x(), position.y(), button, action == 1));
	}
	
	public static void update()
	{
		INSTANCE.scroll.negate();
		INSTANCE.movement.negate();
		INSTANCE.ray.update();
	}
	
	public static boolean isButtonPressed(int button)
	{
		return INSTANCE.pressedButton.contains(button);
	}
	
	public static int getX()
	{
		return INSTANCE.position.x();
	}
	
	public static int getY()
	{
		return INSTANCE.position.y();
	}
	
	public static Vec2i getMovement()
	{
		return INSTANCE.movement;
	}
	
	public static boolean isActive()
	{
		return INSTANCE.active;
	}
	
	public static MouseRay getRayCast()
	{
		return INSTANCE.ray;
	}
	
	public Vec2i getScroll()
	{
		return scroll;
	}
	
	public void pushEvent(MouseEvent event)
	{
		if(active)
		{
			bus.post(event);
			if(!event.isCanceled() || event.isForced())
			{
				if(event instanceof MouseMoveEvent)
				{
					MouseMoveEvent move = (MouseMoveEvent)event;
					movement.add(move.getXMove(), move.getYMove());
				}
				else if(event instanceof MouseClickEvent)
				{
					MouseClickEvent click = (MouseClickEvent)event;
					InputBinding.BINDINGS.updatePressing(BindingType.MOUSE, click.getButton(), click.isPress());
				}
				else if(event instanceof MouseScrollEvent)
				{
					MouseScrollEvent scroll = (MouseScrollEvent)event;
					this.scroll.add(scroll.getScrollX(), scroll.getScrollY());
				}
			}
		}
	}
}
