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

import net.minecraft.world.level.block.Blocks;
import net.minecraft.core.Vec3i;
import net.minecraft.world.level.StructureFeatureManager;
import net.minecraft.world.level.WorldGenLevel;
import java.util.List;
import net.minecraft.world.level.levelgen.structure.StructurePiece;
import java.util.Collection;
import net.minecraft.world.level.levelgen.structure.WoodlandMansionPieces;
import com.google.common.collect.Lists;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.levelgen.Heightmap;
import java.util.Random;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.levelgen.structure.StructureStart;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import java.util.Iterator;
import java.util.Set;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.WorldgenRandom;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.chunk.ChunkGenerator;
import com.mojang.serialization.Codec;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;

public class WoodlandMansionFeature extends StructureFeature<NoneFeatureConfiguration> {
    public WoodlandMansionFeature(final Codec<NoneFeatureConfiguration> codec) {
        super(codec);
    }
    
    @Override
    protected boolean linearSeparation() {
        return false;
    }
    
    @Override
    protected boolean isFeatureChunk(final ChunkGenerator cha, final BiomeSource brh, final long long3, final WorldgenRandom ciy, final int integer5, final int integer6, final Biome bre, final ChunkPos bph, final NoneFeatureConfiguration coa) {
        final Set<Biome> set12 = brh.getBiomesWithin(integer5 * 16 + 9, cha.getSeaLevel(), integer6 * 16 + 9, 32);
        for (final Biome bre2 : set12) {
            if (!bre2.isValidStart(this)) {
                return false;
            }
        }
        return true;
    }
    
    @Override
    public StructureStartFactory<NoneFeatureConfiguration> getStartFactory() {
        return WoodlandMansionStart::new;
    }
    
    public static class WoodlandMansionStart extends StructureStart<NoneFeatureConfiguration> {
        public WoodlandMansionStart(final StructureFeature<NoneFeatureConfiguration> cml, final int integer2, final int integer3, final BoundingBox ctd, final int integer5, final long long6) {
            super(cml, integer2, integer3, ctd, integer5, long6);
        }
        
        @Override
        public void generatePieces(final ChunkGenerator cha, final StructureManager cva, final int integer3, final int integer4, final Biome bre, final NoneFeatureConfiguration coa) {
            final Rotation cap8 = Rotation.getRandom(this.random);
            int integer5 = 5;
            int integer6 = 5;
            if (cap8 == Rotation.CLOCKWISE_90) {
                integer5 = -5;
            }
            else if (cap8 == Rotation.CLOCKWISE_180) {
                integer5 = -5;
                integer6 = -5;
            }
            else if (cap8 == Rotation.COUNTERCLOCKWISE_90) {
                integer6 = -5;
            }
            final int integer7 = (integer3 << 4) + 7;
            final int integer8 = (integer4 << 4) + 7;
            final int integer9 = cha.getFirstOccupiedHeight(integer7, integer8, Heightmap.Types.WORLD_SURFACE_WG);
            final int integer10 = cha.getFirstOccupiedHeight(integer7, integer8 + integer6, Heightmap.Types.WORLD_SURFACE_WG);
            final int integer11 = cha.getFirstOccupiedHeight(integer7 + integer5, integer8, Heightmap.Types.WORLD_SURFACE_WG);
            final int integer12 = cha.getFirstOccupiedHeight(integer7 + integer5, integer8 + integer6, Heightmap.Types.WORLD_SURFACE_WG);
            final int integer13 = Math.min(Math.min(integer9, integer10), Math.min(integer11, integer12));
            if (integer13 < 60) {
                return;
            }
            final BlockPos fu18 = new BlockPos(integer3 * 16 + 8, integer13 + 1, integer4 * 16 + 8);
            final List<WoodlandMansionPieces.WoodlandMansionPiece> list19 = Lists.newLinkedList();
            WoodlandMansionPieces.generateMansion(cva, fu18, cap8, list19, this.random);
            this.pieces.addAll(list19);
            this.calculateBoundingBox();
        }
        
        @Override
        public void placeInChunk(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BoundingBox ctd, final ChunkPos bph) {
            super.placeInChunk(bqu, bqq, cha, random, ctd, bph);
            final int integer8 = this.boundingBox.y0;
            for (int integer9 = ctd.x0; integer9 <= ctd.x1; ++integer9) {
                for (int integer10 = ctd.z0; integer10 <= ctd.z1; ++integer10) {
                    final BlockPos fu11 = new BlockPos(integer9, integer8, integer10);
                    if (!bqu.isEmptyBlock(fu11) && this.boundingBox.isInside(fu11)) {
                        boolean boolean12 = false;
                        for (final StructurePiece cty14 : this.pieces) {
                            if (cty14.getBoundingBox().isInside(fu11)) {
                                boolean12 = true;
                                break;
                            }
                        }
                        if (boolean12) {
                            for (int integer11 = integer8 - 1; integer11 > 1; --integer11) {
                                final BlockPos fu12 = new BlockPos(integer9, integer11, integer10);
                                if (!bqu.isEmptyBlock(fu12) && !bqu.getBlockState(fu12).getMaterial().isLiquid()) {
                                    break;
                                }
                                bqu.setBlock(fu12, Blocks.COBBLESTONE.defaultBlockState(), 2);
                            }
                        }
                    }
                }
            }
        }
    }
}
