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

package com.hypixel.hytale.builtin.buildertools.commands;

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 com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nonnull;
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 com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class UndoCommand extends AbstractPlayerCommand
{
    public UndoCommand() {
        super("undo", "server.commands.undo.desc");
        this.setPermissionGroup(GameMode.Creative);
        this.requirePermission("hytale.editor.history");
        this.addAliases("u");
        this.addUsageVariant(new UndoWithCountCommand());
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
        executeUndo(store, ref, 1);
    }
    
    private static void executeUndo(@Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, final int count) {
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
        assert playerRefComponent != null;
        BuilderToolsPlugin.addToQueue(playerComponent, playerRefComponent, (r, s, c) -> s.undo(r, count, c));
    }
    
    private static class UndoWithCountCommand extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<Integer> countArg;
        
        public UndoWithCountCommand() {
            super("server.commands.undo.desc");
            this.countArg = this.withRequiredArg("count", "server.commands.undo.count.desc", ArgTypes.INTEGER);
            this.setPermissionGroup(GameMode.Creative);
            this.requirePermission("hytale.editor.history");
        }
        
        @Override
        protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
            UndoCommand.executeUndo(store, ref, this.countArg.get(context));
        }
    }
}
