package net.minecraft.world.level.block;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.tags.BlockTags;
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.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;

/**
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
 */
public class SnowyBlock extends Block {
	public static final BooleanProperty SNOWY = BlockStateProperties.SNOWY;

	/**
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
	 */
	public SnowyBlock(final BlockBehaviour.Properties properties) {
		super(properties);
		this.registerDefaultState(this.stateDefinition.any().setValue(SNOWY, false));
	}

	@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 directionToNeighbour == Direction.UP
			? state.setValue(SNOWY, isSnowySetting(neighbourState))
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
	}

	@Override
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
		BlockState aboveState = context.getLevel().getBlockState(context.getClickedPos().above());
		return this.defaultBlockState().setValue(SNOWY, isSnowySetting(aboveState));
	}

	protected static boolean isSnowySetting(final BlockState aboveState) {
		return aboveState.is(BlockTags.SNOW);
	}

	@Override
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
		builder.add(SNOWY);
	}
}
