package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
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.material.Fluids;
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.tags.FluidTags;
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.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((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)SeaPickleBlock.PICKLES, 1)).<Comparable, Boolean>setValue((Property<Comparable>)SeaPickleBlock.WATERLOGGED, true));
    }
    
    @Override
    public int getLightEmission(final BlockState byj) {
        return this.isDead(byj) ? 0 : (super.getLightEmission(byj) + 3 * byj.<Integer>getValue((Property<Integer>)SeaPickleBlock.PICKLES));
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final BlockState byj3 = bcq.getLevel().getBlockState(bcq.getClickedPos());
        if (byj3.getBlock() == this) {
            return ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Integer>setValue((Property<Comparable>)SeaPickleBlock.PICKLES, Math.min(4, byj3.<Integer>getValue((Property<Integer>)SeaPickleBlock.PICKLES) + 1));
        }
        final FluidState coj4 = bcq.getLevel().getFluidState(bcq.getClickedPos());
        final boolean boolean5 = coj4.is(FluidTags.WATER) && coj4.getAmount() == 8;
        return ((AbstractStateHolder<O, BlockState>)super.getStateForPlacement(bcq)).<Comparable, Boolean>setValue((Property<Comparable>)SeaPickleBlock.WATERLOGGED, boolean5);
    }
    
    private boolean isDead(final BlockState byj) {
        return !byj.<Boolean>getValue((Property<Boolean>)SeaPickleBlock.WATERLOGGED);
    }
    
    @Override
    protected boolean mayPlaceOn(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return !byj.getCollisionShape(bjg, fk).getFaceShape(Direction.UP).isEmpty();
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockPos fk2 = fk.below();
        return this.mayPlaceOn(bjz.getBlockState(fk2), bjz, fk2);
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (!byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        if (byj1.<Boolean>getValue((Property<Boolean>)SeaPickleBlock.WATERLOGGED)) {
            bjx.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bjx));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean canBeReplaced(final BlockState byj, final BlockPlaceContext bcq) {
        return (bcq.getItemInHand().getItem() == this.asItem() && byj.<Integer>getValue((Property<Integer>)SeaPickleBlock.PICKLES) < 4) || super.canBeReplaced(byj, bcq);
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        switch (byj.<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 byj) {
        if (byj.<Boolean>getValue((Property<Boolean>)SeaPickleBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(byj);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(SeaPickleBlock.PICKLES, SeaPickleBlock.WATERLOGGED);
    }
    
    @Override
    public boolean isValidBonemealTarget(final BlockGetter bjg, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        return true;
    }
    
    @Override
    public boolean isBonemealSuccess(final Level bjw, final Random random, final BlockPos fk, final BlockState byj) {
        return true;
    }
    
    @Override
    public void performBonemeal(final ServerLevel xd, final Random random, final BlockPos fk, final BlockState byj) {
        if (!this.isDead(byj) && xd.getBlockState(fk.below()).is(BlockTags.CORAL_BLOCKS)) {
            final int integer6 = 5;
            int integer7 = 1;
            final int integer8 = 2;
            int integer9 = 0;
            final int integer10 = fk.getX() - 2;
            int integer11 = 0;
            for (int integer12 = 0; integer12 < 5; ++integer12) {
                for (int integer13 = 0; integer13 < integer7; ++integer13) {
                    for (int integer14 = 2 + fk.getY() - 1, integer15 = integer14 - 2; integer15 < integer14; ++integer15) {
                        final BlockPos fk2 = new BlockPos(integer10 + integer12, integer15, fk.getZ() - integer11 + integer13);
                        if (fk2 != fk) {
                            if (random.nextInt(6) == 0 && xd.getBlockState(fk2).getBlock() == Blocks.WATER) {
                                final BlockState byj2 = xd.getBlockState(fk2.below());
                                if (byj2.is(BlockTags.CORAL_BLOCKS)) {
                                    xd.setBlock(fk2, ((AbstractStateHolder<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;
            }
            xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)SeaPickleBlock.PICKLES, 4), 2);
        }
    }
    
    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);
    }
}
