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

import java.util.Iterator;
import net.minecraft.world.level.levelgen.structure.StructurePiece;
import java.util.Random;
import net.minecraft.world.level.levelgen.structure.MineShaftPieces;
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 java.util.stream.Collectors;
import java.util.Arrays;
import java.util.function.Function;
import java.util.Map;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfiguration;
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.MineshaftConfiguration;

public class MineshaftFeature extends StructureFeature<MineshaftConfiguration> {
    public MineshaftFeature(final Codec<MineshaftConfiguration> codec) {
        super(codec);
    }
    
    @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 MineshaftConfiguration cnw) {
        ciy.setLargeFeatureSeed(long3, integer5, integer6);
        final double double12 = cnw.probability;
        return ciy.nextDouble() < double12;
    }
    
    @Override
    public StructureStartFactory<MineshaftConfiguration> getStartFactory() {
        return MineShaftStart::new;
    }
    
    public enum Type implements StringRepresentable {
        NORMAL("normal"), 
        MESA("mesa");
        
        public static final Codec<Type> CODEC;
        private static final Map<String, Type> BY_NAME;
        private final String name;
        
        private Type(final String string3) {
            this.name = string3;
        }
        
        public String getName() {
            return this.name;
        }
        
        private static Type byName(final String string) {
            return Type.BY_NAME.get(string);
        }
        
        public static Type byId(final int integer) {
            if (integer < 0 || integer >= values().length) {
                return Type.NORMAL;
            }
            return values()[integer];
        }
        
        @Override
        public String getSerializedName() {
            return this.name;
        }
        
        static {
            CODEC = StringRepresentable.<Type>fromEnum(Type::values, Type::byName);
            BY_NAME = Arrays.<Type>stream(values()).collect(Collectors.toMap(Type::getName, b -> b));
        }
    }
    
    public static class MineShaftStart extends StructureStart<MineshaftConfiguration> {
        public MineShaftStart(final StructureFeature<MineshaftConfiguration> 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 MineshaftConfiguration cnw) {
            final MineShaftPieces.MineShaftRoom d8 = new MineShaftPieces.MineShaftRoom(0, this.random, (integer3 << 4) + 2, (integer4 << 4) + 2, cnw.type);
            this.pieces.add(d8);
            d8.addChildren(d8, this.pieces, this.random);
            this.calculateBoundingBox();
            if (cnw.type == Type.MESA) {
                final int integer5 = -5;
                final int integer6 = cha.getSeaLevel() - this.boundingBox.y1 + this.boundingBox.getYSpan() / 2 + 5;
                this.boundingBox.move(0, integer6, 0);
                for (final StructurePiece cty12 : this.pieces) {
                    cty12.move(0, integer6, 0);
                }
            }
            else {
                this.moveBelowSeaLevel(cha.getSeaLevel(), this.random, 10);
            }
        }
    }
}
