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.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.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 byj, final LevelReader bjz, final BlockPos fk) {
        return canAttach(bjz, fk, getConnectedDirection(byj).getOpposite());
    }
    
    public static boolean canAttach(final LevelReader bjz, final BlockPos fk, final Direction fp) {
        final BlockPos fk2 = fk.relative(fp);
        return bjz.getBlockState(fk2).isFaceSturdy(bjz, fk2, fp.getOpposite());
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        for (final Direction fp6 : bcq.getNearestLookingDirections()) {
            BlockState byj7;
            if (fp6.getAxis() == Direction.Axis.Y) {
                byj7 = (((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue(FaceAttachedHorizontalDirectionalBlock.FACE, (fp6 == Direction.UP) ? AttachFace.CEILING : AttachFace.FLOOR)).<Comparable, Direction>setValue((Property<Comparable>)FaceAttachedHorizontalDirectionalBlock.FACING, bcq.getHorizontalDirection());
            }
            else {
                byj7 = (((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue(FaceAttachedHorizontalDirectionalBlock.FACE, AttachFace.WALL)).<Comparable, Direction>setValue((Property<Comparable>)FaceAttachedHorizontalDirectionalBlock.FACING, fp6.getOpposite());
            }
            if (byj7.canSurvive(bcq.getLevel(), bcq.getClickedPos())) {
                return byj7;
            }
        }
        return null;
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (getConnectedDirection(byj1).getOpposite() == fp && !byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    protected static Direction getConnectedDirection(final BlockState byj) {
        switch (byj.<AttachFace>getValue(FaceAttachedHorizontalDirectionalBlock.FACE)) {
            case CEILING: {
                return Direction.DOWN;
            }
            case FLOOR: {
                return Direction.UP;
            }
            default: {
                return byj.<Direction>getValue((Property<Direction>)FaceAttachedHorizontalDirectionalBlock.FACING);
            }
        }
    }
    
    static {
        FACE = BlockStateProperties.ATTACH_FACE;
    }
}
