package io.github.likcoras.asuka;

import java.util.List;

import org.pircbotx.PircBotX;
import org.pircbotx.hooks.Event;
import org.pircbotx.hooks.Listener;
import org.pircbotx.hooks.types.GenericChannelEvent;
import org.pircbotx.hooks.types.GenericUserEvent;

import com.google.common.collect.ImmutableList;

import io.github.likcoras.asuka.exception.ConfigException;
import io.github.likcoras.asuka.exception.HandlerException;
import io.github.likcoras.asuka.exception.PermissionException;
import io.github.likcoras.asuka.handler.AuthManageHandler;
import io.github.likcoras.asuka.handler.BatotoHandler;
import io.github.likcoras.asuka.handler.Handler;
import io.github.likcoras.asuka.handler.IgnoreManageHandler;
import io.github.likcoras.asuka.handler.MangaUpdatesHandler;
import io.github.likcoras.asuka.handler.QuitHandler;
import io.github.likcoras.asuka.handler.SilentSkyRSSHandler;
import io.github.likcoras.asuka.handler.SilentSkyXMLHandler;
import io.github.likcoras.asuka.handler.UptimeHandler;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;

@Log4j2
public class HandlerManager implements Listener<PircBotX> {

	private AsukaBot bot;
	private List<Handler> handlers;

	public HandlerManager(@NonNull AsukaBot bot) {
		this.bot = bot;
		addHandlers();
	}

	public void configHandlers(@NonNull BotConfig config) throws ConfigException {
		for (Handler handler : handlers)
			handler.configure(config);
	}

	private void addHandlers() {
		handlers = ImmutableList.<Handler> builder()
				.add(new AuthManageHandler())
				.add(new IgnoreManageHandler())
				.add(new QuitHandler())
				.add(new UptimeHandler())
				.add(new BatotoHandler())
				.add(new MangaUpdatesHandler())
				.add(new SilentSkyXMLHandler())
				.add(new SilentSkyRSSHandler())
				.build();
	}

	@Override
	public void onEvent(Event<PircBotX> event) {
		if (!shouldIgnore(event))
			handle(event);
	}

	@SuppressWarnings("unchecked")
	private boolean shouldIgnore(Event<PircBotX> event) {
		return !(event instanceof GenericUserEvent)
				|| !bot.getIgnoreManager().isIgnored(((GenericUserEvent<PircBotX>) event).getUser());
	}

	private void handle(Event<PircBotX> event) {
		for (Handler handler : handlers)
			try {
				handler.handle(bot, event).send(bot);
			} catch (PermissionException e) {
				log.warn("User " + BotUtil.getId(e.getUser()) + " was denied access for query " + e.getQuery());
				e.getUser().send().notice("You are not allowed to execute query " + e.getQuery());
			} catch (HandlerException e) {
				if (event instanceof GenericChannelEvent || event instanceof GenericUserEvent)
					event.respond("Error while handling " + event.getClass().getSimpleName() + " with handler "
							+ handler.getClass().getSimpleName() + ": " + e.getCause().toString());
				log.warn("Exception caught while handling " + event.getClass().getSimpleName() + " with handler "
						+ handler.getClass().getSimpleName() + ": ", e);
			}
	}

}

@@ -82,16 +82,42 @@
 rcBotX;%0A
+import org.pircbotx.User;%0A
 import o
@@ -2009,15 +2009,12 @@
 %7B%0A%09%09
-return 
+if (
 !(ev
@@ -2049,50 +2049,41 @@
 ent)
+)
 %0A%09%09%09
-%09%7C%7C !bot.getIgnoreManager().isIgnored(
+return false;%0A%09%09User user = 
 ((Ge
@@ -2124,16 +2124,80 @@
 etUser()
+;%0A%09%09return user != null && bot.getIgnoreManager().isIgnored(user
 );%0A%09%7D%0A%0A%09

Fix ignore checking and null checking