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

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

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 java.util.concurrent.TimeUnit;
import com.hypixel.hytale.server.core.HytaleServer;
import java.util.concurrent.ScheduledFuture;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.math.vector.Vector2i;
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.RelativeChunkPosition;
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 ChunkFixHeightMapCommand extends AbstractWorldCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_STARTED;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_DONE;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_INVALIDATING_LIGHTING;
    @Nonnull
    private final RequiredArg<RelativeChunkPosition> chunkPosArg;
    
    public ChunkFixHeightMapCommand() {
        super("fixheight", "server.commands.chunk.fixheight.desc");
        this.chunkPosArg = this.withRequiredArg("x z", "server.commands.chunk.fixheight.position.desc", ArgTypes.RELATIVE_CHUNK_POSITION);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final RelativeChunkPosition chunkPosition = this.chunkPosArg.get(context);
        final Vector2i position = chunkPosition.getChunkPosition(context, store);
        fixHeightMap(context, world, position.x, position.y);
    }
    
    private static void fixHeightMap(@Nonnull final CommandContext context, @Nonnull final World world, final int chunkX, final int chunkZ) {
        final ChunkLightingManager chunkLighting = world.getChunkLighting();
        final ChunkStore chunkStore = world.getChunkStore();
        final Store<ChunkStore> chunkStoreStore = chunkStore.getStore();
        final long chunkIndex = ChunkUtil.indexChunk(chunkX, chunkZ);
        final Ref<ChunkStore> chunkRef = chunkStore.getChunkReference(chunkIndex);
        if (chunkRef == null || !chunkRef.isValid()) {
            context.sendMessage(Message.translation("server.commands.errors.chunkNotLoaded").param("chunkX", chunkX).param("chunkZ", chunkZ).param("world", world.getName()));
            return;
        }
        final WorldChunk worldChunkComponent = chunkStoreStore.getComponent(chunkRef, WorldChunk.getComponentType());
        assert worldChunkComponent != null;
        final BlockChunk blockChunkComponent = chunkStoreStore.getComponent(chunkRef, BlockChunk.getComponentType());
        assert blockChunkComponent != null;
        context.sendMessage(ChunkFixHeightMapCommand.MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_STARTED);
        blockChunkComponent.updateHeightmap();
        context.sendMessage(ChunkFixHeightMapCommand.MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_DONE);
        context.sendMessage(ChunkFixHeightMapCommand.MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_INVALIDATING_LIGHTING);
        for (int chunkSectionY = 0; chunkSectionY < 10; ++chunkSectionY) {
            blockChunkComponent.getSectionAtIndex(chunkSectionY).invalidateLocalLight();
        }
        chunkLighting.invalidateLightInChunk(worldChunkComponent);
        context.sendMessage(ChunkFixHeightMapCommand.MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_DONE);
        context.sendMessage(Message.translation("server.commands.chunk.fixHeightMap.waitingForLighting").param("x", chunkX).param("z", chunkZ));
        final int[] count = { 0 };
        final ScheduledFuture<?>[] scheduledFuture = { HytaleServer.SCHEDULED_EXECUTOR.scheduleWithFixedDelay(() -> {
                if (chunkLighting.isQueued(chunkX, chunkZ)) {
                    if (count[0]++ > 60) {
                        scheduledFuture[0].cancel(false);
                        context.sendMessage(Message.translation("server.commands.chunk.fixHeightMap.lightingError").param("x", chunkX).param("z", chunkZ));
                    }
                }
                else {
                    world.getNotificationHandler().updateChunk(chunkIndex);
                    context.sendMessage(Message.translation("server.commands.chunk.fixHeightMap.lightingFinished").param("x", chunkX).param("z", chunkZ));
                    scheduledFuture[0].cancel(false);
                }
            }, 1L, 1L, TimeUnit.SECONDS) };
    }
    
    static {
        MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_STARTED = Message.translation("server.commands.chunk.fixHeightMap.started");
        MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_DONE = Message.translation("server.commands.chunk.fixHeightMap.done");
        MESSAGE_COMMANDS_CHUNK_FIXHEIGHTMAP_INVALIDATING_LIGHTING = Message.translation("server.commands.chunk.fixHeightMap.invalidatingLighting");
    }
}
