package speiger.src.coreengine.rendering.input.window;

import org.lwjgl.system.Callback;
import org.lwjgl.system.CallbackI;

public class WindowCallback {
	ReloadFunction<CallbackI> function;
	CallbackI listener;
	Callback callback;
	
	public WindowCallback(CallbackI listener, ReloadFunction<CallbackI> function) {
		this.function = function;
		this.listener = listener;
	}

	public void reload(long windowId) {
		destroy();
		load(windowId);
	}
	
	public void load(long windowId) {
		callback = function.applyListener(windowId, listener);
	}
	
	public void destroy() {
		if(callback == null) return;
		callback.free();
		callback = null;
	}
	
	public static interface ReloadFunction<T extends CallbackI> {
		Callback applyListener(long window, T listener);
		
	}
	public static interface SimpleReloadFunction<T extends CallbackI> extends ReloadFunction<T> {
		Callback applyListener(T listener);
		@Override
		default Callback applyListener(long window, T listener) { return applyListener(listener); }
	}
}
