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

import org.apache.logging.log4j.LogManager;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
import net.minecraft.world.level.block.entity.BlockEntity;
import java.util.Iterator;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.block.entity.SpawnerBlockEntity;
import net.minecraft.world.level.block.entity.RandomizableContainerBlockEntity;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.levelgen.structure.StructurePiece;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.core.BlockPos;
import java.util.Random;
import net.minecraft.world.level.levelgen.ChunkGeneratorSettings;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.LevelAccessor;
import com.mojang.datafixers.Dynamic;
import java.util.function.Function;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.entity.EntityType;
import org.apache.logging.log4j.Logger;
import net.minecraft.world.level.levelgen.feature.configurations.NoneFeatureConfiguration;

public class MonsterRoomFeature extends Feature<NoneFeatureConfiguration> {
    private static final Logger LOGGER;
    private static final EntityType<?>[] MOBS;
    private static final BlockState AIR;
    
    public MonsterRoomFeature(final Function<Dynamic<?>, ? extends NoneFeatureConfiguration> function) {
        super(function);
    }
    
    @Override
    public boolean place(final LevelAccessor bjx, final ChunkGenerator<? extends ChunkGeneratorSettings> caa, final Random random, final BlockPos fk, final NoneFeatureConfiguration cgv) {
        final int integer7 = 3;
        final int integer8 = random.nextInt(2) + 2;
        final int integer9 = -integer8 - 1;
        final int integer10 = integer8 + 1;
        final int integer11 = -1;
        final int integer12 = 4;
        final int integer13 = random.nextInt(2) + 2;
        final int integer14 = -integer13 - 1;
        final int integer15 = integer13 + 1;
        int integer16 = 0;
        for (int integer17 = integer9; integer17 <= integer10; ++integer17) {
            for (int integer18 = -1; integer18 <= 4; ++integer18) {
                for (int integer19 = integer14; integer19 <= integer15; ++integer19) {
                    final BlockPos fk2 = fk.offset(integer17, integer18, integer19);
                    final Material coo21 = bjx.getBlockState(fk2).getMaterial();
                    final boolean boolean22 = coo21.isSolid();
                    if (integer18 == -1 && !boolean22) {
                        return false;
                    }
                    if (integer18 == 4 && !boolean22) {
                        return false;
                    }
                    if ((integer17 == integer9 || integer17 == integer10 || integer19 == integer14 || integer19 == integer15) && integer18 == 0 && bjx.isEmptyBlock(fk2) && bjx.isEmptyBlock(fk2.above())) {
                        ++integer16;
                    }
                }
            }
        }
        if (integer16 < 1 || integer16 > 5) {
            return false;
        }
        for (int integer17 = integer9; integer17 <= integer10; ++integer17) {
            for (int integer18 = 3; integer18 >= -1; --integer18) {
                for (int integer19 = integer14; integer19 <= integer15; ++integer19) {
                    final BlockPos fk2 = fk.offset(integer17, integer18, integer19);
                    if (integer17 == integer9 || integer18 == -1 || integer19 == integer14 || integer17 == integer10 || integer18 == 4 || integer19 == integer15) {
                        if (fk2.getY() >= 0 && !bjx.getBlockState(fk2.below()).getMaterial().isSolid()) {
                            bjx.setBlock(fk2, MonsterRoomFeature.AIR, 2);
                        }
                        else if (bjx.getBlockState(fk2).getMaterial().isSolid() && bjx.getBlockState(fk2).getBlock() != Blocks.CHEST) {
                            if (integer18 == -1 && random.nextInt(4) != 0) {
                                bjx.setBlock(fk2, Blocks.MOSSY_COBBLESTONE.defaultBlockState(), 2);
                            }
                            else {
                                bjx.setBlock(fk2, Blocks.COBBLESTONE.defaultBlockState(), 2);
                            }
                        }
                    }
                    else if (bjx.getBlockState(fk2).getBlock() != Blocks.CHEST) {
                        bjx.setBlock(fk2, MonsterRoomFeature.AIR, 2);
                    }
                }
            }
        }
        for (int integer17 = 0; integer17 < 2; ++integer17) {
            for (int integer18 = 0; integer18 < 3; ++integer18) {
                final int integer19 = fk.getX() + random.nextInt(integer8 * 2 + 1) - integer8;
                final int integer20 = fk.getY();
                final int integer21 = fk.getZ() + random.nextInt(integer13 * 2 + 1) - integer13;
                final BlockPos fk3 = new BlockPos(integer19, integer20, integer21);
                if (bjx.isEmptyBlock(fk3)) {
                    int integer22 = 0;
                    for (final Direction fp25 : Direction.Plane.HORIZONTAL) {
                        if (bjx.getBlockState(fk3.relative(fp25)).getMaterial().isSolid()) {
                            ++integer22;
                        }
                    }
                    if (integer22 == 1) {
                        bjx.setBlock(fk3, StructurePiece.reorient(bjx, fk3, Blocks.CHEST.defaultBlockState()), 2);
                        RandomizableContainerBlockEntity.setLootTable(bjx, random, fk3, BuiltInLootTables.SIMPLE_DUNGEON);
                        break;
                    }
                }
            }
        }
        bjx.setBlock(fk, Blocks.SPAWNER.defaultBlockState(), 2);
        final BlockEntity bwl17 = bjx.getBlockEntity(fk);
        if (bwl17 instanceof SpawnerBlockEntity) {
            ((SpawnerBlockEntity)bwl17).getSpawner().setEntityId(this.randomEntityId(random));
        }
        else {
            MonsterRoomFeature.LOGGER.error("Failed to fetch mob spawner entity at ({}, {}, {})", fk.getX(), fk.getY(), fk.getZ());
        }
        return true;
    }
    
    private EntityType<?> randomEntityId(final Random random) {
        return MonsterRoomFeature.MOBS[random.nextInt(MonsterRoomFeature.MOBS.length)];
    }
    
    static {
        LOGGER = LogManager.getLogger();
        MOBS = new EntityType[] { EntityType.SKELETON, EntityType.ZOMBIE, EntityType.ZOMBIE, EntityType.SPIDER };
        AIR = Blocks.CAVE_AIR.defaultBlockState();
    }
}
