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();
			String command = matcher.group(2);
			if (matcher.group(3) == null || matcher.group(3).isEmpty()) {
				return getName().matches(command);
			} else
				return getName().matches(command) && getParameterRegex().matches(matcher.group(3));

		}
		return false;
	}

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

	public abstract void processMessage(Privmsg privmsg);

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

@@ -294,20 +294,21 @@
 pile(%22(!
-(.+)
+%5B%5E %5D+
 )%7C(!(.+?
@@ -615,9 +615,41 @@
 oup(
-2
+3);%0A%09%09%09System.out.println(command
 );%0A%09
@@ -668,17 +668,17 @@
 r.group(
-3
+4
 ) == nul
@@ -696,17 +696,17 @@
 r.group(
-3
+4
 ).isEmpt
@@ -727,33 +727,33 @@
 urn 
-getName().matches(command
+command.matches(getName()
 );%0A%09
@@ -760,16 +760,18 @@
 %09%09%7D else
+ %7B
 %0A%09%09%09%09ret
@@ -778,85 +778,89 @@
 urn 
-getName().matches(command) && getParameterRegex().matches(matcher.group(3));%0A
+command.matches(getName()) && matcher.group(4).matches(getParameterRegex());%0A%09%09%09%7D
 %0A%09%09%7D

fix speed derp v2