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

import java.util.Arrays;
import net.minecraft.world.level.levelgen.WorldgenRandom;
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.levelgen.synth.PerlinSimplexNoise;
import net.minecraft.world.level.block.state.BlockState;

public class BadlandsSurfaceBuilder extends SurfaceBuilder<SurfaceBuilderBaseConfiguration> {
    private static final BlockState WHITE_TERRACOTTA;
    private static final BlockState ORANGE_TERRACOTTA;
    private static final BlockState TERRACOTTA;
    private static final BlockState YELLOW_TERRACOTTA;
    private static final BlockState BROWN_TERRACOTTA;
    private static final BlockState RED_TERRACOTTA;
    private static final BlockState LIGHT_GRAY_TERRACOTTA;
    protected BlockState[] clayBands;
    protected long seed;
    protected PerlinSimplexNoise pillarNoise;
    protected PerlinSimplexNoise pillarRoofNoise;
    protected PerlinSimplexNoise clayBandsOffsetNoise;
    
    public BadlandsSurfaceBuilder(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) {
        final int integer11 = integer4 & 0xF;
        final int integer12 = integer5 & 0xF;
        BlockState byj10 = BadlandsSurfaceBuilder.WHITE_TERRACOTTA;
        BlockState byj11 = bkt.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 byj12 = bzy.getBlockState(a25);
                if (byj12.isAir()) {
                    integer14 = -1;
                }
                else if (byj12.getBlock() == byj8.getBlock()) {
                    if (integer14 == -1) {
                        boolean22 = false;
                        if (integer13 <= 0) {
                            byj10 = Blocks.AIR.defaultBlockState();
                            byj11 = byj8;
                        }
                        else if (integer16 >= integer10 - 4 && integer16 <= integer10 + 1) {
                            byj10 = BadlandsSurfaceBuilder.WHITE_TERRACOTTA;
                            byj11 = bkt.getSurfaceBuilderConfig().getUnderMaterial();
                        }
                        if (integer16 < integer10 && (byj10 == null || byj10.isAir())) {
                            byj10 = byj9;
                        }
                        integer14 = integer13 + Math.max(0, integer16 - integer10);
                        if (integer16 >= integer10 - 1) {
                            if (integer16 > integer10 + 3 + integer13) {
                                BlockState byj13;
                                if (integer16 < 64 || integer16 > 127) {
                                    byj13 = BadlandsSurfaceBuilder.ORANGE_TERRACOTTA;
                                }
                                else if (boolean21) {
                                    byj13 = BadlandsSurfaceBuilder.TERRACOTTA;
                                }
                                else {
                                    byj13 = this.getBand(integer4, integer16, integer5);
                                }
                                bzy.setBlockState(a25, byj13, false);
                            }
                            else {
                                bzy.setBlockState(a25, bkt.getSurfaceBuilderConfig().getTopMaterial(), false);
                                boolean22 = true;
                            }
                        }
                        else {
                            bzy.setBlockState(a25, byj11, false);
                            final Block bph28 = byj11.getBlock();
                            if (bph28 == Blocks.WHITE_TERRACOTTA || bph28 == Blocks.ORANGE_TERRACOTTA || bph28 == Blocks.MAGENTA_TERRACOTTA || bph28 == Blocks.LIGHT_BLUE_TERRACOTTA || bph28 == Blocks.YELLOW_TERRACOTTA || bph28 == Blocks.LIME_TERRACOTTA || bph28 == Blocks.PINK_TERRACOTTA || bph28 == Blocks.GRAY_TERRACOTTA || bph28 == Blocks.LIGHT_GRAY_TERRACOTTA || bph28 == Blocks.CYAN_TERRACOTTA || bph28 == Blocks.PURPLE_TERRACOTTA || bph28 == Blocks.BLUE_TERRACOTTA || bph28 == Blocks.BROWN_TERRACOTTA || bph28 == Blocks.GREEN_TERRACOTTA || bph28 == Blocks.RED_TERRACOTTA || bph28 == Blocks.BLACK_TERRACOTTA) {
                                bzy.setBlockState(a25, BadlandsSurfaceBuilder.ORANGE_TERRACOTTA, false);
                            }
                        }
                    }
                    else if (integer14 > 0) {
                        --integer14;
                        if (boolean22) {
                            bzy.setBlockState(a25, BadlandsSurfaceBuilder.ORANGE_TERRACOTTA, false);
                        }
                        else {
                            bzy.setBlockState(a25, this.getBand(integer4, integer16, integer5), false);
                        }
                    }
                    ++integer15;
                }
            }
        }
    }
    
    @Override
    public void initNoise(final long long1) {
        if (this.seed != long1 || this.clayBands == null) {
            this.generateBands(long1);
        }
        if (this.seed != long1 || this.pillarNoise == null || this.pillarRoofNoise == null) {
            final WorldgenRandom ccf4 = new WorldgenRandom(long1);
            this.pillarNoise = new PerlinSimplexNoise(ccf4, 3, 0);
            this.pillarRoofNoise = new PerlinSimplexNoise(ccf4, 0, 0);
        }
        this.seed = long1;
    }
    
    protected void generateBands(final long long1) {
        Arrays.fill(this.clayBands = new BlockState[64], BadlandsSurfaceBuilder.TERRACOTTA);
        final WorldgenRandom ccf4 = new WorldgenRandom(long1);
        this.clayBandsOffsetNoise = new PerlinSimplexNoise(ccf4, 0, 0);
        for (int integer5 = 0; integer5 < 64; ++integer5) {
            integer5 += ccf4.nextInt(5) + 1;
            if (integer5 < 64) {
                this.clayBands[integer5] = BadlandsSurfaceBuilder.ORANGE_TERRACOTTA;
            }
        }
        for (int integer5 = ccf4.nextInt(4) + 2, integer6 = 0; integer6 < integer5; ++integer6) {
            for (int integer7 = ccf4.nextInt(3) + 1, integer8 = ccf4.nextInt(64), integer9 = 0; integer8 + integer9 < 64 && integer9 < integer7; ++integer9) {
                this.clayBands[integer8 + integer9] = BadlandsSurfaceBuilder.YELLOW_TERRACOTTA;
            }
        }
        for (int integer6 = ccf4.nextInt(4) + 2, integer7 = 0; integer7 < integer6; ++integer7) {
            for (int integer8 = ccf4.nextInt(3) + 2, integer9 = ccf4.nextInt(64), integer10 = 0; integer9 + integer10 < 64 && integer10 < integer8; ++integer10) {
                this.clayBands[integer9 + integer10] = BadlandsSurfaceBuilder.BROWN_TERRACOTTA;
            }
        }
        for (int integer7 = ccf4.nextInt(4) + 2, integer8 = 0; integer8 < integer7; ++integer8) {
            for (int integer9 = ccf4.nextInt(3) + 1, integer10 = ccf4.nextInt(64), integer11 = 0; integer10 + integer11 < 64 && integer11 < integer9; ++integer11) {
                this.clayBands[integer10 + integer11] = BadlandsSurfaceBuilder.RED_TERRACOTTA;
            }
        }
        int integer8 = ccf4.nextInt(3) + 3;
        int integer9 = 0;
        for (int integer10 = 0; integer10 < integer8; ++integer10) {
            final int integer11 = 1;
            integer9 += ccf4.nextInt(16) + 4;
            for (int integer12 = 0; integer9 + integer12 < 64 && integer12 < 1; ++integer12) {
                this.clayBands[integer9 + integer12] = BadlandsSurfaceBuilder.WHITE_TERRACOTTA;
                if (integer9 + integer12 > 1 && ccf4.nextBoolean()) {
                    this.clayBands[integer9 + integer12 - 1] = BadlandsSurfaceBuilder.LIGHT_GRAY_TERRACOTTA;
                }
                if (integer9 + integer12 < 63 && ccf4.nextBoolean()) {
                    this.clayBands[integer9 + integer12 + 1] = BadlandsSurfaceBuilder.LIGHT_GRAY_TERRACOTTA;
                }
            }
        }
    }
    
    protected BlockState getBand(final int integer1, final int integer2, final int integer3) {
        final int integer4 = (int)Math.round(this.clayBandsOffsetNoise.getValue(integer1 / 512.0, integer3 / 512.0, false) * 2.0);
        return this.clayBands[(integer2 + integer4 + 64) % 64];
    }
    
    static {
        WHITE_TERRACOTTA = Blocks.WHITE_TERRACOTTA.defaultBlockState();
        ORANGE_TERRACOTTA = Blocks.ORANGE_TERRACOTTA.defaultBlockState();
        TERRACOTTA = Blocks.TERRACOTTA.defaultBlockState();
        YELLOW_TERRACOTTA = Blocks.YELLOW_TERRACOTTA.defaultBlockState();
        BROWN_TERRACOTTA = Blocks.BROWN_TERRACOTTA.defaultBlockState();
        RED_TERRACOTTA = Blocks.RED_TERRACOTTA.defaultBlockState();
        LIGHT_GRAY_TERRACOTTA = Blocks.LIGHT_GRAY_TERRACOTTA.defaultBlockState();
    }
}
