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

public class LadderBlock extends Block implements SimpleWaterloggedBlock {
    public static final DirectionProperty FACING;
    public static final BooleanProperty WATERLOGGED;
    protected static final VoxelShape EAST_AABB;
    protected static final VoxelShape WEST_AABB;
    protected static final VoxelShape SOUTH_AABB;
    protected static final VoxelShape NORTH_AABB;
    
    protected LadderBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)LadderBlock.FACING, Direction.NORTH)).<Comparable, Boolean>setValue((Property<Comparable>)LadderBlock.WATERLOGGED, false));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        switch (cfj.<Direction>getValue((Property<Direction>)LadderBlock.FACING)) {
            case NORTH: {
                return LadderBlock.NORTH_AABB;
            }
            case SOUTH: {
                return LadderBlock.SOUTH_AABB;
            }
            case WEST: {
                return LadderBlock.WEST_AABB;
            }
            default: {
                return LadderBlock.EAST_AABB;
            }
        }
    }
    
    private boolean canAttachTo(final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        final BlockState cfj5 = bpg.getBlockState(fu);
        return cfj5.isFaceSturdy(bpg, fu, fz);
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        final Direction fz5 = cfj.<Direction>getValue((Property<Direction>)LadderBlock.FACING);
        return this.canAttachTo(bqd, fu.relative(fz5.getOpposite()), fz5);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (fz.getOpposite() == cfj1.<Direction>getValue((Property<Direction>)LadderBlock.FACING) && !cfj1.canSurvive(bqc, fu5)) {
            return Blocks.AIR.defaultBlockState();
        }
        if (cfj1.<Boolean>getValue((Property<Boolean>)LadderBlock.WATERLOGGED)) {
            bqc.getLiquidTicks().scheduleTick(fu5, Fluids.WATER, Fluids.WATER.getTickDelay(bqc));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        if (!bin.replacingClickedOnBlock()) {
            final BlockState cfj3 = bin.getLevel().getBlockState(bin.getClickedPos().relative(bin.getClickedFace().getOpposite()));
            if (cfj3.is(this) && cfj3.<Comparable>getValue((Property<Comparable>)LadderBlock.FACING) == bin.getClickedFace()) {
                return null;
            }
        }
        BlockState cfj3 = this.defaultBlockState();
        final LevelReader bqd4 = bin.getLevel();
        final BlockPos fu5 = bin.getClickedPos();
        final FluidState cxa6 = bin.getLevel().getFluidState(bin.getClickedPos());
        for (final Direction fz10 : bin.getNearestLookingDirections()) {
            if (fz10.getAxis().isHorizontal()) {
                cfj3 = ((StateHolder<O, BlockState>)cfj3).<Comparable, Direction>setValue((Property<Comparable>)LadderBlock.FACING, fz10.getOpposite());
                if (cfj3.canSurvive(bqd4, fu5)) {
                    return ((StateHolder<O, BlockState>)cfj3).<Comparable, Boolean>setValue((Property<Comparable>)LadderBlock.WATERLOGGED, cxa6.getType() == Fluids.WATER);
                }
            }
        }
        return null;
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)LadderBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)LadderBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<Direction>getValue((Property<Direction>)LadderBlock.FACING)));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(LadderBlock.FACING, LadderBlock.WATERLOGGED);
    }
    
    @Override
    public FluidState getFluidState(final BlockState cfj) {
        if (cfj.<Boolean>getValue((Property<Boolean>)LadderBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(cfj);
    }
    
    static {
        FACING = HorizontalDirectionalBlock.FACING;
        WATERLOGGED = BlockStateProperties.WATERLOGGED;
        EAST_AABB = Block.box(0.0, 0.0, 0.0, 3.0, 16.0, 16.0);
        WEST_AABB = Block.box(13.0, 0.0, 0.0, 16.0, 16.0, 16.0);
        SOUTH_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 3.0);
        NORTH_AABB = Block.box(0.0, 0.0, 13.0, 16.0, 16.0, 16.0);
    }
}
