package speiger.src.coreengine.rendering.input;

import java.io.File;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWDropCallback;

import speiger.src.coreengine.rendering.input.events.FileDropEvent;
import speiger.src.coreengine.rendering.input.window.ScaledResolution;
import speiger.src.coreengine.rendering.input.window.Window;
import speiger.src.coreengine.utils.eventbus.EventBus;

public class DropListener
{
	EventBus bus;
	ScaledResolution resolution;
	
	public DropListener(Window window, EventBus bus)
	{
		window.addCallback(T -> GLFW.glfwSetDropCallback(T, this::onFileDropped));
		resolution = window.getUIFrame();
		this.bus = bus;
	}
	
	public void onFileDropped(long windowId, int count, long names)
	{
		int x = Mouse.getX();
		int y = Mouse.getY();
		for(int i = 0;i<count;i++)
		{
			File file = new File(GLFWDropCallback.getName(names, i));
			if(file.exists())
			{
				bus.post(new FileDropEvent(file, x, y, resolution));
			}
		}
	}
}
