package speiger.src.coreengine.rendering.gui.components.base;

import java.util.function.Consumer;

public sealed interface IListableComponent permits GuiComponent {
	public static final int LISTENER_USER_ACTION = 0;
	public static final int LISTENER_ON_CHANGE = 1;
	public static final int LISTENER_CLOSED = 2;
	
	public static final int MAX_LISTENER_TYPES = 3;
	
	public GuiComponent addListener(Consumer<GuiComponent> listener, int index);
	public GuiComponent removeListener(Consumer<GuiComponent> listener, int index);
	public void notifyListeners(int index);
	
	public default GuiComponent onAction(Consumer<GuiComponent> listener) {
		return addListener(listener, IListableComponent.LISTENER_USER_ACTION);
	}
	
	public default GuiComponent onAction(Runnable listener) {
		return addListener(listener, IListableComponent.LISTENER_USER_ACTION);
	}
	
	public default GuiComponent onChange(Consumer<GuiComponent> listener) {
		return addListener(listener, IListableComponent.LISTENER_ON_CHANGE);
	}
	
	public default GuiComponent onChange(Runnable listener) {
		return addListener(listener, IListableComponent.LISTENER_ON_CHANGE);
	}
	
	public default GuiComponent onClose(Consumer<GuiComponent> listener) {
		return addListener(listener, IListableComponent.LISTENER_CLOSED);
	}
	
	public default GuiComponent onClose(Runnable listener) {
		return addListener(listener, IListableComponent.LISTENER_CLOSED);
	}
	
	public default GuiComponent addListener(Runnable runnable, int index) {
		return addListener(_ -> runnable.run(), index);
	}
	
	public default GuiComponent removeUserActionListener(Consumer<GuiComponent> listener) {
		return removeListener(listener, GuiComponent.LISTENER_USER_ACTION);
	}
	
	public default GuiComponent removeChangeListener(Consumer<GuiComponent> listener) {
		return removeListener(listener, GuiComponent.LISTENER_ON_CHANGE);
	}
	
	public default GuiComponent removeCloseListener(Consumer<GuiComponent> listener) {
		return removeListener(listener, GuiComponent.LISTENER_CLOSED);
	}
}