package net.minecraft.world.level.block;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.ScheduledTickAccess;
import net.minecraft.world.level.block.state.BlockBehaviour;
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.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import org.jspecify.annotations.Nullable;

public abstract class FaceAttachedHorizontalDirectionalBlock extends HorizontalDirectionalBlock {
	public static final EnumProperty<AttachFace> FACE = BlockStateProperties.ATTACH_FACE;

	protected FaceAttachedHorizontalDirectionalBlock(final BlockBehaviour.Properties properties) {
		super(properties);
	}

	@Override
	protected boolean canSurvive(final BlockState state, final LevelReader level, final BlockPos pos) {
		return canAttach(level, pos, getConnectedDirection(state).getOpposite());
	}

	public static boolean canAttach(final LevelReader level, final BlockPos pos, final Direction direction) {
		BlockPos relative = pos.relative(direction);
		return level.getBlockState(relative).isFaceSturdy(level, relative, direction.getOpposite());
	}

	@Override
	public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
		for (Direction direction : context.getNearestLookingDirections()) {
			BlockState state;
			if (direction.getAxis() == Direction.Axis.Y) {
				state = this.defaultBlockState()
					.setValue(FACE, direction == Direction.UP ? AttachFace.CEILING : AttachFace.FLOOR)
					.setValue(FACING, context.getHorizontalDirection());
			} else {
				state = this.defaultBlockState().setValue(FACE, AttachFace.WALL).setValue(FACING, direction.getOpposite());
			}

			if (state.canSurvive(context.getLevel(), context.getClickedPos())) {
				return state;
			}
		}

		return null;
	}

	@Override
	protected BlockState updateShape(
		final BlockState state,
		final LevelReader level,
		final ScheduledTickAccess ticks,
		final BlockPos pos,
		final Direction directionToNeighbour,
		final BlockPos neighbourPos,
		final BlockState neighbourState,
		final RandomSource random
	) {
		return getConnectedDirection(state).getOpposite() == directionToNeighbour && !state.canSurvive(level, pos)
			? Blocks.AIR.defaultBlockState()
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
	}

	protected static Direction getConnectedDirection(final BlockState state) {
		return switch ((AttachFace)state.getValue(FACE)) {
			case CEILING -> Direction.DOWN;
			case FLOOR -> Direction.UP;
			default -> (Direction)state.getValue(FACING);
		};
	}
}
