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

import org.apache.logging.log4j.LogManager;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Iterator;
import java.util.List;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.arguments.blocks.BlockStateParser;
import com.mojang.brigadier.StringReader;
import net.minecraft.world.level.block.state.properties.StructureMode;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.ChunkPos;
import java.util.Random;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.levelgen.feature.StructurePieceType;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import org.apache.logging.log4j.Logger;

public abstract class TemplateStructurePiece extends StructurePiece {
    private static final Logger LOGGER;
    protected StructureTemplate template;
    protected StructurePlaceSettings placeSettings;
    protected BlockPos templatePosition;
    
    public TemplateStructurePiece(final StructurePieceType cfm, final int integer) {
        super(cfm, integer);
    }
    
    public TemplateStructurePiece(final StructurePieceType cfm, final CompoundTag jt) {
        super(cfm, jt);
        this.templatePosition = new BlockPos(jt.getInt("TPX"), jt.getInt("TPY"), jt.getInt("TPZ"));
    }
    
    protected void setup(final StructureTemplate cms, final BlockPos fk, final StructurePlaceSettings cmp) {
        this.template = cms;
        this.setOrientation(Direction.NORTH);
        this.templatePosition = fk;
        this.placeSettings = cmp;
        this.boundingBox = cms.getBoundingBox(cmp, fk);
    }
    
    @Override
    protected void addAdditionalSaveData(final CompoundTag jt) {
        jt.putInt("TPX", this.templatePosition.getX());
        jt.putInt("TPY", this.templatePosition.getY());
        jt.putInt("TPZ", this.templatePosition.getZ());
    }
    
    @Override
    public boolean postProcess(final LevelAccessor bjx, final ChunkGenerator<?> caa, final Random random, final BoundingBox clb, final ChunkPos bjh) {
        this.placeSettings.setBoundingBox(clb);
        this.boundingBox = this.template.getBoundingBox(this.placeSettings, this.templatePosition);
        if (this.template.placeInWorld(bjx, this.templatePosition, this.placeSettings, 2)) {
            final List<StructureTemplate.StructureBlockInfo> list7 = this.template.filterBlocks(this.templatePosition, this.placeSettings, Blocks.STRUCTURE_BLOCK);
            for (final StructureTemplate.StructureBlockInfo b9 : list7) {
                if (b9.nbt == null) {
                    continue;
                }
                final StructureMode bzr10 = StructureMode.valueOf(b9.nbt.getString("mode"));
                if (bzr10 != StructureMode.DATA) {
                    continue;
                }
                this.handleDataMarker(b9.nbt.getString("metadata"), b9.pos, bjx, random, clb);
            }
            final List<StructureTemplate.StructureBlockInfo> list8 = this.template.filterBlocks(this.templatePosition, this.placeSettings, Blocks.JIGSAW);
            for (final StructureTemplate.StructureBlockInfo b10 : list8) {
                if (b10.nbt == null) {
                    continue;
                }
                final String string11 = b10.nbt.getString("final_state");
                final BlockStateParser dv12 = new BlockStateParser(new StringReader(string11), false);
                BlockState byj13 = Blocks.AIR.defaultBlockState();
                try {
                    dv12.parse(true);
                    final BlockState byj14 = dv12.getState();
                    if (byj14 != null) {
                        byj13 = byj14;
                    }
                    else {
                        TemplateStructurePiece.LOGGER.error("Error while parsing blockstate {} in jigsaw block @ {}", string11, b10.pos);
                    }
                }
                catch (CommandSyntaxException commandSyntaxException14) {
                    TemplateStructurePiece.LOGGER.error("Error while parsing blockstate {} in jigsaw block @ {}", string11, b10.pos);
                }
                bjx.setBlock(b10.pos, byj13, 3);
            }
        }
        return true;
    }
    
    protected abstract void handleDataMarker(final String string, final BlockPos fk, final LevelAccessor bjx, final Random random, final BoundingBox clb);
    
    @Override
    public void move(final int integer1, final int integer2, final int integer3) {
        super.move(integer1, integer2, integer3);
        this.templatePosition = this.templatePosition.offset(integer1, integer2, integer3);
    }
    
    @Override
    public Rotation getRotation() {
        return this.placeSettings.getRotation();
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
