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

import net.minecraft.world.level.block.Blocks;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
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;

public class DefaultSurfaceBuilder extends SurfaceBuilder<SurfaceBuilderBaseConfiguration> {
    public DefaultSurfaceBuilder(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) {
        this.apply(random, bzy, bkt, integer4, integer5, integer6, double7, byj8, byj9, cnh.getTopMaterial(), cnh.getUnderMaterial(), cnh.getUnderwaterMaterial(), integer10);
    }
    
    protected 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 BlockState byj10, final BlockState byj11, final BlockState byj12, final int integer13) {
        BlockState byj13 = byj10;
        BlockState byj14 = byj11;
        final BlockPos.MutableBlockPos a18 = new BlockPos.MutableBlockPos();
        int integer14 = -1;
        final int integer15 = (int)(double7 / 3.0 + 3.0 + random.nextDouble() * 0.25);
        final int integer16 = integer4 & 0xF;
        final int integer17 = integer5 & 0xF;
        for (int integer18 = integer6; integer18 >= 0; --integer18) {
            a18.set(integer16, integer18, integer17);
            final BlockState byj15 = bzy.getBlockState(a18);
            if (byj15.isAir()) {
                integer14 = -1;
            }
            else if (byj15.getBlock() == byj8.getBlock()) {
                if (integer14 == -1) {
                    if (integer15 <= 0) {
                        byj13 = Blocks.AIR.defaultBlockState();
                        byj14 = byj8;
                    }
                    else if (integer18 >= integer13 - 4 && integer18 <= integer13 + 1) {
                        byj13 = byj10;
                        byj14 = byj11;
                    }
                    if (integer18 < integer13 && (byj13 == null || byj13.isAir())) {
                        if (bkt.getTemperature(a18.set(integer4, integer18, integer5)) < 0.15f) {
                            byj13 = Blocks.ICE.defaultBlockState();
                        }
                        else {
                            byj13 = byj9;
                        }
                        a18.set(integer16, integer18, integer17);
                    }
                    integer14 = integer15;
                    if (integer18 >= integer13 - 1) {
                        bzy.setBlockState(a18, byj13, false);
                    }
                    else if (integer18 < integer13 - 7 - integer15) {
                        byj13 = Blocks.AIR.defaultBlockState();
                        byj14 = byj8;
                        bzy.setBlockState(a18, byj12, false);
                    }
                    else {
                        bzy.setBlockState(a18, byj14, false);
                    }
                }
                else if (integer14 > 0) {
                    --integer14;
                    bzy.setBlockState(a18, byj14, false);
                    if (integer14 == 0 && byj14.getBlock() == Blocks.SAND && integer15 > 1) {
                        integer14 = random.nextInt(4) + Math.max(0, integer18 - 63);
                        byj14 = ((byj14.getBlock() == Blocks.RED_SAND) ? Blocks.RED_SANDSTONE.defaultBlockState() : Blocks.SANDSTONE.defaultBlockState());
                    }
                }
            }
        }
    }
}
