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

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 WoodedBadlandsSurfaceBuilder extends BadlandsSurfaceBuilder {
    private static final BlockState WHITE_TERRACOTTA;
    private static final BlockState ORANGE_TERRACOTTA;
    private static final BlockState TERRACOTTA;
    
    public WoodedBadlandsSurfaceBuilder(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) {
        final int integer11 = integer4 & 0xF;
        final int integer12 = integer5 & 0xF;
        BlockState cfj10 = WoodedBadlandsSurfaceBuilder.WHITE_TERRACOTTA;
        BlockState cfj11 = bre.getSurfaceBuilderConfig().getUnderMaterial();
        final int integer13 = (int)(double7 / 3.0 + 3.0 + random.nextDouble() * 0.25);
        final boolean boolean21 = Math.cos(double7 / 3.0 * 3.141592653589793) > 0.0;
        int integer14 = -1;
        boolean boolean22 = false;
        int integer15 = 0;
        final BlockPos.MutableBlockPos a25 = new BlockPos.MutableBlockPos();
        for (int integer16 = integer6; integer16 >= 0; --integer16) {
            if (integer15 < 15) {
                a25.set(integer11, integer16, integer12);
                final BlockState cfj12 = cgy.getBlockState(a25);
                if (cfj12.isAir()) {
                    integer14 = -1;
                }
                else if (cfj12.is(cfj8.getBlock())) {
                    if (integer14 == -1) {
                        boolean22 = false;
                        if (integer13 <= 0) {
                            cfj10 = Blocks.AIR.defaultBlockState();
                            cfj11 = cfj8;
                        }
                        else if (integer16 >= integer10 - 4 && integer16 <= integer10 + 1) {
                            cfj10 = WoodedBadlandsSurfaceBuilder.WHITE_TERRACOTTA;
                            cfj11 = bre.getSurfaceBuilderConfig().getUnderMaterial();
                        }
                        if (integer16 < integer10 && (cfj10 == null || cfj10.isAir())) {
                            cfj10 = cfj9;
                        }
                        integer14 = integer13 + Math.max(0, integer16 - integer10);
                        if (integer16 >= integer10 - 1) {
                            if (integer16 > 86 + integer13 * 2) {
                                if (boolean21) {
                                    cgy.setBlockState(a25, Blocks.COARSE_DIRT.defaultBlockState(), false);
                                }
                                else {
                                    cgy.setBlockState(a25, Blocks.GRASS_BLOCK.defaultBlockState(), false);
                                }
                            }
                            else if (integer16 > integer10 + 3 + integer13) {
                                BlockState cfj13;
                                if (integer16 < 64 || integer16 > 127) {
                                    cfj13 = WoodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA;
                                }
                                else if (boolean21) {
                                    cfj13 = WoodedBadlandsSurfaceBuilder.TERRACOTTA;
                                }
                                else {
                                    cfj13 = this.getBand(integer4, integer16, integer5);
                                }
                                cgy.setBlockState(a25, cfj13, false);
                            }
                            else {
                                cgy.setBlockState(a25, bre.getSurfaceBuilderConfig().getTopMaterial(), false);
                                boolean22 = true;
                            }
                        }
                        else {
                            cgy.setBlockState(a25, cfj11, false);
                            if (cfj11 == WoodedBadlandsSurfaceBuilder.WHITE_TERRACOTTA) {
                                cgy.setBlockState(a25, WoodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA, false);
                            }
                        }
                    }
                    else if (integer14 > 0) {
                        --integer14;
                        if (boolean22) {
                            cgy.setBlockState(a25, WoodedBadlandsSurfaceBuilder.ORANGE_TERRACOTTA, false);
                        }
                        else {
                            cgy.setBlockState(a25, this.getBand(integer4, integer16, integer5), false);
                        }
                    }
                    ++integer15;
                }
            }
        }
    }
    
    static {
        WHITE_TERRACOTTA = Blocks.WHITE_TERRACOTTA.defaultBlockState();
        ORANGE_TERRACOTTA = Blocks.ORANGE_TERRACOTTA.defaultBlockState();
        TERRACOTTA = Blocks.TERRACOTTA.defaultBlockState();
    }
}
