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.serialization.Codec;

public class DefaultSurfaceBuilder extends SurfaceBuilder<SurfaceBuilderBaseConfiguration> {
    public DefaultSurfaceBuilder(final Codec<SurfaceBuilderBaseConfiguration> codec) {
        super(codec);
    }
    
    @Override
    public void apply(final Random random, final ChunkAccess cgy, final Biome bre, final int integer4, final int integer5, final int integer6, final double double7, final BlockState cfj8, final BlockState cfj9, final int integer10, final long long11, final SurfaceBuilderBaseConfiguration cvx) {
        this.apply(random, cgy, bre, integer4, integer5, integer6, double7, cfj8, cfj9, cvx.getTopMaterial(), cvx.getUnderMaterial(), cvx.getUnderwaterMaterial(), integer10);
    }
    
    protected void apply(final Random random, final ChunkAccess cgy, final Biome bre, final int integer4, final int integer5, final int integer6, final double double7, final BlockState cfj8, final BlockState cfj9, final BlockState cfj10, final BlockState cfj11, final BlockState cfj12, final int integer13) {
        BlockState cfj13 = cfj10;
        BlockState cfj14 = cfj11;
        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 cfj15 = cgy.getBlockState(a18);
            if (cfj15.isAir()) {
                integer14 = -1;
            }
            else if (cfj15.is(cfj8.getBlock())) {
                if (integer14 == -1) {
                    if (integer15 <= 0) {
                        cfj13 = Blocks.AIR.defaultBlockState();
                        cfj14 = cfj8;
                    }
                    else if (integer18 >= integer13 - 4 && integer18 <= integer13 + 1) {
                        cfj13 = cfj10;
                        cfj14 = cfj11;
                    }
                    if (integer18 < integer13 && (cfj13 == null || cfj13.isAir())) {
                        if (bre.getTemperature(a18.set(integer4, integer18, integer5)) < 0.15f) {
                            cfj13 = Blocks.ICE.defaultBlockState();
                        }
                        else {
                            cfj13 = cfj9;
                        }
                        a18.set(integer16, integer18, integer17);
                    }
                    integer14 = integer15;
                    if (integer18 >= integer13 - 1) {
                        cgy.setBlockState(a18, cfj13, false);
                    }
                    else if (integer18 < integer13 - 7 - integer15) {
                        cfj13 = Blocks.AIR.defaultBlockState();
                        cfj14 = cfj8;
                        cgy.setBlockState(a18, cfj12, false);
                    }
                    else {
                        cgy.setBlockState(a18, cfj14, false);
                    }
                }
                else if (integer14 > 0) {
                    --integer14;
                    cgy.setBlockState(a18, cfj14, false);
                    if (integer14 == 0 && cfj14.is(Blocks.SAND) && integer15 > 1) {
                        integer14 = random.nextInt(4) + Math.max(0, integer18 - 63);
                        cfj14 = (cfj14.is(Blocks.RED_SAND) ? Blocks.RED_SANDSTONE.defaultBlockState() : Blocks.SANDSTONE.defaultBlockState());
                    }
                }
            }
        }
    }
}
