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

import net.minecraft.world.level.levelgen.structure.EndCityPieces;
import net.minecraft.core.BlockPos;
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 net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.block.Rotation;
import java.util.Random;
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 EndCityFeature extends StructureFeature<NoneFeatureConfiguration> {
    public EndCityFeature(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) {
        return getYPositionForFeature(integer5, integer6, cha) >= 60;
    }
    
    @Override
    public StructureStartFactory<NoneFeatureConfiguration> getStartFactory() {
        return EndCityStart::new;
    }
    
    private static int getYPositionForFeature(final int integer1, final int integer2, final ChunkGenerator cha) {
        final Random random4 = new Random(integer1 + integer2 * 10387313);
        final Rotation cap5 = Rotation.getRandom(random4);
        int integer3 = 5;
        int integer4 = 5;
        if (cap5 == Rotation.CLOCKWISE_90) {
            integer3 = -5;
        }
        else if (cap5 == Rotation.CLOCKWISE_180) {
            integer3 = -5;
            integer4 = -5;
        }
        else if (cap5 == Rotation.COUNTERCLOCKWISE_90) {
            integer4 = -5;
        }
        final int integer5 = (integer1 << 4) + 7;
        final int integer6 = (integer2 << 4) + 7;
        final int integer7 = cha.getFirstOccupiedHeight(integer5, integer6, Heightmap.Types.WORLD_SURFACE_WG);
        final int integer8 = cha.getFirstOccupiedHeight(integer5, integer6 + integer4, Heightmap.Types.WORLD_SURFACE_WG);
        final int integer9 = cha.getFirstOccupiedHeight(integer5 + integer3, integer6, Heightmap.Types.WORLD_SURFACE_WG);
        final int integer10 = cha.getFirstOccupiedHeight(integer5 + integer3, integer6 + integer4, Heightmap.Types.WORLD_SURFACE_WG);
        return Math.min(Math.min(integer7, integer8), Math.min(integer9, integer10));
    }
    
    public static class EndCityStart extends StructureStart<NoneFeatureConfiguration> {
        public EndCityStart(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);
            final int integer5 = getYPositionForFeature(integer3, integer4, cha);
            if (integer5 < 60) {
                return;
            }
            final BlockPos fu10 = new BlockPos(integer3 * 16 + 8, integer5, integer4 * 16 + 8);
            EndCityPieces.startHouseTower(cva, fu10, cap8, this.pieces, this.random);
            this.calculateBoundingBox();
        }
    }
}
