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

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

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.server.core.modules.entity.component.HeadRotation;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.builtin.buildertools.PrototypePlayerBuilderToolSettings;
import com.hypixel.hytale.server.core.entity.entities.Player;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.command.system.arguments.types.RelativeDirection;
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 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.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class StackCommand extends AbstractPlayerCommand
{
    @Nonnull
    private final FlagArg emptyFlag;
    @Nonnull
    private final OptionalArg<Integer> spacingArg;
    
    public StackCommand() {
        super("stack", "server.commands.stack.desc");
        this.emptyFlag = this.withFlagArg("empty", "server.commands.stack.empty.desc");
        this.spacingArg = this.withOptionalArg("spacing", "server.commands.stack.spacing.desc", ArgTypes.INTEGER);
        this.setPermissionGroup(GameMode.Creative);
        this.addUsageVariant(new StackWithCountCommand());
        this.addUsageVariant(new StackWithDirectionAndCountCommand());
    }
    
    @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) {
        executeStack(store, ref, null, 1, ((Argument<Arg, Boolean>)this.emptyFlag).get(context), this.spacingArg.provided(context) ? ((int)this.spacingArg.get(context)) : 0);
    }
    
    private static void executeStack(@Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nullable final RelativeDirection direction, final int count, final boolean empty, final int spacing) {
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
        assert playerRefComponent != null;
        if (!PrototypePlayerBuilderToolSettings.isOkayToDoCommandsOnSelection(ref, playerComponent, store)) {
            return;
        }
        final HeadRotation headRotationComponent = store.getComponent(ref, HeadRotation.getComponentType());
        assert headRotationComponent != null;
        final Vector3i directionVector = RelativeDirection.toDirectionVector(direction, headRotationComponent);
        BuilderToolsPlugin.addToQueue(playerComponent, playerRefComponent, (r, s, componentAccessor) -> s.stack(r, directionVector, count, empty, spacing, componentAccessor));
    }
    
    private static class StackWithCountCommand extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<Integer> countArg;
        @Nonnull
        private final FlagArg emptyFlag;
        @Nonnull
        private final OptionalArg<Integer> spacingArg;
        
        public StackWithCountCommand() {
            super("server.commands.stack.desc");
            this.countArg = this.withRequiredArg("count", "server.commands.stack.count.desc", ArgTypes.INTEGER);
            this.emptyFlag = this.withFlagArg("empty", "server.commands.stack.empty.desc");
            this.spacingArg = this.withOptionalArg("spacing", "server.commands.stack.spacing.desc", ArgTypes.INTEGER);
            this.setPermissionGroup(GameMode.Creative);
        }
        
        @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) {
            StackCommand.executeStack(store, ref, null, this.countArg.get(context), ((Argument<Arg, Boolean>)this.emptyFlag).get(context), this.spacingArg.provided(context) ? ((int)this.spacingArg.get(context)) : 0);
        }
    }
    
    private static class StackWithDirectionAndCountCommand extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<RelativeDirection> directionArg;
        @Nonnull
        private final RequiredArg<Integer> countArg;
        @Nonnull
        private final FlagArg emptyFlag;
        @Nonnull
        private final OptionalArg<Integer> spacingArg;
        
        public StackWithDirectionAndCountCommand() {
            super("server.commands.stack.desc");
            this.directionArg = this.withRequiredArg("direction", "server.commands.stack.direction.desc", RelativeDirection.ARGUMENT_TYPE);
            this.countArg = this.withRequiredArg("count", "server.commands.stack.count.desc", ArgTypes.INTEGER);
            this.emptyFlag = this.withFlagArg("empty", "server.commands.stack.empty.desc");
            this.spacingArg = this.withOptionalArg("spacing", "server.commands.stack.spacing.desc", ArgTypes.INTEGER);
            this.setPermissionGroup(GameMode.Creative);
        }
        
        @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) {
            StackCommand.executeStack(store, ref, this.directionArg.get(context), this.countArg.get(context), ((Argument<Arg, Boolean>)this.emptyFlag).get(context), this.spacingArg.provided(context) ? ((int)this.spacingArg.get(context)) : 0);
        }
    }
}
