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

import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.core.Vec3i;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.StructureFeatureManager;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.BlockIgnoreProcessor;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.levelgen.feature.StructurePieceType;
import com.google.common.collect.ImmutableMap;
import java.util.Random;
import java.util.List;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.core.BlockPos;
import java.util.Map;
import net.minecraft.resources.ResourceLocation;

public class IglooPieces {
    private static final ResourceLocation STRUCTURE_LOCATION_IGLOO;
    private static final ResourceLocation STRUCTURE_LOCATION_LADDER;
    private static final ResourceLocation STRUCTURE_LOCATION_LABORATORY;
    private static final Map<ResourceLocation, BlockPos> PIVOTS;
    private static final Map<ResourceLocation, BlockPos> OFFSETS;
    
    public static void addPieces(final StructureManager cva, final BlockPos fu, final Rotation cap, final List<StructurePiece> list, final Random random) {
        if (random.nextDouble() < 0.5) {
            final int integer6 = random.nextInt(8) + 4;
            list.add(new IglooPiece(cva, IglooPieces.STRUCTURE_LOCATION_LABORATORY, fu, cap, integer6 * 3));
            for (int integer7 = 0; integer7 < integer6 - 1; ++integer7) {
                list.add(new IglooPiece(cva, IglooPieces.STRUCTURE_LOCATION_LADDER, fu, cap, integer7 * 3));
            }
        }
        list.add(new IglooPiece(cva, IglooPieces.STRUCTURE_LOCATION_IGLOO, fu, cap, 0));
    }
    
    static {
        STRUCTURE_LOCATION_IGLOO = new ResourceLocation("igloo/top");
        STRUCTURE_LOCATION_LADDER = new ResourceLocation("igloo/middle");
        STRUCTURE_LOCATION_LABORATORY = new ResourceLocation("igloo/bottom");
        PIVOTS = ImmutableMap.<ResourceLocation, BlockPos>of(IglooPieces.STRUCTURE_LOCATION_IGLOO, new BlockPos(3, 5, 5), IglooPieces.STRUCTURE_LOCATION_LADDER, new BlockPos(1, 3, 1), IglooPieces.STRUCTURE_LOCATION_LABORATORY, new BlockPos(3, 6, 7));
        OFFSETS = ImmutableMap.<ResourceLocation, BlockPos>of(IglooPieces.STRUCTURE_LOCATION_IGLOO, BlockPos.ZERO, IglooPieces.STRUCTURE_LOCATION_LADDER, new BlockPos(2, -3, 4), IglooPieces.STRUCTURE_LOCATION_LABORATORY, new BlockPos(0, -3, -2));
    }
    
    public static class IglooPiece extends TemplateStructurePiece {
        private final ResourceLocation templateLocation;
        private final Rotation rotation;
        
        public IglooPiece(final StructureManager cva, final ResourceLocation uh, final BlockPos fu, final Rotation cap, final int integer) {
            super(StructurePieceType.IGLOO, 0);
            this.templateLocation = uh;
            final BlockPos fu2 = IglooPieces.OFFSETS.get(uh);
            this.templatePosition = fu.offset(fu2.getX(), fu2.getY() - integer, fu2.getZ());
            this.rotation = cap;
            this.loadTemplate(cva);
        }
        
        public IglooPiece(final StructureManager cva, final CompoundTag le) {
            super(StructurePieceType.IGLOO, le);
            this.templateLocation = new ResourceLocation(le.getString("Template"));
            this.rotation = Rotation.valueOf(le.getString("Rot"));
            this.loadTemplate(cva);
        }
        
        private void loadTemplate(final StructureManager cva) {
            final StructureTemplate cve3 = cva.getOrCreate(this.templateLocation);
            final StructurePlaceSettings cvb4 = new StructurePlaceSettings().setRotation(this.rotation).setMirror(Mirror.NONE).setRotationPivot(IglooPieces.PIVOTS.get(this.templateLocation)).addProcessor(BlockIgnoreProcessor.STRUCTURE_BLOCK);
            this.setup(cve3, this.templatePosition, cvb4);
        }
        
        @Override
        protected void addAdditionalSaveData(final CompoundTag le) {
            super.addAdditionalSaveData(le);
            le.putString("Template", this.templateLocation.toString());
            le.putString("Rot", this.rotation.name());
        }
        
        @Override
        protected void handleDataMarker(final String string, final BlockPos fu, final LevelAccessor bqc, final Random random, final BoundingBox ctd) {
            if (!"chest".equals(string)) {
                return;
            }
            bqc.setBlock(fu, Blocks.AIR.defaultBlockState(), 3);
            final BlockEntity cdl7 = bqc.getBlockEntity(fu.below());
            if (cdl7 instanceof ChestBlockEntity) {
                ((ChestBlockEntity)cdl7).setLootTable(BuiltInLootTables.IGLOO_CHEST, random.nextLong());
            }
        }
        
        @Override
        public boolean postProcess(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BoundingBox ctd, final ChunkPos bph, final BlockPos fu) {
            final StructurePlaceSettings cvb9 = new StructurePlaceSettings().setRotation(this.rotation).setMirror(Mirror.NONE).setRotationPivot(IglooPieces.PIVOTS.get(this.templateLocation)).addProcessor(BlockIgnoreProcessor.STRUCTURE_BLOCK);
            final BlockPos fu2 = IglooPieces.OFFSETS.get(this.templateLocation);
            final BlockPos fu3 = this.templatePosition.offset(StructureTemplate.calculateRelativePosition(cvb9, new BlockPos(3 - fu2.getX(), 0, 0 - fu2.getZ())));
            final int integer12 = bqu.getHeight(Heightmap.Types.WORLD_SURFACE_WG, fu3.getX(), fu3.getZ());
            final BlockPos fu4 = this.templatePosition;
            this.templatePosition = this.templatePosition.offset(0, integer12 - 90 - 1, 0);
            final boolean boolean14 = super.postProcess(bqu, bqq, cha, random, ctd, bph, fu);
            if (this.templateLocation.equals(IglooPieces.STRUCTURE_LOCATION_IGLOO)) {
                final BlockPos fu5 = this.templatePosition.offset(StructureTemplate.calculateRelativePosition(cvb9, new BlockPos(3, 0, 5)));
                final BlockState cfj16 = bqu.getBlockState(fu5.below());
                if (!cfj16.isAir() && !cfj16.is(Blocks.LADDER)) {
                    bqu.setBlock(fu5, Blocks.SNOW_BLOCK.defaultBlockState(), 3);
                }
            }
            this.templatePosition = fu4;
            return boolean14;
        }
    }
}
