// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.command.commands.utility.help;

import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import java.util.Set;
import java.util.Iterator;
import java.util.Map;
import com.hypixel.hytale.server.core.command.system.CommandManager;
import com.hypixel.hytale.server.core.universe.world.World;
import java.util.concurrent.Executor;
import com.hypixel.hytale.server.core.entity.entities.player.pages.CustomUIPage;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.command.system.pages.CommandListPage;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import javax.annotation.Nullable;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import com.hypixel.hytale.protocol.GameMode;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractAsyncCommand;

public class HelpCommand extends AbstractAsyncCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
    
    public HelpCommand() {
        super("help", "server.commands.help.desc");
        this.addAliases("?");
        this.setPermissionGroup(GameMode.Adventure);
        this.addUsageVariant(new HelpCommandVariant());
    }
    
    @Nonnull
    @Override
    protected CompletableFuture<Void> executeAsync(@Nonnull final CommandContext context) {
        return openHelpUI(context, null);
    }
    
    @Nonnull
    static CompletableFuture<Void> openHelpUI(@Nonnull final CommandContext context, @Nullable final String initialCommand) {
        if (!context.isPlayer()) {
            return CompletableFuture.completedFuture((Void)null);
        }
        final Ref<EntityStore> playerRef = context.senderAsPlayerRef();
        if (playerRef == null || !playerRef.isValid()) {
            context.sendMessage(HelpCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
            return CompletableFuture.completedFuture((Void)null);
        }
        final Store<EntityStore> store = playerRef.getStore();
        final World world = store.getExternalData().getWorld();
        final String resolvedCommand = resolveCommandName(initialCommand);
        return CompletableFuture.runAsync(() -> {
            final Player playerComponent = store.getComponent(playerRef, Player.getComponentType());
            final PlayerRef playerRefComponent = store.getComponent(playerRef, PlayerRef.getComponentType());
            if (playerComponent != null && playerRefComponent != null) {
                playerComponent.getPageManager().openCustomPage(playerRef, store, new CommandListPage(playerRefComponent, resolvedCommand));
            }
        }, world);
    }
    
    @Nullable
    private static String resolveCommandName(@Nullable final String commandNameOrAlias) {
        if (commandNameOrAlias == null) {
            return null;
        }
        final String lowerName = commandNameOrAlias.toLowerCase();
        final Map<String, AbstractCommand> commands = CommandManager.get().getCommandRegistration();
        if (commands.containsKey(lowerName)) {
            return lowerName;
        }
        for (final Map.Entry<String, AbstractCommand> entry : commands.entrySet()) {
            final Set<String> aliases = entry.getValue().getAliases();
            if (aliases != null && aliases.contains(lowerName)) {
                return entry.getKey();
            }
        }
        return lowerName;
    }
    
    static {
        MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
    }
    
    private static class HelpCommandVariant extends AbstractAsyncCommand
    {
        @Nonnull
        private final RequiredArg<String> commandArg;
        
        HelpCommandVariant() {
            super("server.commands.help.command.desc");
            this.commandArg = this.withRequiredArg("command", "server.commands.help.command.name.desc", ArgTypes.STRING);
        }
        
        @Nonnull
        @Override
        protected CompletableFuture<Void> executeAsync(@Nonnull final CommandContext context) {
            final String commandName = this.commandArg.get(context);
            return HelpCommand.openHelpUI(context, commandName);
        }
    }
}
