package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.server.level.ServerLevel;
import java.util.Random;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import javax.annotation.Nullable;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class SeaPickleBlock extends BushBlock implements BonemealableBlock, SimpleWaterloggedBlock {
    public static final IntegerProperty PICKLES;
    public static final BooleanProperty WATERLOGGED;
    protected static final VoxelShape ONE_AABB;
    protected static final VoxelShape TWO_AABB;
    protected static final VoxelShape THREE_AABB;
    protected static final VoxelShape FOUR_AABB;
    
    protected SeaPickleBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)SeaPickleBlock.PICKLES, 1)).<Comparable, Boolean>setValue((Property<Comparable>)SeaPickleBlock.WATERLOGGED, true));
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final BlockState cfj3 = bin.getLevel().getBlockState(bin.getClickedPos());
        if (cfj3.is(this)) {
            return ((StateHolder<O, BlockState>)cfj3).<Comparable, Integer>setValue((Property<Comparable>)SeaPickleBlock.PICKLES, Math.min(4, cfj3.<Integer>getValue((Property<Integer>)SeaPickleBlock.PICKLES) + 1));
        }
        final FluidState cxa4 = bin.getLevel().getFluidState(bin.getClickedPos());
        final boolean boolean5 = cxa4.getType() == Fluids.WATER;
        return ((StateHolder<O, BlockState>)super.getStateForPlacement(bin)).<Comparable, Boolean>setValue((Property<Comparable>)SeaPickleBlock.WATERLOGGED, boolean5);
    }
    
    public static boolean isDead(final BlockState cfj) {
        return !cfj.<Boolean>getValue((Property<Boolean>)SeaPickleBlock.WATERLOGGED);
    }
    
    @Override
    protected boolean mayPlaceOn(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return !cfj.getCollisionShape(bpg, fu).getFaceShape(Direction.UP).isEmpty();
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        final BlockPos fu2 = fu.below();
        return this.mayPlaceOn(bqd.getBlockState(fu2), bqd, fu2);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (!cfj1.canSurvive(bqc, fu5)) {
            return Blocks.AIR.defaultBlockState();
        }
        if (cfj1.<Boolean>getValue((Property<Boolean>)SeaPickleBlock.WATERLOGGED)) {
            bqc.getLiquidTicks().scheduleTick(fu5, Fluids.WATER, Fluids.WATER.getTickDelay(bqc));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Override
    public boolean canBeReplaced(final BlockState cfj, final BlockPlaceContext bin) {
        return (bin.getItemInHand().getItem() == this.asItem() && cfj.<Integer>getValue((Property<Integer>)SeaPickleBlock.PICKLES) < 4) || super.canBeReplaced(cfj, bin);
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        switch (cfj.<Integer>getValue((Property<Integer>)SeaPickleBlock.PICKLES)) {
            default: {
                return SeaPickleBlock.ONE_AABB;
            }
            case 2: {
                return SeaPickleBlock.TWO_AABB;
            }
            case 3: {
                return SeaPickleBlock.THREE_AABB;
            }
            case 4: {
                return SeaPickleBlock.FOUR_AABB;
            }
        }
    }
    
    @Override
    public FluidState getFluidState(final BlockState cfj) {
        if (cfj.<Boolean>getValue((Property<Boolean>)SeaPickleBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(cfj);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(SeaPickleBlock.PICKLES, SeaPickleBlock.WATERLOGGED);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bpg, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        return true;
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bqb, final Random random, final BlockPos fu, final BlockState cfj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel zd, final Random random, final BlockPos fu, final BlockState cfj) {
        if (!isDead(cfj) && zd.getBlockState(fu.below()).is(BlockTags.CORAL_BLOCKS)) {
            final int integer6 = 5;
            int integer7 = 1;
            final int integer8 = 2;
            int integer9 = 0;
            final int integer10 = fu.getX() - 2;
            int integer11 = 0;
            for (int integer12 = 0; integer12 < 5; ++integer12) {
                for (int integer13 = 0; integer13 < integer7; ++integer13) {
                    for (int integer14 = 2 + fu.getY() - 1, integer15 = integer14 - 2; integer15 < integer14; ++integer15) {
                        final BlockPos fu2 = new BlockPos(integer10 + integer12, integer15, fu.getZ() - integer11 + integer13);
                        if (fu2 != fu) {
                            if (random.nextInt(6) == 0 && zd.getBlockState(fu2).is(Blocks.WATER)) {
                                final BlockState cfj2 = zd.getBlockState(fu2.below());
                                if (cfj2.is(BlockTags.CORAL_BLOCKS)) {
                                    zd.setBlock(fu2, ((StateHolder<O, BlockState>)Blocks.SEA_PICKLE.defaultBlockState()).<Comparable, Integer>setValue((Property<Comparable>)SeaPickleBlock.PICKLES, random.nextInt(4) + 1), 3);
                                }
                            }
                        }
                    }
                }
                if (integer9 < 2) {
                    integer7 += 2;
                    ++integer11;
                }
                else {
                    integer7 -= 2;
                    --integer11;
                }
                ++integer9;
            }
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)SeaPickleBlock.PICKLES, 4), 2);
        }
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    static {
        PICKLES = BlockStateProperties.PICKLES;
        WATERLOGGED = BlockStateProperties.WATERLOGGED;
        ONE_AABB = Block.box(6.0, 0.0, 6.0, 10.0, 6.0, 10.0);
        TWO_AABB = Block.box(3.0, 0.0, 3.0, 13.0, 6.0, 13.0);
        THREE_AABB = Block.box(2.0, 0.0, 2.0, 14.0, 6.0, 14.0);
        FOUR_AABB = Block.box(2.0, 0.0, 2.0, 14.0, 7.0, 14.0);
    }
}
