package speiger.src.coreengine.core.events.bus;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;

import speiger.src.coreengine.core.events.api.Event;
import speiger.src.coreengine.core.events.bus.EventBus.ClassFilter;
import speiger.src.coreengine.core.events.utility.IEventDispatcher;
import speiger.src.coreengine.core.events.utility.IEventExceptionHandler;

public class BusBuilder {
	IEventDispatcher dispatcher = IEventDispatcher.DIRECT;
	Optional<IEventExceptionHandler> exceptions = Optional.empty();
	ClassFilter typeCheck = new ClassFilter(false, _ -> true, "");
	boolean running = true;
	
	public BusBuilder dispatcher(IEventDispatcher dispatcher) {
		this.dispatcher = Objects.requireNonNull(dispatcher, "A Dispatcher is required");
		return this;
	}
	
	public BusBuilder exceptions(IEventExceptionHandler exceptions) {
		this.exceptions = Optional.ofNullable(exceptions);
		return this;
	}
	
	public BusBuilder filter(Predicate<Class<? extends Event>> filter, String filterMessage) {
		this.typeCheck = new ClassFilter(true, Objects.requireNonNull(filter, "A Filter is required"), Objects.requireNonNull(filterMessage, "A Filter Message is required"));
		return this;
	}
	
	public BusBuilder beginShutdown() {
		running = false;
		return this;
	}
	
	public EventBus build() {
		return new EventBus(this);
	}
}
