package com.blamejared.mcbot.commands;

import java.awt.Color;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import java.util.stream.Collectors;

import com.blamejared.mcbot.commands.api.Command;
import com.blamejared.mcbot.commands.api.CommandBase;
import com.blamejared.mcbot.commands.api.CommandContext;
import com.blamejared.mcbot.commands.api.CommandException;
import com.blamejared.mcbot.commands.api.ICommand;
import com.blamejared.mcbot.mcp.DataDownloader;
import com.blamejared.mcbot.mcp.IMapping;
import com.blamejared.mcbot.mcp.ISrgMapping;
import com.blamejared.mcbot.mcp.ISrgMapping.MappingType;
import com.blamejared.mcbot.mcp.SrgMappingFactory;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;

import sx.blah.discord.util.EmbedBuilder;

@Command
public class CommandMCP extends CommandBase {
    
    private final MappingType type;
    private final Random rand = new Random();
    
    public CommandMCP() {
        this(null);
    }
    
    private CommandMCP(MappingType type) {
        super("mcp" + (type == null ? "" : type.getKey()), false);
        this.type = type;
    }
    
    @Override
    public boolean isTransient() {
        return type == null;
    }
    
    @SuppressWarnings("null")
    @Override
    public Iterable<ICommand> getChildren() {
        if (isTransient()) {
            return Arrays.stream(MappingType.values()).map(CommandMCP::new).collect(Collectors.toList());
        }
        return super.getChildren();
    }
    
    @Override
    public void process(CommandContext ctx) throws CommandException {
    
        String mcver = ctx.argCount() > 1 ? ctx.getArg(1) : DataDownloader.INSTANCE.getVersions().getLatestVersion();

        if (type == MappingType.CLASS) {
            List<ISrgMapping> classmappings = DataDownloader.INSTANCE.lookupSRG(MappingType.CLASS, ctx.getArg(0), mcver);
            if (!classmappings.isEmpty()) {
                ctx.reply(Joiner.on('\n').join(classmappings));
            } else {
                ctx.reply("No class found.");
            }
            return;
        }

        List<IMapping> mappings = DataDownloader.INSTANCE.lookup(type, ctx.getArg(0), mcver);
        
        StringBuilder builder = new StringBuilder();
        if (!mappings.isEmpty()) {
            mappings.forEach(m -> {
                // FIXME implement param lookup
                if(type == MappingType.PARAM){
                    ISrgMapping srg = DataDownloader.INSTANCE.lookupSRG(type, m.getSRG(), mcver).get(0);
                    builder.append("**MC " + mcver + ": " + srg.getOwner() + "." + m.getMCP() + "**\n");
                    builder.append("\n`").append(m.getSRG()).append("` <=> `").append(m.getMCP()).append("`");
                    builder.append("\n").append("Side: ").append(m.getSide());
                }else {
                    ISrgMapping srg = DataDownloader.INSTANCE.lookupSRG(type, m.getSRG(), mcver).get(0);
                    builder.append("\n");
                    if(m != mappings.get(0)) {
                        builder.append("\n");
                    }
                    builder.append("**MC " + mcver + ": " + srg.getOwner() + "." + m.getMCP() + "**\n");
                    builder.append("__Name__: " + srg.getNotch() + " => `" + m.getSRG() + "` => " + m.getMCP() + "\n");
                    builder.append("__Comment__: `" + (m.getComment().isEmpty() ? "None" : m.getComment()) + "`\n");
                    builder.append("__Side__: `" + m.getSide() + "`\n");
                    if(srg instanceof SrgMappingFactory.MethodMapping) {
                        SrgMappingFactory.MethodMapping map = (SrgMappingFactory.MethodMapping) srg;
                        builder.append("__AT__: `public ").append(Strings.nullToEmpty(srg.getOwner()).replaceAll("/", ".")).append(" ").append(srg.getSRG()).append(map.getSrgDesc()).append(" #").append(m.getMCP()).append("`");
                    } else
                        builder.append("__AT__: `public ").append(Strings.nullToEmpty(srg.getOwner()).replaceAll("/", ".")).append(" ").append(srg.getSRG()).append(" #").append(m.getMCP()).append("`");
                }
                });
        } else {
            builder.append("No information found!");
        }

        rand.setSeed(builder.toString().hashCode());

        final EmbedBuilder embed = new EmbedBuilder()
        	.setLenient(true)
        	.withDesc(builder.toString())
        	.withColor(Color.HSBtoRGB(rand.nextFloat(), 1, 1));
        
        ctx.reply(embed.build());
    }
    
    @Override
    public String getUsage() {
        return "<name> [mcver] - Looks up MCP info for a given " + type.name().toLowerCase(Locale.US) + ".";
    }
}

@@ -2878,16 +2878,17 @@
        %7D
+ 
 else %7B%0A 
@@ -3555,32 +3555,186 @@
 ide() + %22%60%5Cn%22);%0A
+                    builder.append(%22__AT__: %60public %22).append(Strings.nullToEmpty(srg.getOwner()).replaceAll(%22/%22, %22.%22)).append(%22 %22).append(srg.getSRG());%0A
                 
@@ -3934,193 +3934,24 @@
 end(
-%22__AT__: %60public %22).append(Strings.nullToEmpty(srg.getOwner()).replaceAll(%22/%22, %22.%22)).append(%22 %22).append(srg.getSRG()).append(map.getSrgDesc()).append(%22 #%22).append(m.getMCP()).append(%22%60%22
+map.getSrgDesc()
 );%0A 
@@ -3974,16 +3974,9 @@
    %7D
- else%0A  
+%0A
     
@@ -3983,34 +3983,32 @@
                 
-  
 builder.append(%22
@@ -4011,135 +4011,11 @@
 nd(%22
-__AT__: %60public %22).append(Strings.nullToEmpty(srg.getOwner()).replaceAll(%22/%22, %22.%22)).append(%22 %22).append(srg.getSRG()).append(%22
  #
+ 
 %22).a
@@ -4055,36 +4055,32 @@
               %7D%0A
-    
             %7D);%0A

Clean up !mcp AT output