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

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

public class ErodedBadlandsSurfaceBuilder extends BadlandsSurfaceBuilder {
    private static final BlockState WHITE_TERRACOTTA;
    private static final BlockState ORANGE_TERRACOTTA;
    private static final BlockState TERRACOTTA;
    
    public ErodedBadlandsSurfaceBuilder(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) {
        double double8 = 0.0;
        final double double9 = Math.min(Math.abs(double7), this.pillarNoise.getValue(integer4 * 0.25, integer5 * 0.25, false) * 15.0);
        if (double9 > 0.0) {
            final double double10 = 0.001953125;
            final double double11 = Math.abs(this.pillarRoofNoise.getValue(integer4 * 0.001953125, integer5 * 0.001953125, false));
            double8 = double9 * double9 * 2.5;
            final double double12 = Math.ceil(double11 * 50.0) + 14.0;
            if (double8 > double12) {
                double8 = double12;
            }
            double8 += 64.0;
        }
        final int integer11 = integer4 & 0xF;
        final int integer12 = integer5 & 0xF;
        BlockState byj10 = ErodedBadlandsSurfaceBuilder.WHITE_TERRACOTTA;
        BlockState byj11 = bkt.getSurfaceBuilderConfig().getUnderMaterial();
        final int integer13 = (int)(double7 / 3.0 + 3.0 + random.nextDouble() * 0.25);
        final boolean boolean25 = Math.cos(double7 / 3.0 * 3.141592653589793) > 0.0;
        int integer14 = -1;
        boolean boolean26 = false;
        final BlockPos.MutableBlockPos a28 = new BlockPos.MutableBlockPos();
        for (int integer15 = Math.max(integer6, (int)double8 + 1); integer15 >= 0; --integer15) {
            a28.set(integer11, integer15, integer12);
            if (bzy.getBlockState(a28).isAir() && integer15 < (int)double8) {
                bzy.setBlockState(a28, byj8, false);
            }
            final BlockState byj12 = bzy.getBlockState(a28);
            if (byj12.isAir()) {
                integer14 = -1;
            }
            else if (byj12.getBlock() == byj8.getBlock()) {
                if (integer14 == -1) {
                    boolean26 = false;
                    if (integer13 <= 0) {
                        byj10 = Blocks.AIR.defaultBlockState();
                        byj11 = byj8;
                    }
                    else if (integer15 >= integer10 - 4 && integer15 <= integer10 + 1) {
                        byj10 = ErodedBadlandsSurfaceBuilder.WHITE_TERRACOTTA;
                        byj11 = bkt.getSurfaceBuilderConfig().getUnderMaterial();
                    }
                    if (integer15 < integer10 && (byj10 == null || byj10.isAir())) {
                        byj10 = byj9;
                    }
                    integer14 = integer13 + Math.max(0, integer15 - integer10);
                    if (integer15 >= integer10 - 1) {
                        if (integer15 > integer10 + 3 + integer13) {
                            BlockState byj13;
                            if (integer15 < 64 || integer15 > 127) {
                                byj13 = ErodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA;
                            }
                            else if (boolean25) {
                                byj13 = ErodedBadlandsSurfaceBuilder.TERRACOTTA;
                            }
                            else {
                                byj13 = this.getBand(integer4, integer15, integer5);
                            }
                            bzy.setBlockState(a28, byj13, false);
                        }
                        else {
                            bzy.setBlockState(a28, bkt.getSurfaceBuilderConfig().getTopMaterial(), false);
                            boolean26 = true;
                        }
                    }
                    else {
                        bzy.setBlockState(a28, byj11, false);
                        final Block bph31 = byj11.getBlock();
                        if (bph31 == Blocks.WHITE_TERRACOTTA || bph31 == Blocks.ORANGE_TERRACOTTA || bph31 == Blocks.MAGENTA_TERRACOTTA || bph31 == Blocks.LIGHT_BLUE_TERRACOTTA || bph31 == Blocks.YELLOW_TERRACOTTA || bph31 == Blocks.LIME_TERRACOTTA || bph31 == Blocks.PINK_TERRACOTTA || bph31 == Blocks.GRAY_TERRACOTTA || bph31 == Blocks.LIGHT_GRAY_TERRACOTTA || bph31 == Blocks.CYAN_TERRACOTTA || bph31 == Blocks.PURPLE_TERRACOTTA || bph31 == Blocks.BLUE_TERRACOTTA || bph31 == Blocks.BROWN_TERRACOTTA || bph31 == Blocks.GREEN_TERRACOTTA || bph31 == Blocks.RED_TERRACOTTA || bph31 == Blocks.BLACK_TERRACOTTA) {
                            bzy.setBlockState(a28, ErodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA, false);
                        }
                    }
                }
                else if (integer14 > 0) {
                    --integer14;
                    if (boolean26) {
                        bzy.setBlockState(a28, ErodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA, false);
                    }
                    else {
                        bzy.setBlockState(a28, this.getBand(integer4, integer15, integer5), false);
                    }
                }
            }
        }
    }
    
    static {
        WHITE_TERRACOTTA = Blocks.WHITE_TERRACOTTA.defaultBlockState();
        ORANGE_TERRACOTTA = Blocks.ORANGE_TERRACOTTA.defaultBlockState();
        TERRACOTTA = Blocks.TERRACOTTA.defaultBlockState();
    }
}
