package org.monospark.actioncontrol.rule.response;

import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.Event;

public abstract class ActionResponseType {

    public static final Set<ActionResponseType> ALL_TYPES = createAllResponseTypes();

    private static Set<ActionResponseType> createAllResponseTypes() {
        Set<ActionResponseType> allTypes = new HashSet<ActionResponseType>();
        allTypes.add(new ActionResponseType() {

            @Override
            public Optional<ActionResponse> parse(String string) {
                return string.equalsIgnoreCase("deny") ? Optional.of(new ActionResponse() {
                    @Override
                    public <E extends Event & Cancellable> void execute(E event) {
                        event.setCancelled(true);
                    }
                }) : Optional.empty();
            }
        });

        allTypes.add(new ActionResponseType() {

            private Pattern pattern = Pattern.compile("command\\((?<command>.+)\\)");

            @Override
            public Optional<ActionResponse> parse(String string) {
                Matcher matcher = pattern.matcher(string);
                if (!matcher.matches()) {
                    return Optional.empty();
                }

                String command = matcher.group("command");
                return Optional.of(new ActionResponse() {
                    @Override
                    public <E extends Event & Cancellable> void execute(E event) {
                        Sponge.getGame().getCommandManager().process(event.getCause().first(Player.class).get(),
                                command);
                    }
                });
            }
        });

        allTypes.add(new ActionResponseType() {

            private Pattern pattern = Pattern.compile("log\\((?<message>.+)\\)");

            @Override
            public Optional<ActionResponse> parse(String string) {
                Matcher matcher = pattern.matcher(string);
                if (!matcher.matches()) {
                    return Optional.empty();
                }

                String message = matcher.group("message");
                return Optional.of(new ActionResponse() {
                    @Override
                    public <E extends Event & Cancellable> void execute(E event) {
                        System.out.println(message);
                    }
                });
            }
        });

        return allTypes;
    }

    public abstract Optional<ActionResponse> parse(String string);
}

@@ -1513,32 +1513,945 @@
              %7D%0A%0A
+                String command = matcher.group(%22command%22);%0A                return Optional.of(new ActionResponse() %7B%0A                    @Override%0A                    public %3CE extends Event & Cancellable%3E void execute(E event) %7B%0A                        Sponge.getGame().getCommandManager().process(Sponge.getServer().getConsole(),%0A                                command.replace(%22%3Cplayer%3E%22, event.getCause().first(Player.class).get().getName()));%0A                    %7D%0A                %7D);%0A            %7D%0A        %7D);%0A%0A        allTypes.add(new ActionResponseType() %7B%0A%0A            private Pattern pattern = Pattern.compile(%22playerCommand%5C%5C((?%3Ccommand%3E.+)%5C%5C)%22);%0A%0A            @Override%0A            public Optional%3CActionResponse%3E parse(String string) %7B%0A                Matcher matcher = pattern.matcher(string);%0A                if (!matcher.matches()) %7B%0A                    return Optional.empty();%0A                %7D%0A%0A
                 

Add more command action responses