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

package com.hypixel.hytale.server.core.command.commands.world.chunk;

import com.hypixel.hytale.server.core.universe.world.chunk.section.BlockSection;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.math.vector.Vector3i;
import java.util.logging.Level;
import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.math.vector.Vector2i;
import com.hypixel.hytale.math.util.ChunkUtil;
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.RequiredArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class ChunkLightingCommand extends AbstractWorldCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_CHUNKINFO_SERIALIZED;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_CHUNKINFO_SERIALIZED_FAILED;
    @Nonnull
    private final RequiredArg<RelativeIntPosition> positionArg;
    
    public ChunkLightingCommand() {
        super("lighting", "server.commands.chunklighting.desc");
        this.positionArg = this.withRequiredArg("x y z", "server.commands.chunk.lighting.position.desc", ArgTypes.RELATIVE_BLOCK_POSITION);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final Vector3i position = this.positionArg.get(context).getBlockPosition(context, store);
        final ChunkStore chunkStore = world.getChunkStore();
        final Store<ChunkStore> chunkStoreStore = chunkStore.getStore();
        final Vector2i chunkPos = new Vector2i(ChunkUtil.chunkCoordinate(position.getX()), ChunkUtil.chunkCoordinate(position.getZ()));
        final long chunkIndex = ChunkUtil.indexChunk(chunkPos.x, chunkPos.y);
        final Ref<ChunkStore> chunkRef = chunkStore.getChunkReference(chunkIndex);
        if (chunkRef == null || !chunkRef.isValid()) {
            context.sendMessage(Message.translation("server.commands.errors.chunkNotLoaded").param("chunkX", chunkPos.x).param("chunkZ", chunkPos.y).param("world", world.getName()));
            context.sendMessage(Message.translation("server.commands.chunkinfo.load.usage").param("chunkX", chunkPos.x).param("chunkZ", chunkPos.y));
            return;
        }
        final BlockChunk blockChunkComponent = chunkStoreStore.getComponent(chunkRef, BlockChunk.getComponentType());
        assert blockChunkComponent != null;
        try {
            final BlockSection section = blockChunkComponent.getSectionAtBlockY(position.y);
            final String s = section.getLocalLight().octreeToString();
            HytaleLogger.getLogger().at(Level.INFO).log("Chunk light output for (%d, %d, %d): %s", position.x, position.y, position.z, s);
            context.sendMessage(ChunkLightingCommand.MESSAGE_COMMANDS_CHUNKINFO_SERIALIZED);
        }
        catch (final Throwable t) {
            HytaleLogger.getLogger().at(Level.SEVERE).log("Failed to print chunk:", t);
            context.sendMessage(ChunkLightingCommand.MESSAGE_COMMANDS_CHUNKINFO_SERIALIZED_FAILED);
        }
    }
    
    static {
        MESSAGE_COMMANDS_CHUNKINFO_SERIALIZED = Message.translation("server.commands.chunkinfo.serialized");
        MESSAGE_COMMANDS_CHUNKINFO_SERIALIZED_FAILED = Message.translation("server.commands.chunkinfo.serialized.failed");
    }
}
