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

import org.apache.logging.log4j.LogManager;
import net.minecraft.world.level.ChunkPos;
import java.util.Random;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.StructureFeatureManager;
import net.minecraft.world.level.WorldGenLevel;
import java.util.Iterator;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import com.mojang.serialization.Dynamic;
import java.util.function.Consumer;
import com.mojang.serialization.DynamicOps;
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;
import org.apache.logging.log4j.Logger;

public abstract class PoolElementStructurePiece extends StructurePiece {
    private static final Logger LOGGER;
    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 cmm, final StructureManager cva, final StructurePoolElement cqd, final BlockPos fu, final int integer, final Rotation cap, final BoundingBox ctd) {
        super(cmm, 0);
        this.junctions = Lists.newArrayList();
        this.structureManager = cva;
        this.element = cqd;
        this.position = fu;
        this.groundLevelDelta = integer;
        this.rotation = cap;
        this.boundingBox = ctd;
    }
    
    public PoolElementStructurePiece(final StructureManager cva, final CompoundTag le, final StructurePieceType cmm) {
        super(cmm, le);
        this.junctions = Lists.newArrayList();
        this.structureManager = cva;
        this.position = new BlockPos(le.getInt("PosX"), le.getInt("PosY"), le.getInt("PosZ"));
        this.groundLevelDelta = le.getInt("ground_level_delta");
        this.element = StructurePoolElement.CODEC.parse((DynamicOps)NbtOps.INSTANCE, le.getCompound("pool_element")).resultOrPartial((Consumer)PoolElementStructurePiece.LOGGER::error).orElse(EmptyPoolElement.INSTANCE);
        this.rotation = Rotation.valueOf(le.getString("rotation"));
        this.boundingBox = this.element.getBoundingBox(cva, this.position, this.rotation);
        final ListTag lk5 = le.getList("junctions", 10);
        this.junctions.clear();
        lk5.forEach(lu -> this.junctions.add(JigsawJunction.deserialize((com.mojang.serialization.Dynamic<Object>)new Dynamic((DynamicOps)NbtOps.INSTANCE, lu))));
    }
    
    @Override
    protected void addAdditionalSaveData(final CompoundTag le) {
        le.putInt("PosX", this.position.getX());
        le.putInt("PosY", this.position.getY());
        le.putInt("PosZ", this.position.getZ());
        le.putInt("ground_level_delta", this.groundLevelDelta);
        StructurePoolElement.CODEC.encodeStart((DynamicOps)NbtOps.INSTANCE, this.element).resultOrPartial((Consumer)PoolElementStructurePiece.LOGGER::error).ifPresent(lu -> le.put("pool_element", lu));
        le.putString("rotation", this.rotation.name());
        final ListTag lk3 = new ListTag();
        for (final JigsawJunction cpy5 : this.junctions) {
            lk3.add((Tag)cpy5.serialize((com.mojang.serialization.DynamicOps<Object>)NbtOps.INSTANCE).getValue());
        }
        le.put("junctions", lk3);
    }
    
    @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) {
        return this.place(bqu, bqq, cha, random, ctd, fu, false);
    }
    
    public boolean place(final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final Random random, final BoundingBox ctd, final BlockPos fu, final boolean boolean7) {
        return this.element.place(this.structureManager, bqu, bqq, cha, this.position, fu, this.rotation, ctd, random, boolean7);
    }
    
    @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 cpy) {
        this.junctions.add(cpy);
    }
    
    public List<JigsawJunction> getJunctions() {
        return this.junctions;
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
