package net.minecraft.server.commands;

import com.mojang.brigadier.Message;
import net.minecraft.commands.SharedSuggestionProvider;
import java.util.concurrent.CompletableFuture;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.network.chat.ComponentUtils;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.server.packs.repository.PackRepository;
import java.util.Collection;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Function;
import java.util.List;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import com.google.common.collect.Lists;
import net.minecraft.server.packs.repository.Pack;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.minecraft.commands.Commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.commands.CommandSourceStack;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;

public class DataPackCommand {
    private static final DynamicCommandExceptionType ERROR_UNKNOWN_PACK;
    private static final DynamicCommandExceptionType ERROR_PACK_ALREADY_ENABLED;
    private static final DynamicCommandExceptionType ERROR_PACK_ALREADY_DISABLED;
    private static final SuggestionProvider<CommandSourceStack> SELECTED_PACKS;
    private static final SuggestionProvider<CommandSourceStack> UNSELECTED_PACKS;
    
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("datapack").requires(cz -> cz.hasPermission(2))).then(Commands.literal("enable").then(((RequiredArgumentBuilder)((RequiredArgumentBuilder)((RequiredArgumentBuilder)((RequiredArgumentBuilder)Commands.argument("name", (com.mojang.brigadier.arguments.ArgumentType<Object>)StringArgumentType.string()).suggests((SuggestionProvider)DataPackCommand.UNSELECTED_PACKS).executes(commandContext -> enablePack((CommandSourceStack)commandContext.getSource(), getPack((CommandContext<CommandSourceStack>)commandContext, "name", true), (list, aap) -> aap.getDefaultPosition().<Pack, Pack>insert(list, aap, aap -> aap, false)))).then(Commands.literal("after").then(Commands.argument("existing", (com.mojang.brigadier.arguments.ArgumentType<Object>)StringArgumentType.string()).suggests((SuggestionProvider)DataPackCommand.SELECTED_PACKS).executes(commandContext -> enablePack((CommandSourceStack)commandContext.getSource(), getPack((CommandContext<CommandSourceStack>)commandContext, "name", true), (list, aap) -> list.add(list.indexOf(getPack((CommandContext<CommandSourceStack>)commandContext, "existing", false)) + 1, aap)))))).then(Commands.literal("before").then(Commands.argument("existing", (com.mojang.brigadier.arguments.ArgumentType<Object>)StringArgumentType.string()).suggests((SuggestionProvider)DataPackCommand.SELECTED_PACKS).executes(commandContext -> enablePack((CommandSourceStack)commandContext.getSource(), getPack((CommandContext<CommandSourceStack>)commandContext, "name", true), (list, aap) -> list.add(list.indexOf(getPack((CommandContext<CommandSourceStack>)commandContext, "existing", false)), aap)))))).then(Commands.literal("last").executes(commandContext -> enablePack((CommandSourceStack)commandContext.getSource(), getPack((CommandContext<CommandSourceStack>)commandContext, "name", true), List::add)))).then(Commands.literal("first").executes(commandContext -> enablePack((CommandSourceStack)commandContext.getSource(), getPack((CommandContext<CommandSourceStack>)commandContext, "name", true), (list, aap) -> list.add(0, aap))))))).then(Commands.literal("disable").then(Commands.argument("name", (com.mojang.brigadier.arguments.ArgumentType<Object>)StringArgumentType.string()).suggests((SuggestionProvider)DataPackCommand.SELECTED_PACKS).executes(commandContext -> disablePack((CommandSourceStack)commandContext.getSource(), getPack((CommandContext<CommandSourceStack>)commandContext, "name", false)))))).then(((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("list").executes(commandContext -> listPacks((CommandSourceStack)commandContext.getSource()))).then(Commands.literal("available").executes(commandContext -> listAvailablePacks((CommandSourceStack)commandContext.getSource())))).then(Commands.literal("enabled").executes(commandContext -> listEnabledPacks((CommandSourceStack)commandContext.getSource())))));
    }
    
    private static int enablePack(final CommandSourceStack cz, final Pack aap, final Inserter a) throws CommandSyntaxException {
        final PackRepository<?> aar4 = cz.getServer().getPackRepository();
        final List<Pack> list5 = Lists.newArrayList(aar4.getSelectedPacks());
        a.apply(list5, aap);
        cz.sendSuccess(new TranslatableComponent("commands.datapack.modify.enable", new Object[] { aap.getChatLink(true) }), true);
        ReloadCommand.reloadPacks(list5.stream().map(Pack::getId).collect(Collectors.toList()), cz);
        return list5.size();
    }
    
    private static int disablePack(final CommandSourceStack cz, final Pack aap) {
        final PackRepository<?> aar3 = cz.getServer().getPackRepository();
        final List<Pack> list4 = Lists.newArrayList(aar3.getSelectedPacks());
        list4.remove(aap);
        cz.sendSuccess(new TranslatableComponent("commands.datapack.modify.disable", new Object[] { aap.getChatLink(true) }), true);
        ReloadCommand.reloadPacks(list4.stream().map(Pack::getId).collect(Collectors.toList()), cz);
        return list4.size();
    }
    
    private static int listPacks(final CommandSourceStack cz) {
        return listEnabledPacks(cz) + listAvailablePacks(cz);
    }
    
    private static int listAvailablePacks(final CommandSourceStack cz) {
        final PackRepository<?> aar2 = cz.getServer().getPackRepository();
        aar2.reload();
        final Collection<? extends Pack> collection3 = aar2.getSelectedPacks();
        final Collection<? extends Pack> collection4 = aar2.getAvailablePacks();
        final List<Pack> list5 = collection4.stream().filter(aap -> !collection3.contains(aap)).collect(Collectors.toList());
        if (list5.isEmpty()) {
            cz.sendSuccess(new TranslatableComponent("commands.datapack.list.available.none"), false);
        }
        else {
            cz.sendSuccess(new TranslatableComponent("commands.datapack.list.available.success", new Object[] { list5.size(), ComponentUtils.<Pack>formatList((Collection<Pack>)list5, aap -> aap.getChatLink(false)) }), false);
        }
        return list5.size();
    }
    
    private static int listEnabledPacks(final CommandSourceStack cz) {
        final PackRepository<?> aar2 = cz.getServer().getPackRepository();
        aar2.reload();
        final Collection<? extends Pack> collection3 = aar2.getSelectedPacks();
        if (collection3.isEmpty()) {
            cz.sendSuccess(new TranslatableComponent("commands.datapack.list.enabled.none"), false);
        }
        else {
            cz.sendSuccess(new TranslatableComponent("commands.datapack.list.enabled.success", new Object[] { collection3.size(), ComponentUtils.<Pack>formatList((Collection<Pack>)collection3, aap -> aap.getChatLink(true)) }), false);
        }
        return collection3.size();
    }
    
    private static Pack getPack(final CommandContext<CommandSourceStack> commandContext, final String string, final boolean boolean3) throws CommandSyntaxException {
        final String string2 = StringArgumentType.getString((CommandContext)commandContext, string);
        final PackRepository<?> aar5 = ((CommandSourceStack)commandContext.getSource()).getServer().getPackRepository();
        final Pack aap6 = (Pack)aar5.getPack(string2);
        if (aap6 == null) {
            throw DataPackCommand.ERROR_UNKNOWN_PACK.create(string2);
        }
        final boolean boolean4 = aar5.getSelectedPacks().contains(aap6);
        if (boolean3 && boolean4) {
            throw DataPackCommand.ERROR_PACK_ALREADY_ENABLED.create(string2);
        }
        if (!boolean3 && !boolean4) {
            throw DataPackCommand.ERROR_PACK_ALREADY_DISABLED.create(string2);
        }
        return aap6;
    }
    
    static {
        final TranslatableComponent translatableComponent;
        ERROR_UNKNOWN_PACK = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("commands.datapack.unknown", new Object[] { object });
            return translatableComponent;
        });
        final TranslatableComponent translatableComponent2;
        ERROR_PACK_ALREADY_ENABLED = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("commands.datapack.enable.failed", new Object[] { object });
            return translatableComponent2;
        });
        final TranslatableComponent translatableComponent3;
        ERROR_PACK_ALREADY_DISABLED = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("commands.datapack.disable.failed", new Object[] { object });
            return translatableComponent3;
        });
        SELECTED_PACKS = ((commandContext, suggestionsBuilder) -> SharedSuggestionProvider.suggest(((CommandSourceStack)commandContext.getSource()).getServer().getPackRepository().getSelectedIds().stream().<String>map(StringArgumentType::escapeIfRequired), suggestionsBuilder));
        UNSELECTED_PACKS = ((commandContext, suggestionsBuilder) -> {
            final PackRepository<?> aar3 = ((CommandSourceStack)commandContext.getSource()).getServer().getPackRepository();
            final Collection<String> collection4 = aar3.getSelectedIds();
            return SharedSuggestionProvider.suggest(aar3.getAvailableIds().stream().filter(string -> !collection4.contains(string)).<String>map(StringArgumentType::escapeIfRequired), suggestionsBuilder);
        });
    }
    
    interface Inserter {
        void apply(final List<Pack> list, final Pack aap) throws CommandSyntaxException;
    }
}
