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 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.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((((AbstractStateHolder<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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        switch (byj.<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 bjg, final BlockPos fk, final Direction fp) {
        final BlockState byj5 = bjg.getBlockState(fk);
        return !byj5.isSignalSource() && byj5.isFaceSturdy(bjg, fk, fp);
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final Direction fp5 = byj.<Direction>getValue((Property<Direction>)LadderBlock.FACING);
        return this.canAttachTo(bjz, fk.relative(fp5.getOpposite()), fp5);
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (fp.getOpposite() == byj1.<Direction>getValue((Property<Direction>)LadderBlock.FACING) && !byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        if (byj1.<Boolean>getValue((Property<Boolean>)LadderBlock.WATERLOGGED)) {
            bjx.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bjx));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        if (!bcq.replacingClickedOnBlock()) {
            final BlockState byj3 = bcq.getLevel().getBlockState(bcq.getClickedPos().relative(bcq.getClickedFace().getOpposite()));
            if (byj3.getBlock() == this && byj3.<Comparable>getValue((Property<Comparable>)LadderBlock.FACING) == bcq.getClickedFace()) {
                return null;
            }
        }
        BlockState byj3 = this.defaultBlockState();
        final LevelReader bjz4 = bcq.getLevel();
        final BlockPos fk5 = bcq.getClickedPos();
        final FluidState coj6 = bcq.getLevel().getFluidState(bcq.getClickedPos());
        for (final Direction fp10 : bcq.getNearestLookingDirections()) {
            if (fp10.getAxis().isHorizontal()) {
                byj3 = ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Direction>setValue((Property<Comparable>)LadderBlock.FACING, fp10.getOpposite());
                if (byj3.canSurvive(bjz4, fk5)) {
                    return ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Boolean>setValue((Property<Comparable>)LadderBlock.WATERLOGGED, coj6.getType() == Fluids.WATER);
                }
            }
        }
        return null;
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)LadderBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)LadderBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<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 byj) {
        if (byj.<Boolean>getValue((Property<Boolean>)LadderBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(byj);
    }
    
    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);
    }
}
