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

package com.hypixel.hytale.server.core.command.commands.utility.lighting;

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.server.core.universe.world.chunk.section.BlockSection;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.universe.world.lighting.ChunkLightingManager;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
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.system.FlagArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class LightingInvalidateCommand extends AbstractWorldCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_INVALIDATED_LIGHTING;
    @Nonnull
    private final FlagArg oneFlag;
    
    public LightingInvalidateCommand() {
        super("invalidate", "server.commands.invalidatelighting.desc");
        this.oneFlag = this.withFlagArg("one", "server.commands.invalidatelighting.one.desc");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final ChunkLightingManager chunkLighting = world.getChunkLighting();
        final ChunkStore chunkStore = world.getChunkStore();
        if (((Argument<Arg, Boolean>)this.oneFlag).get(context)) {
            if (!context.isPlayer()) {
                context.sendMessage(LightingInvalidateCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                return;
            }
            final Ref<EntityStore> ref = context.senderAsPlayerRef();
            if (ref == null || !ref.isValid()) {
                context.sendMessage(LightingInvalidateCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                return;
            }
            final Store<EntityStore> entityStore = ref.getStore();
            final TransformComponent transformComponent = entityStore.getComponent(ref, TransformComponent.getComponentType());
            assert transformComponent != null;
            final Vector3d position = transformComponent.getPosition();
            final long chunkIndex = ChunkUtil.indexChunkFromBlock((int)position.getX(), (int)position.getZ());
            final int chunkX = ChunkUtil.xOfChunkIndex(chunkIndex);
            final int chunkZ = ChunkUtil.zOfChunkIndex(chunkIndex);
            final Ref<ChunkStore> chunkReference = chunkStore.getChunkReference(chunkIndex);
            if (chunkReference == null || !chunkReference.isValid()) {
                final Message errorMessage = Message.translation("server.commands.errors.chunkNotLoaded").param("chunkX", chunkX).param("chunkZ", chunkZ).param("world", world.getName());
                context.sendMessage(errorMessage);
                return;
            }
            final Store<ChunkStore> chunkStoreStore = chunkStore.getStore();
            final BlockChunk blockChunkComponent = chunkStoreStore.getComponent(chunkReference, BlockChunk.getComponentType());
            assert blockChunkComponent != null;
            final int chunkY = MathUtil.floor(position.getY()) >> 5;
            final BlockSection section = blockChunkComponent.getSectionAtBlockY(chunkY);
            section.invalidateLocalLight();
            blockChunkComponent.invalidateChunkSection(chunkY);
            final Vector3i chunkPosition = new Vector3i(blockChunkComponent.getX(), chunkY, blockChunkComponent.getZ());
            chunkLighting.addToQueue(chunkPosition);
            context.sendMessage(Message.translation("server.commands.invalidatelighting.success").param("chunkPosition", chunkPosition.toString()));
        }
        else {
            chunkLighting.invalidateLoadedChunks();
            context.sendMessage(LightingInvalidateCommand.MESSAGE_COMMANDS_INVALIDATED_LIGHTING);
        }
    }
    
    static {
        MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        MESSAGE_COMMANDS_INVALIDATED_LIGHTING = Message.translation("server.commands.invalidatedlighting");
    }
}
