/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.mojang.brigadier.builder.LiteralArgumentBuilder
 *  com.mojang.brigadier.tree.CommandNode
 *  com.mojang.brigadier.tree.RootCommandNode
 *  net.minecraft.class_124
 *  net.minecraft.class_2172
 *  net.minecraft.class_2561
 *  net.minecraft.class_5250
 *  net.minecraft.class_637
 */
package meteordevelopment.meteorclient.commands.commands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import java.util.Map;
import meteordevelopment.meteorclient.commands.Command;
import meteordevelopment.meteorclient.commands.Commands;
import meteordevelopment.meteorclient.commands.arguments.CommandArgumentType;
import meteordevelopment.meteorclient.utils.player.ChatUtils;
import net.minecraft.class_124;
import net.minecraft.class_2172;
import net.minecraft.class_2561;
import net.minecraft.class_5250;
import net.minecraft.class_637;

public class HelpCommand
extends Command {
    public HelpCommand() {
        super("help", "Shows you what a command does.", new String[0]);
    }

    @Override
    public void build(LiteralArgumentBuilder<class_2172> builder) {
        builder.then(HelpCommand.argument("command", CommandArgumentType.create()).executes(context -> {
            this.showHelp(CommandArgumentType.get(context));
            return 1;
        }));
        builder.executes(context -> {
            this.showHelp(this);
            return 1;
        });
    }

    private void showHelp(Command cmd) {
        class_5250 msg = class_2561.method_43470((String)"");
        msg.method_10852((class_2561)class_2561.method_43470((String)"Help for ").method_27692(class_124.field_1080).method_10852((class_2561)class_2561.method_43470((String)cmd.getName()).method_27692(class_124.field_1054)));
        msg.method_10852((class_2561)class_2561.method_43470((String)"\n ")).method_10852((class_2561)class_2561.method_43470((String)"Description: ").method_27692(class_124.field_1080).method_10852((class_2561)class_2561.method_43470((String)cmd.getDescription()).method_27692(class_124.field_1068)));
        if (!cmd.getAliases().isEmpty()) {
            msg.method_10852((class_2561)class_2561.method_43470((String)"\n ")).method_10852((class_2561)class_2561.method_43470((String)"Aliases: ").method_27692(class_124.field_1080));
            msg.method_10852((class_2561)class_2561.method_43470((String)String.join((CharSequence)", ", cmd.getAliases())).method_27692(class_124.field_1075));
        }
        msg.method_10852((class_2561)this.getUsageText(cmd));
        ChatUtils.sendMsg((class_2561)msg);
    }

    private class_5250 getUsageText(Command cmd) {
        class_637 source = mc.method_1562().method_2875();
        RootCommandNode root = Commands.DISPATCHER.getRoot();
        CommandNode node = root.getChild(cmd.getName());
        class_5250 usagesText = class_2561.method_43470((String)"");
        if (node != null) {
            Map usages = Commands.DISPATCHER.getSmartUsage(node, (Object)source);
            for (String usage : usages.values()) {
                usagesText.method_10852((class_2561)class_2561.method_43470((String)("\n " + String.valueOf(cmd) + " ")).method_27692(class_124.field_1060)).method_10852((class_2561)class_2561.method_43470((String)usage).method_27692(class_124.field_1060));
            }
        }
        if (usagesText.getString().isEmpty()) {
            usagesText.method_10852((class_2561)class_2561.method_43470((String)("\n " + String.valueOf(cmd))).method_27692(class_124.field_1060));
        }
        return class_2561.method_43470((String)"\n Usage:").method_27692(class_124.field_1080).method_10852((class_2561)usagesText);
    }
}

