package com.pg85.otg.forge.commands;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.command.ISuggestionProvider;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;

public class HelpCommand extends BaseCommand
{
	public HelpCommand()
	{
		this.name = "help";
		this.helpMessage = "Shows help for all OTG commands.";
		this.usage = "/otg help [command/page]";
		this.detailedHelp = new String[] { 
			"[command/page]: The name of the command you want to view detailed help for, or the page number of the help menu you want to display." 
		};
	}

	@Override
	public void build(LiteralArgumentBuilder<CommandSource> builder)
	{
		builder.then(Commands.literal("help").executes(context -> showHelp(context.getSource(), ""))
				.then(Commands.argument("command", new CommandArgument()).executes(
						(context -> showHelp(context.getSource(), context.getArgument("command", String.class))))));
	}

	protected int showHelp(CommandSource source, String cmd)
	{
		if (cmd.isEmpty())
		{
			showHelp(source, 1);
		} else
		{

			if (StringUtils.isNumeric(cmd))
			{
				showHelp(source, Integer.parseInt(cmd));
			} else
			{

				OTGCommand.getCommands().stream().filter(basecmd -> basecmd.getName().equalsIgnoreCase(cmd)).findFirst()
						.ifPresent(command ->
						{
							source.sendSuccess(new StringTextComponent("/otg " + command.getName() + ": ")
									.withStyle(TextFormatting.GOLD)
									.append(new StringTextComponent(command.getHelpMessage())
											.withStyle(TextFormatting.GREEN)),
									false);
							source.sendSuccess(new StringTextComponent("usage: " + command.getUsage())
									.withStyle(TextFormatting.GRAY), false);
							for (String help : command.getDetailedHelp())
							{
								source.sendSuccess(new StringTextComponent(help).withStyle(TextFormatting.GRAY), false);
							}
						});
			}
		}
		return 0;
	}

	private void showHelp(CommandSource source, int page)
	{
		int start = (page - 1) * 5;

		List<BaseCommand> commands = OTGCommand.getCommands();

		for (int i = start; i < commands.size() && i < start + 5; i++)
		{
			BaseCommand command = commands.get(i);

			source.sendSuccess(
					new StringTextComponent("/otg " + command.getName() + ": ").withStyle(TextFormatting.GOLD).append(
							new StringTextComponent(command.getHelpMessage()).withStyle(TextFormatting.GREEN)),
					false);
			source.sendSuccess(
					new StringTextComponent(" - usage: " + command.getUsage()).withStyle(TextFormatting.GRAY), false);
		}
		source.sendSuccess(
				new StringTextComponent("Use /otg help <page> for more commands.").withStyle(TextFormatting.GOLD),
				false);
	}

	public static class CommandArgument implements ArgumentType<String>
	{
		public static CommandArgument create()
		{
			return new CommandArgument();
		}

		@Override
		public String parse(StringReader reader) throws CommandSyntaxException
		{
			return reader.readString();
		}

		@Override
		public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder)
		{
			return ISuggestionProvider.suggest(
					OTGCommand.getCommands().stream().map(BaseCommand::getName).collect(Collectors.toList()), builder);
		}
	}
}

@@ -188,50 +188,8 @@
 s;%0A%0A
-import com.mojang.brigadier.StringReader;%0A
 impo
@@ -222,16 +222,22 @@
 guments.
+String
 Argument
@@ -358,71 +358,8 @@
 xt;%0A
-import com.mojang.brigadier.exceptions.CommandSyntaxException;%0A
 impo
@@ -931,15 +931,13 @@
 ng%5B%5D
- %7B 
 %0A%09%09
-%09
+%7B 
 %22%5Bco
@@ -1067,19 +1067,16 @@
 splay.%22 
-%0A%09%09
 %7D;%0A%09%7D%0A%0A%09
@@ -1288,37 +1288,69 @@
 mmand%22, 
-new CommandArgument()
+StringArgumentType.word()).suggests(this::suggestHelp
 ).execut
@@ -3186,310 +3186,14 @@
 %0A%0A%09p
-ublic static class CommandArgument implements ArgumentType%3CString%3E%0A%09%7B%0A%09%09public static CommandArgument create()%0A%09%09%7B%0A%09%09%09return new CommandArgument();%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public String parse(StringReader reader) throws CommandSyntaxException%0A%09%09%7B%0A%09%09%09return reader.readString();%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public %3CS%3E
+rivate
  Com
@@ -3224,23 +3224,19 @@
 ns%3E 
-listS
+s
 uggest
-ions
+Help
 (Com
@@ -3247,17 +3247,29 @@
 Context%3C
-S
+CommandSource
 %3E contex
@@ -3270,17 +3270,20 @@
 context,
- 
+%0A%09%09%09
 Suggesti
@@ -3302,21 +3302,19 @@
 uilder)%0A
-%09
 %09%7B%0A
-%09
 %09%09return
@@ -3343,17 +3343,16 @@
 uggest(%0A
-%09
 %09%09%09%09OTGC
@@ -3447,17 +3447,13 @@
 ilder);%0A
-%09%09%7D%0A
 %09%7D%0A%7D%0A

Remove help command argument class in favor of custom suggestion provider