package net.minecraft.world.item;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.BaseCoralWallFanBlock;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.block.Blocks;
import javax.annotation.Nullable;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.BonemealableBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.InteractionResult;

public class BoneMealItem extends Item {
    public BoneMealItem(final Properties a) {
        super(a);
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final Level bjw3 = bfz.getLevel();
        final BlockPos fk4 = bfz.getClickedPos();
        final BlockPos fk5 = fk4.relative(bfz.getClickedFace());
        if (growCrop(bfz.getItemInHand(), bjw3, fk4)) {
            if (!bjw3.isClientSide) {
                bjw3.levelEvent(2005, fk4, 0);
            }
            return InteractionResult.SUCCESS;
        }
        final BlockState byj6 = bjw3.getBlockState(fk4);
        final boolean boolean7 = byj6.isFaceSturdy(bjw3, fk4, bfz.getClickedFace());
        if (boolean7 && growWaterPlant(bfz.getItemInHand(), bjw3, fk5, bfz.getClickedFace())) {
            if (!bjw3.isClientSide) {
                bjw3.levelEvent(2005, fk5, 0);
            }
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.PASS;
    }
    
    public static boolean growCrop(final ItemStack ben, final Level bjw, final BlockPos fk) {
        final BlockState byj4 = bjw.getBlockState(fk);
        if (byj4.getBlock() instanceof BonemealableBlock) {
            final BonemealableBlock bpj5 = (BonemealableBlock)byj4.getBlock();
            if (bpj5.isValidBonemealTarget(bjw, fk, byj4, bjw.isClientSide)) {
                if (bjw instanceof ServerLevel) {
                    if (bpj5.isBonemealSuccess(bjw, bjw.random, fk, byj4)) {
                        bpj5.performBonemeal((ServerLevel)bjw, bjw.random, fk, byj4);
                    }
                    ben.shrink(1);
                }
                return true;
            }
        }
        return false;
    }
    
    public static boolean growWaterPlant(final ItemStack ben, final Level bjw, final BlockPos fk, @Nullable final Direction fp) {
        if (bjw.getBlockState(fk).getBlock() != Blocks.WATER || bjw.getFluidState(fk).getAmount() != 8) {
            return false;
        }
        if (!(bjw instanceof ServerLevel)) {
            return true;
        }
        int integer5 = 0;
    Label_0043:
        while (integer5 < 128) {
            BlockPos fk2 = fk;
            Biome bkt7 = bjw.getBiome(fk2);
            BlockState byj8 = Blocks.SEAGRASS.defaultBlockState();
            while (true) {
                for (int integer6 = 0; integer6 < integer5 / 16; ++integer6) {
                    fk2 = fk2.offset(BoneMealItem.random.nextInt(3) - 1, (BoneMealItem.random.nextInt(3) - 1) * BoneMealItem.random.nextInt(3) / 2, BoneMealItem.random.nextInt(3) - 1);
                    bkt7 = bjw.getBiome(fk2);
                    if (bjw.getBlockState(fk2).isCollisionShapeFullBlock(bjw, fk2)) {
                        ++integer5;
                        continue Label_0043;
                    }
                }
                if (bkt7 == Biomes.WARM_OCEAN || bkt7 == Biomes.DEEP_WARM_OCEAN) {
                    if (integer5 == 0 && fp != null && fp.getAxis().isHorizontal()) {
                        byj8 = ((AbstractStateHolder<O, BlockState>)BlockTags.WALL_CORALS.getRandomElement(bjw.random).defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)BaseCoralWallFanBlock.FACING, fp);
                    }
                    else if (BoneMealItem.random.nextInt(4) == 0) {
                        byj8 = BlockTags.UNDERWATER_BONEMEALS.getRandomElement(BoneMealItem.random).defaultBlockState();
                    }
                }
                if (byj8.getBlock().is(BlockTags.WALL_CORALS)) {
                    for (int integer6 = 0; !byj8.canSurvive(bjw, fk2) && integer6 < 4; byj8 = ((AbstractStateHolder<O, BlockState>)byj8).<Comparable, Direction>setValue((Property<Comparable>)BaseCoralWallFanBlock.FACING, Direction.Plane.HORIZONTAL.getRandomDirection(BoneMealItem.random)), ++integer6) {}
                }
                if (!byj8.canSurvive(bjw, fk2)) {
                    continue;
                }
                final BlockState byj9 = bjw.getBlockState(fk2);
                if (byj9.getBlock() == Blocks.WATER && bjw.getFluidState(fk2).getAmount() == 8) {
                    bjw.setBlock(fk2, byj8, 3);
                    continue;
                }
                if (byj9.getBlock() == Blocks.SEAGRASS && BoneMealItem.random.nextInt(10) == 0) {
                    ((BonemealableBlock)Blocks.SEAGRASS).performBonemeal((ServerLevel)bjw, BoneMealItem.random, fk2, byj9);
                }
                continue;
            }
        }
        ben.shrink(1);
        return true;
    }
    
    public static void addGrowthParticles(final LevelAccessor bjx, final BlockPos fk, int integer) {
        if (integer == 0) {
            integer = 15;
        }
        final BlockState byj4 = bjx.getBlockState(fk);
        if (byj4.isAir()) {
            return;
        }
        for (int integer2 = 0; integer2 < integer; ++integer2) {
            final double double6 = BoneMealItem.random.nextGaussian() * 0.02;
            final double double7 = BoneMealItem.random.nextGaussian() * 0.02;
            final double double8 = BoneMealItem.random.nextGaussian() * 0.02;
            bjx.addParticle(ParticleTypes.HAPPY_VILLAGER, fk.getX() + BoneMealItem.random.nextFloat(), fk.getY() + BoneMealItem.random.nextFloat() * byj4.getShape(bjx, fk).max(Direction.Axis.Y), fk.getZ() + BoneMealItem.random.nextFloat(), double6, double7, double8);
        }
    }
}
