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

package com.hypixel.hytale.server.core.universe.world.lighting;

import com.hypixel.hytale.server.core.universe.world.chunk.section.BlockSection;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.server.core.universe.world.chunk.section.ChunkLightDataBuilder;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.math.vector.Vector3i;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;

public class FullBrightLightCalculation implements LightCalculation
{
    private final ChunkLightingManager chunkLightingManager;
    private LightCalculation delegate;
    
    public FullBrightLightCalculation(final ChunkLightingManager chunkLightingManager, final LightCalculation delegate) {
        this.chunkLightingManager = chunkLightingManager;
        this.delegate = delegate;
    }
    
    @Override
    public void init(@Nonnull final WorldChunk worldChunk) {
        this.delegate.init(worldChunk);
    }
    
    @Nonnull
    @Override
    public CalculationResult calculateLight(@Nonnull final Vector3i chunkPosition) {
        final CalculationResult result = this.delegate.calculateLight(chunkPosition);
        if (result == CalculationResult.DONE) {
            final WorldChunk worldChunk = this.chunkLightingManager.getWorld().getChunkIfInMemory(ChunkUtil.indexChunk(chunkPosition.x, chunkPosition.z));
            if (worldChunk == null) {
                return CalculationResult.NOT_LOADED;
            }
            this.setFullBright(worldChunk, chunkPosition.y);
        }
        return result;
    }
    
    @Override
    public boolean invalidateLightAtBlock(@Nonnull final WorldChunk worldChunk, final int blockX, final int blockY, final int blockZ, @Nonnull final BlockType blockType, final int oldHeight, final int newHeight) {
        final boolean handled = this.delegate.invalidateLightAtBlock(worldChunk, blockX, blockY, blockZ, blockType, oldHeight, newHeight);
        if (handled) {
            this.setFullBright(worldChunk, blockY >> 5);
        }
        return handled;
    }
    
    @Override
    public boolean invalidateLightInChunkSections(@Nonnull final WorldChunk worldChunk, final int sectionIndexFrom, final int sectionIndexTo) {
        final boolean handled = this.delegate.invalidateLightInChunkSections(worldChunk, sectionIndexFrom, sectionIndexTo);
        if (handled) {
            for (int y = sectionIndexTo - 1; y >= sectionIndexFrom; --y) {
                this.setFullBright(worldChunk, y);
            }
        }
        return handled;
    }
    
    public void setFullBright(@Nonnull final WorldChunk worldChunk, final int chunkY) {
        final BlockSection section = worldChunk.getBlockChunk().getSectionAtIndex(chunkY);
        final ChunkLightDataBuilder light = new ChunkLightDataBuilder(section.getGlobalChangeCounter());
        for (int i = 0; i < 32768; ++i) {
            light.setSkyLight(i, (byte)15);
        }
        section.setGlobalLight(light);
        if (BlockChunk.SEND_LOCAL_LIGHTING_DATA || BlockChunk.SEND_GLOBAL_LIGHTING_DATA) {
            worldChunk.getBlockChunk().invalidateChunkSection(chunkY);
        }
    }
}
