package org.freecode.irc.votebot.api;

import org.freecode.irc.Privmsg;
import org.freecode.irc.Transmittable;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class CommandModule extends FVBModule {

	private static final Pattern COMMAND_PATTERN = Pattern.compile("(!(.+))|(!(.+?) (.+))");

	@Override
	public boolean canRun(Transmittable trns) {
		if (!trns.isPrivmsg())
			return false;
		String msg = trns.asPrivmsg().getMessage();
		Matcher matcher = COMMAND_PATTERN.matcher(msg);
		if (matcher.matches()) {
			int count = matcher.groupCount();
			if (count == 3) {
				String command = matcher.group(2);
				return getName().matches(command);
			} else if (count == 4) {
				String command = matcher.group(2);
				return getName().matches(command) && getParameterRegex().matches(matcher.group(3));
			} else {
				System.err.println("Oops CommandModule, group count: " + count);
			}
		}
		return false;
	}

	@Override
	public void process(Transmittable trns) {
		processMessage((Privmsg) trns);
	}

	public abstract void processMessage(Privmsg privmsg);

	protected String getParameterRegex() {
		return ".*";
	}
}

@@ -583,170 +583,156 @@
 %0A%09%09%09
-if (count == 3) %7B%0A%09%09%09%09String command = matcher.group(2);%0A%09%09%09%09return getName().matches(command);%0A%09%09%09%7D else if (count == 4) %7B%0A%09%09%09%09String command = matcher.group(2);
+String command = matcher.group(2);%0A%09%09%09if (matcher.group(3) == null %7C%7C matcher.group(3).isEmpty()) %7B%0A%09%09%09%09return getName().matches(command);%0A%09%09%09%7D else
 %0A%09%09%09
@@ -820,93 +820,8 @@
 ));%0A
-%09%09%09%7D else %7B%0A%09%09%09%09System.err.println(%22Oops CommandModule, group count: %22 + count);%0A%09%09%09%7D
 %0A%09%09%7D

fix speed derp