package com.tterrag.k9.commands;

import java.io.File;

import com.google.gson.Gson;
import com.tterrag.k9.commands.api.Argument;
import com.tterrag.k9.commands.api.Command;
import com.tterrag.k9.commands.api.CommandBase;
import com.tterrag.k9.commands.api.CommandContext;
import com.tterrag.k9.commands.api.CommandException;
import com.tterrag.k9.commands.api.CommandRegistrar;
import com.tterrag.k9.mappings.mcp.McpDownloader;
import com.tterrag.k9.mappings.mcp.McpVersionJson;
import com.tterrag.k9.mappings.mcp.McpVersionJson.McpMappingsJson;

import sx.blah.discord.util.EmbedBuilder;

@Command
public class CommandMCPVersions extends CommandBase {
    
    private static final Argument<String> ARG_VERSION = CommandMappings.ARG_VERSION;
    
    private CommandMCP mcpCommand;
    
    public CommandMCPVersions() {
        super("mcpv", false);
    }
    
    @Override
    public void init(File dataFolder, Gson gson) {
        super.init(dataFolder, gson);
        
        mcpCommand = (CommandMCP) CommandRegistrar.INSTANCE.findCommand(null, "mcp");
    }

    @Override
    public void process(CommandContext ctx) throws CommandException {
        String version = ctx.getArgOrGet(ARG_VERSION, () -> mcpCommand.getData(ctx));
        if (version == null) {
            version = McpDownloader.INSTANCE.getLatestMinecraftVersion();
        }
        McpVersionJson versions = McpDownloader.INSTANCE.getVersions();
        EmbedBuilder builder = new EmbedBuilder();

        for (String s : versions.getVersions()) {
            if (s.equals(version)) {
                McpMappingsJson mappings = versions.getMappings(s);
                if (mappings == null) {
                    throw new IllegalStateException("No mappings found for MC version: " + s);
                }
                builder.withTitle("Latest mappings for MC " + s);
                StringBuilder desc = new StringBuilder();
                String stableVersion = null;
                if (mappings.latestStable() > 0) {
                    stableVersion = "stable_" + mappings.latestStable();
                    desc.append(stableVersion).append("\n");
                }
                String snapshotVersion = "snapshot_" + mappings.latestSnapshot();
                desc.append(snapshotVersion);
                builder.withDesc(desc.toString());
                boolean fg3 = Integer.parseInt(s.split("\\.")[1]) >= 13;
                if (stableVersion != null) {
                    builder.appendField("Gradle String (Stable)", fg3 ? "`mappings channel: 'stable', version: '" + mappings.latestStable() + "'`" : "`mappings = '" + stableVersion + "'`", true);
                }
                builder.appendField("Gradle String (Snapshot)", fg3 ? "`mappings channel: 'snapshot', version: '" + mappings.latestSnapshot() + "'`" : "`mappings = '" + snapshotVersion + "'`", true);
                builder.withColor(0x810000);
            }
        }
        ctx.reply(builder.build());        
    }

    @Override
    public String getDescription() {
        return "Lists the latest mappings version for the given MC version. If none is given, uses the guild default.";
    }

}

@@ -2350,19 +2350,22 @@
     
-boolean fg3
+String pattern
  = I
@@ -2404,16 +2404,88 @@
 %5D) %3E= 13
+ ? %22%60mappings channel: '%25s', version: '%25s-%25s'%60%22 : %22%60mappings = '%25s_%25s'%60%22
 ;%0A      
@@ -2593,347 +2593,235 @@
 )%22, 
-fg3 ? %22%60mappings channel: 'stable', version: '%22 + mappings.latestStable() + %22'%60%22 : %22%60mappings = '%22 + stableVersion + %22'%60%22, true);%0A                %7D%0A                builder.appendField(%22Gradle String (Snapshot)%22, fg3 ? %22%60mappings channel: 'snapshot', version: '%22 + mappings.latestSnapshot() + %22'%60%22 : %22%60mappings = '%22 + snapshotVersion + %22'%60%22
+String.format(pattern, %22stable%22, mappings.latestStable(), version), true);%0A                %7D%0A                builder.appendField(%22Gradle String (Snapshot)%22, String.format(pattern, %22snapshot%22, mappings.latestSnapshot(), version)
 , tr

Clean up mcp versions gradle string building, fix issues with FG3 format