package com.blamejared.mcbot.commands;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

import com.blamejared.mcbot.commands.api.Command;
import com.blamejared.mcbot.commands.api.CommandException;
import com.blamejared.mcbot.commands.api.CommandPersisted;
import com.google.gson.reflect.TypeToken;

import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import sx.blah.discord.handle.obj.IMessage;

@Command
public class CommandQuote extends CommandPersisted<TIntObjectMap<String>> {
    
    public CommandQuote() {
        super("quote", false, TIntObjectHashMap::new);
//        quotes.put(id++, "But noone cares - HellFirePVP");
//        quotes.put(id++, "CRAFTTWEAKER I MEANT CRAFTTWEAKER - Drullkus");
//        quotes.put(id++, "oh yeah im dumb - Kit");
//        quotes.put(id++, "i call zenscripts \"mt scripts\" - Kit");
//        quotes.put(id++, "yes - Shadows");
    }
    
    @Override
    protected TypeToken<TIntObjectMap<String>> getDataType() {
        return new TypeToken<TIntObjectMap<String>>(){};
    }

    Random rand = new Random();
    
    @Override
    public void process(IMessage message, List<String> flags, List<String> args) throws CommandException {
        for(String flag : flags) {
            if(flag.equalsIgnoreCase("ls")) {
                StringBuilder builder = new StringBuilder();
                builder.append("List of quotes:\n");
                storage.get(message).forEachEntry((k, v) -> {
                    builder.append(k).append(") ").append(v).append("\n");
                    return true;
                });
                message.getChannel().sendMessage(builder.toString());
                return;
            } else if(flag.equalsIgnoreCase("add")) {
                TIntObjectMap<String> quotes = storage.get(message);
                quotes.put(Arrays.stream(quotes.keys()).max().orElse(1), escapeMentions(message.getGuild(), message.getContent().substring("!quote -add ".length())));
                message.getChannel().sendMessage("Added quote!");
                return;
            } else if(flag.startsWith("remove=")) {
                int index = Integer.parseInt(flag.split("remove=")[1]);
                storage.get(message).remove(index);
                message.getChannel().sendMessage("Removed quote!");
                return;
            }
        }
        if(args.isEmpty()) {
            int[] keys = storage.get(message).keys();
            if (keys.length == 0) {
                throw new CommandException("There are no quotes!");
            }
            message.getChannel().sendMessage(storage.get(message).get(keys[rand.nextInt(keys.length)]));
        } else {
            int id;
            try {
                id = Integer.parseInt(args.get(0));
            } catch (NumberFormatException e) {
                throw new CommandException(args.get(0) + " is not a number!");
            }
            String quote = storage.get(message).get(id);
            if (quote != null) {
                message.getChannel().sendMessage(quote);
            } else {
                throw new CommandException("No quote for ID " + args.get(0));
            }
        }
    }
    
    @Override
    public String getUsage() {
        return "[-add] <id>/<quote>";
    }
}

@@ -50,22 +50,23 @@
 va.util.
-Arrays
+HashMap
 ;%0Aimport
@@ -82,16 +82,38 @@
 l.List;%0A
+import java.util.Map;%0A
 import j
@@ -345,89 +345,8 @@
 n;%0A%0A
-import gnu.trove.map.TIntObjectMap;%0Aimport gnu.trove.map.hash.TIntObjectHashMap;%0A
 impo
@@ -446,30 +446,29 @@
 rsisted%3C
-TIntObjectMap%3C
+Map%3CInteger, 
 String%3E%3E
@@ -533,26 +533,16 @@
  false, 
-TIntObject
 HashMap:
@@ -898,38 +898,37 @@
 d TypeToken%3C
-TIntObjectMap%3C
+Map%3CInteger, 
 String%3E%3E get
@@ -969,30 +969,29 @@
 peToken%3C
-TIntObjectMap%3C
+Map%3CInteger, 
 String%3E%3E
@@ -1404,13 +1404,8 @@
 Each
-Entry
 ((k,
@@ -1492,41 +1492,8 @@
 %22);%0A
-                    return true;%0A
     
@@ -1676,22 +1676,21 @@
     
-TIntObjectMap%3C
+Map%3CInteger, 
 Stri
@@ -1755,41 +1755,53 @@
 put(
-Arrays.stream(quotes.keys()).max(
+quotes.keySet().stream().max(Integer::compare
 ).or
@@ -2326,19 +2326,23 @@
         
-i
+I
 nt
+eger
 %5B%5D keys 
@@ -2367,18 +2367,44 @@
 age).key
-s(
+Set().toArray(new Integer%5B0%5D
 );%0A     

Fix serialization of quotes