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

import com.mojang.datafixers.kinds.Applicative;
import com.mojang.datafixers.kinds.App;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Direction;
import com.google.common.collect.Lists;
import net.minecraft.world.level.levelgen.feature.foliageplacers.FoliagePlacer;
import java.util.List;
import net.minecraft.world.level.levelgen.feature.configurations.TreeConfiguration;
import net.minecraft.world.level.levelgen.structure.BoundingBox;
import java.util.Set;
import net.minecraft.core.BlockPos;
import java.util.Random;
import net.minecraft.world.level.LevelSimulatedRW;
import com.mojang.serialization.Codec;

public class ForkingTrunkPlacer extends TrunkPlacer {
    public static final Codec<ForkingTrunkPlacer> CODEC;
    
    public ForkingTrunkPlacer(final int integer1, final int integer2, final int integer3) {
        super(integer1, integer2, integer3);
    }
    
    @Override
    protected TrunkPlacerType<?> type() {
        return TrunkPlacerType.FORKING_TRUNK_PLACER;
    }
    
    @Override
    public List<FoliagePlacer.FoliageAttachment> placeTrunk(final LevelSimulatedRW bqf, final Random random, final int integer, final BlockPos fu, final Set<BlockPos> set, final BoundingBox ctd, final TreeConfiguration cou) {
        TrunkPlacer.setDirtAt(bqf, fu.below());
        final List<FoliagePlacer.FoliageAttachment> list9 = Lists.newArrayList();
        final Direction fz10 = Direction.Plane.HORIZONTAL.getRandomDirection(random);
        final int integer2 = integer - random.nextInt(4) - 1;
        int integer3 = 3 - random.nextInt(3);
        final BlockPos.MutableBlockPos a13 = new BlockPos.MutableBlockPos();
        int integer4 = fu.getX();
        int integer5 = fu.getZ();
        int integer6 = 0;
        for (int integer7 = 0; integer7 < integer; ++integer7) {
            final int integer8 = fu.getY() + integer7;
            if (integer7 >= integer2 && integer3 > 0) {
                integer4 += fz10.getStepX();
                integer5 += fz10.getStepZ();
                --integer3;
            }
            if (TrunkPlacer.placeLog(bqf, random, a13.set(integer4, integer8, integer5), set, ctd, cou)) {
                integer6 = integer8 + 1;
            }
        }
        list9.add(new FoliagePlacer.FoliageAttachment(new BlockPos(integer4, integer6, integer5), 1, false));
        integer4 = fu.getX();
        integer5 = fu.getZ();
        final Direction fz11 = Direction.Plane.HORIZONTAL.getRandomDirection(random);
        if (fz11 != fz10) {
            final int integer8 = integer2 - random.nextInt(2) - 1;
            int integer9 = 1 + random.nextInt(3);
            integer6 = 0;
            for (int integer10 = integer8; integer10 < integer && integer9 > 0; ++integer10, --integer9) {
                if (integer10 >= 1) {
                    final int integer11 = fu.getY() + integer10;
                    integer4 += fz11.getStepX();
                    integer5 += fz11.getStepZ();
                    if (TrunkPlacer.placeLog(bqf, random, a13.set(integer4, integer11, integer5), set, ctd, cou)) {
                        integer6 = integer11 + 1;
                    }
                }
            }
            if (integer6 > 1) {
                list9.add(new FoliagePlacer.FoliageAttachment(new BlockPos(integer4, integer6, integer5), 0, false));
            }
        }
        return list9;
    }
    
    static {
        CODEC = RecordCodecBuilder.create(instance -> TrunkPlacer.<TrunkPlacer>trunkPlacerParts((RecordCodecBuilder.Instance<TrunkPlacer>)instance).apply(instance, ForkingTrunkPlacer::new));
    }
}
