package com.avairebot.orion.middleware;

import com.avairebot.orion.Orion;
import com.avairebot.orion.contracts.middleware.AbstractMiddleware;
import net.dv8tion.jda.core.entities.Message;

import java.util.Arrays;
import java.util.regex.Pattern;

public class ProcessCommand extends AbstractMiddleware {

    private final static String COMMAND_OUTPUT = "Executing Command \"%command%\" in \"%category%\" category:"
            + "\n\t\tUser:\t %author%"
            + "\n\t\tServer:\t %server%"
            + "\n\t\tChannel: %channel%"
            + "\n\t\tMessage: %message%";

    private final Pattern argumentsRegEX;

    public ProcessCommand(Orion orion) {
        super(orion);

        this.argumentsRegEX = Pattern.compile("[\\s\"]+|\"([^\"]*)\"", Pattern.MULTILINE);
    }

    @Override
    public boolean handle(Message message, MiddlewareStack stack, String... args) {
        String[] arguments = argumentsRegEX.split(message.getContent());

        orion.logger.info(COMMAND_OUTPUT
                .replace("%command%", stack.getCommand().getName())
                .replace("%category%", stack.getCommandContainer().getCategory().getName())
                .replace("%author%", generateUsername(message))
                .replace("%server%", generateServer(message))
                .replace("%channel%", generateChannel(message))
                .replace("%message%", message.getRawContent())
        );

        return stack.getCommand().onCommand(message, Arrays.copyOfRange(arguments, 1, arguments.length));
    }

    private String generateUsername(Message message) {
        return String.format("%s#%s [%s]",
                message.getAuthor().getName(),
                message.getAuthor().getDiscriminator(),
                message.getAuthor().getId()
        );
    }

    private String generateServer(Message message) {
        if (!message.getChannelType().isGuild()) {
            return "PRIVATE";
        }

        return String.format("%s [%s]",
                message.getGuild().getName(),
                message.getGuild().getId()
        );
    }

    private CharSequence generateChannel(Message message) {
        if (!message.getChannelType().isGuild()) {
            return "PRIVATE";
        }

        return String.format("%s [%s]",
                message.getChannel().getName(),
                message.getChannel().getId()
        );
    }
}

@@ -205,17 +205,100 @@
 il.Array
-s
+List;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0Aimport java.util.regex.Matcher
 ;%0Aimport
@@ -815,29 +815,31 @@
 le(%22
-%5B%5C%5Cs%5C%22%5D+%7C%5C%22(%5B%5E%5C%22%5D*)%5C%22
+(%5B%5E%5C%22%5D%5C%5CS*%7C%5C%22.+?%5C%22)%5C%5Cs*
 %22, P
@@ -995,49 +995,40 @@
 s = 
-argumentsRegEX.split(message.getContent()
+generateCommandArguments(message
 );%0A%0A
@@ -1489,24 +1489,24 @@
         );%0A%0A
-
         retu
@@ -1598,32 +1598,437 @@
 ength));%0A    %7D%0A%0A
+    private String%5B%5D generateCommandArguments(Message message) %7B%0A        List%3CString%3E arguments = new ArrayList%3C%3E();%0A%0A        Matcher matcher = argumentsRegEX.matcher(message.getContent());%0A        while (matcher.find()) %7B%0A            arguments.add(matcher.group(0)%0A                    .replaceAll(%22%5C%22%22, %22%22)%0A                    .trim());%0A        %7D%0A%0A        return arguments.toArray(new String%5B0%5D);%0A    %7D%0A%0A
     private Stri

Fix process command spliting command arguments correctly