package net.minecraft.world.level.lighting;

import net.minecraft.world.level.chunk.DataLayer;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.core.SectionPos;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.chunk.LightChunkGetter;
import javax.annotation.Nullable;

public class LevelLightEngine implements LightEventListener {
    @Nullable
    private final LayerLightEngine<?, ?> blockEngine;
    @Nullable
    private final LayerLightEngine<?, ?> skyEngine;
    
    public LevelLightEngine(final LightChunkGetter chl, final boolean boolean2, final boolean boolean3) {
        this.blockEngine = (boolean2 ? new BlockLightEngine(chl) : null);
        this.skyEngine = (boolean3 ? new SkyLightEngine(chl) : null);
    }
    
    public void checkBlock(final BlockPos fu) {
        if (this.blockEngine != null) {
            this.blockEngine.checkBlock(fu);
        }
        if (this.skyEngine != null) {
            this.skyEngine.checkBlock(fu);
        }
    }
    
    public void onBlockEmissionIncrease(final BlockPos fu, final int integer) {
        if (this.blockEngine != null) {
            this.blockEngine.onBlockEmissionIncrease(fu, integer);
        }
    }
    
    public boolean hasLightWork() {
        return (this.skyEngine != null && this.skyEngine.hasLightWork()) || (this.blockEngine != null && this.blockEngine.hasLightWork());
    }
    
    public int runUpdates(final int integer, final boolean boolean2, final boolean boolean3) {
        if (this.blockEngine != null && this.skyEngine != null) {
            final int integer2 = integer / 2;
            final int integer3 = this.blockEngine.runUpdates(integer2, boolean2, boolean3);
            final int integer4 = integer - integer2 + integer3;
            final int integer5 = this.skyEngine.runUpdates(integer4, boolean2, boolean3);
            if (integer3 == 0 && integer5 > 0) {
                return this.blockEngine.runUpdates(integer5, boolean2, boolean3);
            }
            return integer5;
        }
        else {
            if (this.blockEngine != null) {
                return this.blockEngine.runUpdates(integer, boolean2, boolean3);
            }
            if (this.skyEngine != null) {
                return this.skyEngine.runUpdates(integer, boolean2, boolean3);
            }
            return integer;
        }
    }
    
    @Override
    public void updateSectionStatus(final SectionPos go, final boolean boolean2) {
        if (this.blockEngine != null) {
            this.blockEngine.updateSectionStatus(go, boolean2);
        }
        if (this.skyEngine != null) {
            this.skyEngine.updateSectionStatus(go, boolean2);
        }
    }
    
    public void enableLightSources(final ChunkPos bph, final boolean boolean2) {
        if (this.blockEngine != null) {
            this.blockEngine.enableLightSources(bph, boolean2);
        }
        if (this.skyEngine != null) {
            this.skyEngine.enableLightSources(bph, boolean2);
        }
    }
    
    public LayerLightEventListener getLayerListener(final LightLayer bqi) {
        if (bqi == LightLayer.BLOCK) {
            if (this.blockEngine == null) {
                return LayerLightEventListener.DummyLightLayerEventListener.INSTANCE;
            }
            return this.blockEngine;
        }
        else {
            if (this.skyEngine == null) {
                return LayerLightEventListener.DummyLightLayerEventListener.INSTANCE;
            }
            return this.skyEngine;
        }
    }
    
    public String getDebugData(final LightLayer bqi, final SectionPos go) {
        if (bqi == LightLayer.BLOCK) {
            if (this.blockEngine != null) {
                return this.blockEngine.getDebugData(go.asLong());
            }
        }
        else if (this.skyEngine != null) {
            return this.skyEngine.getDebugData(go.asLong());
        }
        return "n/a";
    }
    
    public void queueSectionData(final LightLayer bqi, final SectionPos go, @Nullable final DataLayer chd, final boolean boolean4) {
        if (bqi == LightLayer.BLOCK) {
            if (this.blockEngine != null) {
                this.blockEngine.queueSectionData(go.asLong(), chd, boolean4);
            }
        }
        else if (this.skyEngine != null) {
            this.skyEngine.queueSectionData(go.asLong(), chd, boolean4);
        }
    }
    
    public void retainData(final ChunkPos bph, final boolean boolean2) {
        if (this.blockEngine != null) {
            this.blockEngine.retainData(bph, boolean2);
        }
        if (this.skyEngine != null) {
            this.skyEngine.retainData(bph, boolean2);
        }
    }
    
    public int getRawBrightness(final BlockPos fu, final int integer) {
        final int integer2 = (this.skyEngine == null) ? 0 : (this.skyEngine.getLightValue(fu) - integer);
        final int integer3 = (this.blockEngine == null) ? 0 : this.blockEngine.getLightValue(fu);
        return Math.max(integer3, integer2);
    }
}
