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.world.level.block.state.StateDefinition;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.phys.shapes.CollisionContext;
import javax.annotation.Nullable;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.tags.FluidTags;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelAccessor;
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;

public class BaseCoralPlantTypeBlock extends Block implements SimpleWaterloggedBlock {
    public static final BooleanProperty WATERLOGGED;
    private static final VoxelShape AABB;
    
    protected BaseCoralPlantTypeBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Boolean>setValue((Property<Comparable>)BaseCoralPlantTypeBlock.WATERLOGGED, true));
    }
    
    protected void tryScheduleDieTick(final BlockState byj, final LevelAccessor bjx, final BlockPos fk) {
        if (!scanForWater(byj, bjx, fk)) {
            bjx.getBlockTicks().scheduleTick(fk, this, 60 + bjx.getRandom().nextInt(40));
        }
    }
    
    protected static boolean scanForWater(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        if (byj.<Boolean>getValue((Property<Boolean>)BaseCoralPlantTypeBlock.WATERLOGGED)) {
            return true;
        }
        for (final Direction fp7 : Direction.values()) {
            if (bjg.getFluidState(fk.relative(fp7)).is(FluidTags.WATER)) {
                return true;
            }
        }
        return false;
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final FluidState coj3 = bcq.getLevel().getFluidState(bcq.getClickedPos());
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Boolean>setValue((Property<Comparable>)BaseCoralPlantTypeBlock.WATERLOGGED, coj3.is(FluidTags.WATER) && coj3.getAmount() == 8);
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return BaseCoralPlantTypeBlock.AABB;
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (byj1.<Boolean>getValue((Property<Boolean>)BaseCoralPlantTypeBlock.WATERLOGGED)) {
            bjx.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bjx));
        }
        if (fp == Direction.DOWN && !this.canSurvive(byj1, bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockPos fk2 = fk.below();
        return bjz.getBlockState(fk2).isFaceSturdy(bjz, fk2, Direction.UP);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(BaseCoralPlantTypeBlock.WATERLOGGED);
    }
    
    @Override
    public FluidState getFluidState(final BlockState byj) {
        if (byj.<Boolean>getValue((Property<Boolean>)BaseCoralPlantTypeBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(byj);
    }
    
    static {
        WATERLOGGED = BlockStateProperties.WATERLOGGED;
        AABB = Block.box(2.0, 0.0, 2.0, 14.0, 4.0, 14.0);
    }
}
