/* © SRSoftware 2025 */
package de.srsoftware.umbrella.messagebus;

import de.srsoftware.umbrella.messagebus.events.Event;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MessageBus {
	private static final MessageBus SINGLETON = new MessageBus();
	private final Set<EventListener> listeners = new HashSet<>();

	private MessageBus(){}

	public void dispatch(Event<?> event){
		new Thread(() -> { // TODO: use thread pool
			try {
				Thread.sleep(100);
				List.copyOf(listeners).parallelStream().forEach(l -> l.onEvent(event));
			} catch (InterruptedException ignored) {
			}
		}).start();
	}

	public void drop(EventListener listener){
		listeners.remove(listener);
	}

	public static MessageBus messageBus(){
		return SINGLETON;
	}

	public void register(EventListener listener){
		listeners.add(listener);
	}
}