package com.avairebot.orion.commands.fun;

import com.avairebot.orion.Orion;
import com.avairebot.orion.contracts.commands.Command;
import com.avairebot.orion.factories.MessageFactory;
import com.avairebot.orion.factories.RequestFactory;
import com.avairebot.orion.requests.Response;
import com.avairebot.orion.requests.service.ChuckNorrisService;
import net.dv8tion.jda.core.entities.Message;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public class ChuckNorrisCommand extends Command {

    public ChuckNorrisCommand(Orion orion) {
        super(orion);
    }

    @Override
    public String getName() {
        return "Chuck Norris Command";
    }

    @Override
    public String getDescription() {
        return "I will get a random 100% true, real facts about Chuck Norris for you using the \"Internet Chuck Norris Database\".";
    }

    @Override
    public List<String> getUsageInstructions() {
        return Collections.singletonList("`:command` - Gets a random fact for you");
    }

    @Override
    public String getExampleUsage() {
        return null;
    }

    @Override
    public List<String> getTriggers() {
        return Arrays.asList("chucknorris", "chuck", "norris");
    }

    @Override
    public List<String> getMiddleware() {
        return Collections.singletonList("throttle:user,2,5");
    }

    @Override
    public boolean onCommand(Message message, String[] args) {
        RequestFactory.makeGET("http://api.icndb.com/jokes/random")
            .addParameter("escape", "javascript")
            .send((Consumer<Response>) response -> {
                ChuckNorrisService service = (ChuckNorrisService) response.toService(ChuckNorrisService.class);

                MessageFactory.makeSuccess(message, service.getValue().getJoke()).queue();
            });
        return true;
    }
}

@@ -1023,16 +1023,23 @@
 :command
+ %5Bname%5D
 %60 - Gets
@@ -1060,16 +1060,91 @@
  for you
+, if a name is given %5C%22Chuck Norris%5C%22 will be replaced with the given name.
 %22);%0A    
@@ -1217,12 +1217,30 @@
 urn 
-null
+%22%60:command @Senither%60%22
 ;%0A  
@@ -1907,16 +1907,43 @@
 message,
+ prepareJoke(message, args,
  service
@@ -1964,16 +1964,17 @@
 tJoke())
+)
 .queue()
@@ -2017,11 +2017,467 @@
 e;%0A    %7D
+%0A%0A    private String prepareJoke(Message message, String%5B%5D args, String joke) %7B%0A        if (!message.getMentionedUsers().isEmpty()) %7B%0A            return joke.replaceAll(%22Chuck Norris%22, message.getGuild().getMember(%0A                message.getMentionedUsers().get(0)%0A            ).getEffectiveName());%0A        %7D%0A%0A        if (args.length %3E 0) %7B%0A            return joke.replaceAll(%22Chuck Norris%22, String.join(%22 %22, args));%0A        %7D%0A%0A        return joke;%0A    %7D
 %0A%7D%0A

Add support for custom names in chuck norris command