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

package com.hypixel.hytale.builtin.blockspawner.command;

import com.hypixel.hytale.assetstore.map.JsonAssetWithMap;
import com.hypixel.hytale.server.core.command.system.arguments.types.AssetArgumentType;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.builtin.blockspawner.state.BlockSpawner;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.server.core.command.system.exceptions.GeneralCommandException;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.RelativeIntPosition;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.builtin.blockspawner.BlockSpawnerTable;
import com.hypixel.hytale.server.core.command.system.arguments.types.SingleArgumentType;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class BlockSpawnerSetCommand extends AbstractWorldCommand
{
    @Nonnull
    private static final Message MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ERRORS_PROVIDE_POSITION;
    @Nonnull
    private static final SingleArgumentType<BlockSpawnerTable> BLOCK_SPAWNER_ASSET_TYPE;
    @Nonnull
    private final RequiredArg<BlockSpawnerTable> blockSpawnerIdArg;
    @Nonnull
    private final OptionalArg<RelativeIntPosition> positionArg;
    @Nonnull
    private final FlagArg ignoreChecksFlag;
    
    public BlockSpawnerSetCommand() {
        super("set", "server.commands.blockspawner.set.desc");
        this.blockSpawnerIdArg = this.withRequiredArg("blockSpawnerId", "server.commands.blockspawner.set.blockSpawnerId.desc", BlockSpawnerSetCommand.BLOCK_SPAWNER_ASSET_TYPE);
        this.positionArg = this.withOptionalArg("position", "server.commands.blockspawner.position.desc", ArgTypes.RELATIVE_BLOCK_POSITION);
        this.ignoreChecksFlag = this.withFlagArg("ignoreChecks", "server.commands.blockspawner.arg.ignoreChecks");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        Vector3i position;
        if (this.positionArg.provided(context)) {
            final RelativeIntPosition relativePosition = this.positionArg.get(context);
            position = relativePosition.getBlockPosition(context, store);
        }
        else {
            if (!context.isPlayer()) {
                throw new GeneralCommandException(BlockSpawnerSetCommand.MESSAGE_COMMANDS_ERRORS_PROVIDE_POSITION);
            }
            final Ref<EntityStore> ref = context.senderAsPlayerRef();
            final Vector3i targetBlock = TargetUtil.getTargetBlock(ref, 10.0, store);
            if (targetBlock == null) {
                throw new GeneralCommandException(BlockSpawnerSetCommand.MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE);
            }
            position = targetBlock;
        }
        final WorldChunk chunk = world.getChunk(ChunkUtil.indexChunkFromBlock(position.x, position.z));
        final Ref<ChunkStore> blockRef = chunk.getBlockComponentEntity(position.x, position.y, position.z);
        if (blockRef == null) {
            context.sendMessage(Message.translation("server.general.containerNotFound").param("block", position.toString()));
            return;
        }
        final BlockSpawner spawnerState = world.getChunkStore().getStore().getComponent(blockRef, BlockSpawner.getComponentType());
        if (spawnerState == null) {
            context.sendMessage(Message.translation("server.general.containerNotFound").param("block", position.toString()));
            return;
        }
        String spawnerId;
        if (((Argument<Arg, Boolean>)this.ignoreChecksFlag).get(context)) {
            final String[] input = context.getInput(this.blockSpawnerIdArg);
            spawnerId = ((input != null && input.length > 0) ? input[0] : null);
            if (spawnerId == null) {
                context.sendMessage(Message.translation("errors.validation_failure").param("message", "blockSpawnerId is required when --ignoreChecks is set"));
                return;
            }
        }
        else {
            spawnerId = this.blockSpawnerIdArg.get(context).getId();
        }
        spawnerState.setBlockSpawnerId(spawnerId);
        chunk.markNeedsSaving();
        context.sendMessage(Message.translation("server.commands.blockspawner.blockSpawnerSet").param("id", spawnerId));
    }
    
    static {
        MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE = Message.translation("server.general.blockTargetNotInRange");
        MESSAGE_COMMANDS_ERRORS_PROVIDE_POSITION = Message.translation("server.commands.errors.providePosition");
        BLOCK_SPAWNER_ASSET_TYPE = new AssetArgumentType("server.commands.parsing.argtype.asset.blockspawnertable.name", (Class<JsonAssetWithMap>)BlockSpawnerTable.class, "server.commands.parsing.argtype.asset.blockspawnertable.usage");
    }
}
