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.LevelAccessor;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.AttachFace;
import net.minecraft.world.level.block.state.properties.EnumProperty;

public class FaceAttachedHorizontalDirectionalBlock extends HorizontalDirectionalBlock {
    public static final EnumProperty<AttachFace> FACE;
    
    protected FaceAttachedHorizontalDirectionalBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        return canAttach(bqd, fu, getConnectedDirection(cfj).getOpposite());
    }
    
    public static boolean canAttach(final LevelReader bqd, final BlockPos fu, final Direction fz) {
        final BlockPos fu2 = fu.relative(fz);
        return bqd.getBlockState(fu2).isFaceSturdy(bqd, fu2, fz.getOpposite());
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        for (final Direction fz6 : bin.getNearestLookingDirections()) {
            BlockState cfj7;
            if (fz6.getAxis() == Direction.Axis.Y) {
                cfj7 = (((StateHolder<O, BlockState>)this.defaultBlockState()).setValue(FaceAttachedHorizontalDirectionalBlock.FACE, (fz6 == Direction.UP) ? AttachFace.CEILING : AttachFace.FLOOR)).<Comparable, Direction>setValue((Property<Comparable>)FaceAttachedHorizontalDirectionalBlock.FACING, bin.getHorizontalDirection());
            }
            else {
                cfj7 = (((StateHolder<O, BlockState>)this.defaultBlockState()).setValue(FaceAttachedHorizontalDirectionalBlock.FACE, AttachFace.WALL)).<Comparable, Direction>setValue((Property<Comparable>)FaceAttachedHorizontalDirectionalBlock.FACING, fz6.getOpposite());
            }
            if (cfj7.canSurvive(bin.getLevel(), bin.getClickedPos())) {
                return cfj7;
            }
        }
        return null;
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (getConnectedDirection(cfj1).getOpposite() == fz && !cfj1.canSurvive(bqc, fu5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    protected static Direction getConnectedDirection(final BlockState cfj) {
        switch (cfj.<AttachFace>getValue(FaceAttachedHorizontalDirectionalBlock.FACE)) {
            case CEILING: {
                return Direction.DOWN;
            }
            case FLOOR: {
                return Direction.UP;
            }
            default: {
                return cfj.<Direction>getValue((Property<Direction>)FaceAttachedHorizontalDirectionalBlock.FACING);
            }
        }
    }
    
    static {
        FACE = BlockStateProperties.ATTACH_FACE;
    }
}
