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

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

import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.builtin.blockspawner.state.BlockSpawner;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
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.RelativeIntPosition;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class BlockSpawnerGetCommand 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 Message MESSAGE_COMMANDS_BLOCK_SPAWNER_NO_BLOCK_SPAWNER_SET;
    @Nonnull
    private final OptionalArg<RelativeIntPosition> positionArg;
    
    public BlockSpawnerGetCommand() {
        super("get", "server.commands.blockspawner.get.desc");
        this.positionArg = this.withOptionalArg("position", "server.commands.blockspawner.position.desc", ArgTypes.RELATIVE_BLOCK_POSITION);
    }
    
    @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(BlockSpawnerGetCommand.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(BlockSpawnerGetCommand.MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE);
            }
            position = targetBlock;
        }
        final ChunkStore chunkStore = world.getChunkStore();
        final long chunkIndex = ChunkUtil.indexChunkFromBlock(position.x, position.z);
        final Ref<ChunkStore> chunkRef = chunkStore.getChunkReference(chunkIndex);
        if (chunkRef == null || !chunkRef.isValid()) {
            context.sendMessage(Message.translation("server.general.containerNotFound").param("block", position.toString()));
            return;
        }
        final WorldChunk worldChunkComponent = chunkStore.getStore().getComponent(chunkRef, WorldChunk.getComponentType());
        assert worldChunkComponent != null;
        final Ref<ChunkStore> blockRef = worldChunkComponent.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 = chunkStore.getStore().getComponent(blockRef, BlockSpawner.getComponentType());
        if (spawnerState == null) {
            context.sendMessage(Message.translation("server.general.containerNotFound").param("block", position.toString()));
            return;
        }
        if (spawnerState.getBlockSpawnerId() == null) {
            context.sendMessage(BlockSpawnerGetCommand.MESSAGE_COMMANDS_BLOCK_SPAWNER_NO_BLOCK_SPAWNER_SET);
        }
        else {
            context.sendMessage(Message.translation("server.commands.blockspawner.currentBlockSpawner").param("id", spawnerState.getBlockSpawnerId()));
        }
    }
    
    static {
        MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE = Message.translation("server.general.blockTargetNotInRange");
        MESSAGE_COMMANDS_ERRORS_PROVIDE_POSITION = Message.translation("server.commands.errors.providePosition");
        MESSAGE_COMMANDS_BLOCK_SPAWNER_NO_BLOCK_SPAWNER_SET = Message.translation("server.commands.blockspawner.noBlockSpawnerSet");
    }
}
