package net.minecraft.world.level.levelgen.surfacebuilders;

import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.WorldgenRandom;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.chunk.ChunkAccess;
import java.util.Random;
import com.mojang.datafixers.Dynamic;
import java.util.function.Function;
import net.minecraft.world.level.levelgen.synth.PerlinNoise;
import net.minecraft.world.level.block.state.BlockState;

public class NetherSurfaceBuilder extends SurfaceBuilder<SurfaceBuilderBaseConfiguration> {
    private static final BlockState AIR;
    private static final BlockState NETHERRACK;
    private static final BlockState GRAVEL;
    private static final BlockState SOUL_SAND;
    protected long seed;
    protected PerlinNoise decorationNoise;
    
    public NetherSurfaceBuilder(final Function<Dynamic<?>, ? extends SurfaceBuilderBaseConfiguration> function) {
        super(function);
    }
    
    @Override
    public void apply(final Random random, final ChunkAccess bzy, final Biome bkt, final int integer4, final int integer5, final int integer6, final double double7, final BlockState byj8, final BlockState byj9, final int integer10, final long long11, final SurfaceBuilderBaseConfiguration cnh) {
        final int integer11 = integer10 + 1;
        final int integer12 = integer4 & 0xF;
        final int integer13 = integer5 & 0xF;
        final double double8 = 0.03125;
        final boolean boolean21 = this.decorationNoise.getValue(integer4 * 0.03125, integer5 * 0.03125, 0.0) * 75.0 + random.nextDouble() > 0.0;
        final boolean boolean22 = this.decorationNoise.getValue(integer4 * 0.03125, 109.0, integer5 * 0.03125) * 75.0 + random.nextDouble() > 0.0;
        final int integer14 = (int)(double7 / 3.0 + 3.0 + random.nextDouble() * 0.25);
        final BlockPos.MutableBlockPos a24 = new BlockPos.MutableBlockPos();
        int integer15 = -1;
        BlockState byj10 = NetherSurfaceBuilder.NETHERRACK;
        BlockState byj11 = NetherSurfaceBuilder.NETHERRACK;
        for (int integer16 = 127; integer16 >= 0; --integer16) {
            a24.set(integer12, integer16, integer13);
            final BlockState byj12 = bzy.getBlockState(a24);
            if (byj12.getBlock() == null || byj12.isAir()) {
                integer15 = -1;
            }
            else if (byj12.getBlock() == byj8.getBlock()) {
                if (integer15 == -1) {
                    if (integer14 <= 0) {
                        byj10 = NetherSurfaceBuilder.AIR;
                        byj11 = NetherSurfaceBuilder.NETHERRACK;
                    }
                    else if (integer16 >= integer11 - 4 && integer16 <= integer11 + 1) {
                        byj10 = NetherSurfaceBuilder.NETHERRACK;
                        byj11 = NetherSurfaceBuilder.NETHERRACK;
                        if (boolean22) {
                            byj10 = NetherSurfaceBuilder.GRAVEL;
                            byj11 = NetherSurfaceBuilder.NETHERRACK;
                        }
                        if (boolean21) {
                            byj10 = NetherSurfaceBuilder.SOUL_SAND;
                            byj11 = NetherSurfaceBuilder.SOUL_SAND;
                        }
                    }
                    if (integer16 < integer11 && (byj10 == null || byj10.isAir())) {
                        byj10 = byj9;
                    }
                    integer15 = integer14;
                    if (integer16 >= integer11 - 1) {
                        bzy.setBlockState(a24, byj10, false);
                    }
                    else {
                        bzy.setBlockState(a24, byj11, false);
                    }
                }
                else if (integer15 > 0) {
                    --integer15;
                    bzy.setBlockState(a24, byj11, false);
                }
            }
        }
    }
    
    @Override
    public void initNoise(final long long1) {
        if (this.seed != long1 || this.decorationNoise == null) {
            this.decorationNoise = new PerlinNoise(new WorldgenRandom(long1), 3, 0);
        }
        this.seed = long1;
    }
    
    static {
        AIR = Blocks.CAVE_AIR.defaultBlockState();
        NETHERRACK = Blocks.NETHERRACK.defaultBlockState();
        GRAVEL = Blocks.GRAVEL.defaultBlockState();
        SOUL_SAND = Blocks.SOUL_SAND.defaultBlockState();
    }
}
