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

import net.minecraft.world.level.ChunkPos;
import java.util.Random;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.LevelAccessor;
import java.util.Iterator;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.level.levelgen.feature.structures.StructurePoolElementType;
import net.minecraft.util.Deserializer;
import net.minecraft.core.Registry;
import com.mojang.datafixers.types.DynamicOps;
import com.mojang.datafixers.Dynamic;
import net.minecraft.nbt.NbtOps;
import net.minecraft.world.level.levelgen.feature.structures.EmptyPoolElement;
import net.minecraft.nbt.CompoundTag;
import com.google.common.collect.Lists;
import net.minecraft.world.level.levelgen.feature.StructurePieceType;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.world.level.levelgen.feature.structures.JigsawJunction;
import java.util.List;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.levelgen.feature.structures.StructurePoolElement;

public abstract class PoolElementStructurePiece extends StructurePiece {
    protected final StructurePoolElement element;
    protected BlockPos position;
    private final int groundLevelDelta;
    protected final Rotation rotation;
    private final List<JigsawJunction> junctions;
    private final StructureManager structureManager;
    
    public PoolElementStructurePiece(final StructurePieceType cfm, final StructureManager cmo, final StructurePoolElement cik, final BlockPos fk, final int integer, final Rotation btu, final BoundingBox clb) {
        super(cfm, 0);
        this.junctions = Lists.newArrayList();
        this.structureManager = cmo;
        this.element = cik;
        this.position = fk;
        this.groundLevelDelta = integer;
        this.rotation = btu;
        this.boundingBox = clb;
    }
    
    public PoolElementStructurePiece(final StructureManager cmo, final CompoundTag jt, final StructurePieceType cfm) {
        super(cfm, jt);
        this.junctions = Lists.newArrayList();
        this.structureManager = cmo;
        this.position = new BlockPos(jt.getInt("PosX"), jt.getInt("PosY"), jt.getInt("PosZ"));
        this.groundLevelDelta = jt.getInt("ground_level_delta");
        this.element = Deserializer.<Object, EmptyPoolElement, StructurePoolElementType>deserialize((com.mojang.datafixers.Dynamic<Object>)new Dynamic((DynamicOps)NbtOps.INSTANCE, jt.getCompound("pool_element")), Registry.STRUCTURE_POOL_ELEMENT, "element_type", EmptyPoolElement.INSTANCE);
        this.rotation = Rotation.valueOf(jt.getString("rotation"));
        this.boundingBox = this.element.getBoundingBox(cmo, this.position, this.rotation);
        final ListTag jz5 = jt.getList("junctions", 10);
        this.junctions.clear();
        jz5.forEach(kj -> this.junctions.add(JigsawJunction.deserialize((com.mojang.datafixers.Dynamic<Object>)new Dynamic((DynamicOps)NbtOps.INSTANCE, kj))));
    }
    
    @Override
    protected void addAdditionalSaveData(final CompoundTag jt) {
        jt.putInt("PosX", this.position.getX());
        jt.putInt("PosY", this.position.getY());
        jt.putInt("PosZ", this.position.getZ());
        jt.putInt("ground_level_delta", this.groundLevelDelta);
        jt.put("pool_element", (Tag)this.element.serialize((com.mojang.datafixers.types.DynamicOps<Object>)NbtOps.INSTANCE).getValue());
        jt.putString("rotation", this.rotation.name());
        final ListTag jz3 = new ListTag();
        for (final JigsawJunction cig5 : this.junctions) {
            jz3.add((Tag)cig5.serialize((com.mojang.datafixers.types.DynamicOps<Object>)NbtOps.INSTANCE).getValue());
        }
        jt.put("junctions", jz3);
    }
    
    @Override
    public boolean postProcess(final LevelAccessor bjx, final ChunkGenerator<?> caa, final Random random, final BoundingBox clb, final ChunkPos bjh) {
        return this.element.place(this.structureManager, bjx, caa, this.position, this.rotation, clb, random);
    }
    
    @Override
    public void move(final int integer1, final int integer2, final int integer3) {
        super.move(integer1, integer2, integer3);
        this.position = this.position.offset(integer1, integer2, integer3);
    }
    
    @Override
    public Rotation getRotation() {
        return this.rotation;
    }
    
    @Override
    public String toString() {
        return String.format("<%s | %s | %s | %s>", this.getClass().getSimpleName(), this.position, this.rotation, this.element);
    }
    
    public StructurePoolElement getElement() {
        return this.element;
    }
    
    public BlockPos getPosition() {
        return this.position;
    }
    
    public int getGroundLevelDelta() {
        return this.groundLevelDelta;
    }
    
    public void addJunction(final JigsawJunction cig) {
        this.junctions.add(cig);
    }
    
    public List<JigsawJunction> getJunctions() {
        return this.junctions;
    }
}
