package co.phoenixlab.discord.commands.fun;

import co.phoenixlab.discord.Command;
import co.phoenixlab.discord.MessageContext;
import co.phoenixlab.discord.api.DiscordApiClient;
import co.phoenixlab.discord.api.entities.Server;
import co.phoenixlab.discord.util.RateLimiter;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;

public class SignCommand implements Command {
    public static final int COOLDOWN_PERIOD = 30;
    public static final int COOLDOWN_CHARGES = 3;
    public static final String SIGN = "|￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣￣|\n" +
            "%s\n" +
            "|＿＿＿＿＿＿＿＿＿＿＿＿＿＿＿＿|\n" +
            "              (__/)   ||\n" +
            "              (•ㅅ•) ||\n" +
            "             / 　 づ";
    public static final int MESSAGE_AREA_WIDTH = 32;
    private LoadingCache<String, RateLimiter> rateLimiters;

    public SignCommand() {
        rateLimiters = CacheBuilder.newBuilder()
                .expireAfterAccess(5, TimeUnit.MINUTES)
                .build(new CacheLoader<String, RateLimiter>() {
                    @Override
                    public RateLimiter load(String key) throws Exception {
                        return new RateLimiter(
                                key,
                                TimeUnit.SECONDS.toMillis(COOLDOWN_PERIOD),
                                COOLDOWN_CHARGES
                        );
                    }
                });
    }

    @Override
    public void handleCommand(MessageContext context, String args) {
        if (args.isEmpty()) {
            return;
        }
        Server server = context.getServer();
        if (server == null || server == DiscordApiClient.NO_SERVER) {
            return;
        }
        String[] lines = new String[args.length() / MESSAGE_AREA_WIDTH + 1];
        for (int i = 0; i < lines.length; i++) {
            lines[i] = args.substring(i * MESSAGE_AREA_WIDTH,
                    Math.min((i + 1) * MESSAGE_AREA_WIDTH, args.length()));
        }
        //  center the last line
        String lastLine = lines[lines.length - 1];
        int numPad = MESSAGE_AREA_WIDTH - lastLine.length();
        if (numPad > 1) {
            numPad /= 2;
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < numPad; i++) {
                builder.append(' ');
            }
            builder.append(lastLine);
            lines[lines.length - 1] = builder.toString();
        }
        StringJoiner joiner = new StringJoiner("\n");
        for (String line : lines) {
            joiner.add(line);
        }
        String output = String.format(SIGN, joiner.toString());
        context.getApiClient().sendMessage(output, context.getChannel());
    }
}

@@ -214,20 +214,118 @@
 ntities.
-Serv
+Member;%0Aimport co.phoenixlab.discord.api.entities.Server;%0Aimport co.phoenixlab.discord.api.entities.Us
 er;%0Aimpo
@@ -572,16 +572,77 @@
 eUnit;%0A%0A
+import static com.google.common.base.Strings.isNullOrEmpty;%0A%0A
 public c
@@ -978,16 +978,22 @@
 (%E2%80%A2%E3%85%85%E2%80%A2) %7C%7C
+   ~%25s
 %5Cn%22 +%0A  
@@ -1077,10 +1077,10 @@
 H = 
-32
+64
 ;%0A  
@@ -2866,32 +2866,159 @@
 ine);%0A        %7D%0A
+        User author = context.getAuthor();%0A        Member authorMember = context.getApiClient().getUserMember(author, server);%0A
         String o
@@ -3062,16 +3062,58 @@
 String()
+, getUserDisplayName(author, authorMember)
 );%0A     
@@ -3177,20 +3177,299 @@
 Channel());%0A
+        context.getApiClient().deleteMessage(context.getChannel().getId(), context.getMessage().getId());%0A    %7D%0A%0A    private String getUserDisplayName(User user, Member member) %7B%0A        return isNullOrEmpty(member.getNick()) ?%0A            user.getUsername() : member.getNick();%0A
     %7D%0A%7D%0A

Add author, pad more, delete source msg