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.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.world.level.timers.FunctionCallback;
import net.minecraft.world.level.timers.TimerCallback;
import net.minecraft.world.level.timers.FunctionTagCallback;
import net.minecraft.world.level.timers.TimerQueue;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.tags.Tag;
import net.minecraft.commands.CommandFunction;
import com.mojang.datafixers.util.Either;
import net.minecraft.resources.ResourceLocation;
import com.mojang.datafixers.util.Pair;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.commands.arguments.TimeArgument;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.arguments.ArgumentType;
import net.minecraft.commands.arguments.item.FunctionArgument;
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;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;

public class ScheduleCommand {
    private static final SimpleCommandExceptionType ERROR_SAME_TICK;
    private static final DynamicCommandExceptionType ERROR_CANT_REMOVE;
    private static final SuggestionProvider<CommandSourceStack> SUGGEST_SCHEDULE;
    
    public static void register(final CommandDispatcher<CommandSourceStack> commandDispatcher) {
        commandDispatcher.register((LiteralArgumentBuilder)((LiteralArgumentBuilder)((LiteralArgumentBuilder)Commands.literal("schedule").requires(cz -> cz.hasPermission(2))).then(Commands.literal("function").then(Commands.argument("function", (com.mojang.brigadier.arguments.ArgumentType<Object>)FunctionArgument.functions()).suggests((SuggestionProvider)FunctionCommand.SUGGEST_FUNCTION).then(((RequiredArgumentBuilder)((RequiredArgumentBuilder)Commands.argument("time", (com.mojang.brigadier.arguments.ArgumentType<Object>)TimeArgument.time()).executes(commandContext -> schedule((CommandSourceStack)commandContext.getSource(), FunctionArgument.getFunctionOrTag((CommandContext<CommandSourceStack>)commandContext, "function"), IntegerArgumentType.getInteger(commandContext, "time"), true))).then(Commands.literal("append").executes(commandContext -> schedule((CommandSourceStack)commandContext.getSource(), FunctionArgument.getFunctionOrTag((CommandContext<CommandSourceStack>)commandContext, "function"), IntegerArgumentType.getInteger(commandContext, "time"), false)))).then(Commands.literal("replace").executes(commandContext -> schedule((CommandSourceStack)commandContext.getSource(), FunctionArgument.getFunctionOrTag((CommandContext<CommandSourceStack>)commandContext, "function"), IntegerArgumentType.getInteger(commandContext, "time"), true))))))).then(Commands.literal("clear").then(Commands.argument("function", (com.mojang.brigadier.arguments.ArgumentType<Object>)StringArgumentType.greedyString()).suggests((SuggestionProvider)ScheduleCommand.SUGGEST_SCHEDULE).executes(commandContext -> remove((CommandSourceStack)commandContext.getSource(), StringArgumentType.getString(commandContext, "function"))))));
    }
    
    private static int schedule(final CommandSourceStack cz, final Pair<ResourceLocation, Either<CommandFunction, Tag<CommandFunction>>> pair, final int integer, final boolean boolean4) throws CommandSyntaxException {
        // 
        // This method could not be decompiled.
        // 
        // Original Bytecode:
        // 
        //     1: ifne            11
        //     4: getstatic       net/minecraft/server/commands/ScheduleCommand.ERROR_SAME_TICK:Lcom/mojang/brigadier/exceptions/SimpleCommandExceptionType;
        //     7: invokevirtual   com/mojang/brigadier/exceptions/SimpleCommandExceptionType.create:()Lcom/mojang/brigadier/exceptions/CommandSyntaxException;
        //    10: athrow         
        //    11: aload_0         /* cz */
        //    12: invokevirtual   net/minecraft/commands/CommandSourceStack.getLevel:()Lnet/minecraft/server/level/ServerLevel;
        //    15: invokevirtual   net/minecraft/server/level/ServerLevel.getGameTime:()J
        //    18: iload_2         /* integer */
        //    19: i2l            
        //    20: ladd           
        //    21: lstore          long5
        //    23: aload_1         /* pair */
        //    24: invokevirtual   com/mojang/datafixers/util/Pair.getFirst:()Ljava/lang/Object;
        //    27: checkcast       Lnet/minecraft/resources/ResourceLocation;
        //    30: astore          uh7
        //    32: aload_0         /* cz */
        //    33: invokevirtual   net/minecraft/commands/CommandSourceStack.getServer:()Lnet/minecraft/server/MinecraftServer;
        //    36: invokevirtual   net/minecraft/server/MinecraftServer.getWorldData:()Lnet/minecraft/world/level/storage/WorldData;
        //    39: invokeinterface net/minecraft/world/level/storage/WorldData.overworldData:()Lnet/minecraft/world/level/storage/ServerLevelData;
        //    44: invokeinterface net/minecraft/world/level/storage/ServerLevelData.getScheduledEvents:()Lnet/minecraft/world/level/timers/TimerQueue;
        //    49: astore          ded8
        //    51: aload_1         /* pair */
        //    52: invokevirtual   com/mojang/datafixers/util/Pair.getSecond:()Ljava/lang/Object;
        //    55: checkcast       Lcom/mojang/datafixers/util/Either;
        //    58: aload           uh7
        //    60: iload_3         /* boolean4 */
        //    61: aload           ded8
        //    63: lload           long5
        //    65: aload_0         /* cz */
        //    66: iload_2         /* integer */
        //    67: invokedynamic   BootstrapMethod #5, accept:(Lnet/minecraft/resources/ResourceLocation;ZLnet/minecraft/world/level/timers/TimerQueue;JLnet/minecraft/commands/CommandSourceStack;I)Ljava/util/function/Consumer;
        //    72: invokevirtual   com/mojang/datafixers/util/Either.ifLeft:(Ljava/util/function/Consumer;)Lcom/mojang/datafixers/util/Either;
        //    75: aload           uh7
        //    77: iload_3         /* boolean4 */
        //    78: aload           ded8
        //    80: lload           long5
        //    82: aload_0         /* cz */
        //    83: iload_2         /* integer */
        //    84: invokedynamic   BootstrapMethod #6, accept:(Lnet/minecraft/resources/ResourceLocation;ZLnet/minecraft/world/level/timers/TimerQueue;JLnet/minecraft/commands/CommandSourceStack;I)Ljava/util/function/Consumer;
        //    89: invokevirtual   com/mojang/datafixers/util/Either.ifRight:(Ljava/util/function/Consumer;)Lcom/mojang/datafixers/util/Either;
        //    92: pop            
        //    93: lload           long5
        //    95: ldc2_w          2147483647
        //    98: invokestatic    java/lang/Math.floorMod:(JJ)J
        //   101: l2i            
        //   102: ireturn        
        //    Exceptions:
        //  throws com.mojang.brigadier.exceptions.CommandSyntaxException
        //    Signature:
        //  (Lnet/minecraft/commands/CommandSourceStack;Lcom/mojang/datafixers/util/Pair<Lnet/minecraft/resources/ResourceLocation;Lcom/mojang/datafixers/util/Either<Lnet/minecraft/commands/CommandFunction;Lnet/minecraft/tags/Tag<Lnet/minecraft/commands/CommandFunction;>;>;>;IZ)I
        //    MethodParameters:
        //  Name      Flags  
        //  --------  -----
        //  cz        
        //  pair      
        //  integer   
        //  boolean4  
        //    StackMapTable: 00 01 0B
        // 
        // The error that occurred was:
        // 
        // java.lang.IllegalStateException: Could not infer any expression.
        //     at com.strobel.decompiler.ast.TypeAnalysis.runInference(TypeAnalysis.java:374)
        //     at com.strobel.decompiler.ast.TypeAnalysis.run(TypeAnalysis.java:96)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:344)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:42)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:214)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:99)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethodBody(AstBuilder.java:782)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethod(AstBuilder.java:675)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addTypeMembers(AstBuilder.java:552)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeCore(AstBuilder.java:519)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeNoCache(AstBuilder.java:161)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createType(AstBuilder.java:150)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addType(AstBuilder.java:125)
        //     at cuchaz.enigma.SourceProvider.getSources(SourceProvider.java:85)
        //     at cuchaz.enigma.EnigmaProject$JarExport.decompileClass(EnigmaProject.java:241)
        //     at cuchaz.enigma.EnigmaProject$JarExport.lambda$decompile$1(EnigmaProject.java:230)
        //     at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        //     at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
        //     at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        //     at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        //     at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:952)
        //     at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:926)
        //     at java.base/java.util.stream.AbstractTask.compute(AbstractTask.java:327)
        //     at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
        //     at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
        //     at java.base/java.util.concurrent.ForkJoinTask.doInvoke(ForkJoinTask.java:408)
        //     at java.base/java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:736)
        //     at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateParallel(ReduceOps.java:919)
        //     at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
        //     at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
        //     at cuchaz.enigma.EnigmaProject$JarExport.decompile(EnigmaProject.java:233)
        //     at cuchaz.enigma.command.DecompileCommand.run(DecompileCommand.java:35)
        //     at cuchaz.enigma.CommandMain.main(CommandMain.java:43)
        // 
        throw new IllegalStateException("An error occurred while decompiling this method.");
    }
    
    private static int remove(final CommandSourceStack cz, final String string) throws CommandSyntaxException {
        final int integer3 = cz.getServer().getWorldData().overworldData().getScheduledEvents().remove(string);
        if (integer3 == 0) {
            throw ScheduleCommand.ERROR_CANT_REMOVE.create(string);
        }
        cz.sendSuccess(new TranslatableComponent("commands.schedule.cleared.success", new Object[] { integer3, string }), true);
        return integer3;
    }
    
    static {
        ERROR_SAME_TICK = new SimpleCommandExceptionType((Message)new TranslatableComponent("commands.schedule.same_tick"));
        final TranslatableComponent translatableComponent;
        ERROR_CANT_REMOVE = new DynamicCommandExceptionType(object -> {
            new TranslatableComponent("commands.schedule.cleared.failure", new Object[] { object });
            return translatableComponent;
        });
        SUGGEST_SCHEDULE = ((commandContext, suggestionsBuilder) -> SharedSuggestionProvider.suggest(((CommandSourceStack)commandContext.getSource()).getServer().getWorldData().overworldData().getScheduledEvents().getEventsIds(), suggestionsBuilder));
    }
}
