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

import net.minecraft.world.level.block.state.StateHolder;
import java.util.function.BiFunction;
import com.mojang.datafixers.kinds.Applicative;
import com.mojang.datafixers.kinds.App;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.world.level.levelgen.feature.Feature;
import net.minecraft.core.Registry;
import net.minecraft.world.level.chunk.ChunkGenerator;
import net.minecraft.world.level.StructureFeatureManager;
import net.minecraft.world.level.WorldGenLevel;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.FrontAndTop;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.JigsawBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import com.google.common.collect.Lists;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import java.util.List;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureManager;
import net.minecraft.world.level.block.entity.JigsawBlockEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import com.mojang.serialization.Codec;

public class FeaturePoolElement extends StructurePoolElement {
    public static final Codec<FeaturePoolElement> CODEC;
    private final ConfiguredFeature<?, ?> feature;
    private final CompoundTag defaultJigsawNBT;
    
    @Deprecated
    public FeaturePoolElement(final ConfiguredFeature<?, ?> ckb) {
        this(ckb, StructureTemplatePool.Projection.RIGID);
    }
    
    private FeaturePoolElement(final ConfiguredFeature<?, ?> ckb, final StructureTemplatePool.Projection a) {
        super(a);
        this.feature = ckb;
        this.defaultJigsawNBT = this.fillDefaultJigsawNBT();
    }
    
    private CompoundTag fillDefaultJigsawNBT() {
        final CompoundTag le2 = new CompoundTag();
        le2.putString("name", "minecraft:bottom");
        le2.putString("final_state", "minecraft:air");
        le2.putString("pool", "minecraft:empty");
        le2.putString("target", "minecraft:empty");
        le2.putString("joint", JigsawBlockEntity.JointType.ROLLABLE.getSerializedName());
        return le2;
    }
    
    public BlockPos getSize(final StructureManager cva, final Rotation cap) {
        return BlockPos.ZERO;
    }
    
    @Override
    public List<StructureTemplate.StructureBlockInfo> getShuffledJigsawBlocks(final StructureManager cva, final BlockPos fu, final Rotation cap, final Random random) {
        final List<StructureTemplate.StructureBlockInfo> list6 = Lists.newArrayList();
        list6.add(new StructureTemplate.StructureBlockInfo(fu, ((StateHolder<O, BlockState>)Blocks.JIGSAW.defaultBlockState()).<FrontAndTop, FrontAndTop>setValue(JigsawBlock.ORIENTATION, FrontAndTop.fromFrontAndTop(Direction.DOWN, Direction.SOUTH)), this.defaultJigsawNBT));
        return list6;
    }
    
    @Override
    public BoundingBox getBoundingBox(final StructureManager cva, final BlockPos fu, final Rotation cap) {
        final BlockPos fu2 = this.getSize(cva, cap);
        return new BoundingBox(fu.getX(), fu.getY(), fu.getZ(), fu.getX() + fu2.getX(), fu.getY() + fu2.getY(), fu.getZ() + fu2.getZ());
    }
    
    @Override
    public boolean place(final StructureManager cva, final WorldGenLevel bqu, final StructureFeatureManager bqq, final ChunkGenerator cha, final BlockPos fu5, final BlockPos fu6, final Rotation cap, final BoundingBox ctd, final Random random, final boolean boolean10) {
        return this.feature.place(bqu, bqq, cha, random, fu5);
    }
    
    @Override
    public StructurePoolElementType<?> getType() {
        return StructurePoolElementType.FEATURE;
    }
    
    @Override
    public String toString() {
        return "Feature[" + Registry.FEATURE.getKey(this.feature.feature) + "]";
    }
    
    static {
        CODEC = RecordCodecBuilder.create(instance -> ((RecordCodecBuilder.Instance)instance).group((App)ConfiguredFeature.CODEC.fieldOf("feature").forGetter(cpx -> cpx.feature), (App)StructurePoolElement.<StructurePoolElement>projectionCodec()).apply(instance, (BiFunction)FeaturePoolElement::new));
    }
}
