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.serialization.Codec;
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 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) {
        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 cfj10 = ErodedBadlandsSurfaceBuilder.WHITE_TERRACOTTA;
        BlockState cfj11 = bre.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 (cgy.getBlockState(a28).isAir() && integer15 < (int)double8) {
                cgy.setBlockState(a28, cfj8, false);
            }
            final BlockState cfj12 = cgy.getBlockState(a28);
            if (cfj12.isAir()) {
                integer14 = -1;
            }
            else if (cfj12.is(cfj8.getBlock())) {
                if (integer14 == -1) {
                    boolean26 = false;
                    if (integer13 <= 0) {
                        cfj10 = Blocks.AIR.defaultBlockState();
                        cfj11 = cfj8;
                    }
                    else if (integer15 >= integer10 - 4 && integer15 <= integer10 + 1) {
                        cfj10 = ErodedBadlandsSurfaceBuilder.WHITE_TERRACOTTA;
                        cfj11 = bre.getSurfaceBuilderConfig().getUnderMaterial();
                    }
                    if (integer15 < integer10 && (cfj10 == null || cfj10.isAir())) {
                        cfj10 = cfj9;
                    }
                    integer14 = integer13 + Math.max(0, integer15 - integer10);
                    if (integer15 >= integer10 - 1) {
                        if (integer15 > integer10 + 3 + integer13) {
                            BlockState cfj13;
                            if (integer15 < 64 || integer15 > 127) {
                                cfj13 = ErodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA;
                            }
                            else if (boolean25) {
                                cfj13 = ErodedBadlandsSurfaceBuilder.TERRACOTTA;
                            }
                            else {
                                cfj13 = this.getBand(integer4, integer15, integer5);
                            }
                            cgy.setBlockState(a28, cfj13, false);
                        }
                        else {
                            cgy.setBlockState(a28, bre.getSurfaceBuilderConfig().getTopMaterial(), false);
                            boolean26 = true;
                        }
                    }
                    else {
                        cgy.setBlockState(a28, cfj11, false);
                        final Block bvr31 = cfj11.getBlock();
                        if (bvr31 == Blocks.WHITE_TERRACOTTA || bvr31 == Blocks.ORANGE_TERRACOTTA || bvr31 == Blocks.MAGENTA_TERRACOTTA || bvr31 == Blocks.LIGHT_BLUE_TERRACOTTA || bvr31 == Blocks.YELLOW_TERRACOTTA || bvr31 == Blocks.LIME_TERRACOTTA || bvr31 == Blocks.PINK_TERRACOTTA || bvr31 == Blocks.GRAY_TERRACOTTA || bvr31 == Blocks.LIGHT_GRAY_TERRACOTTA || bvr31 == Blocks.CYAN_TERRACOTTA || bvr31 == Blocks.PURPLE_TERRACOTTA || bvr31 == Blocks.BLUE_TERRACOTTA || bvr31 == Blocks.BROWN_TERRACOTTA || bvr31 == Blocks.GREEN_TERRACOTTA || bvr31 == Blocks.RED_TERRACOTTA || bvr31 == Blocks.BLACK_TERRACOTTA) {
                            cgy.setBlockState(a28, ErodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA, false);
                        }
                    }
                }
                else if (integer14 > 0) {
                    --integer14;
                    if (boolean26) {
                        cgy.setBlockState(a28, ErodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA, false);
                    }
                    else {
                        cgy.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();
    }
}
