package net.minecraft.world.level.block;

import java.util.function.Function;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
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.EnumProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

/**
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
 */
public class FlowerBedBlock extends VegetationBlock implements BonemealableBlock, SegmentableBlock {
	public static final EnumProperty<Direction> FACING = BlockStateProperties.HORIZONTAL_FACING;
	public static final IntegerProperty AMOUNT = BlockStateProperties.FLOWER_AMOUNT;
	private final int shapeHeight;
	private final Function<BlockState, VoxelShape> shapes;

	/**
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
	 */
	public FlowerBedBlock(final BlockBehaviour.Properties properties, final int shapeHeight) {
		super(properties);
		this.shapeHeight = shapeHeight;
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(AMOUNT, 1));
		this.shapes = this.makeShapes();
	}

	private Function<BlockState, VoxelShape> makeShapes() {
		return this.getShapeForEachState(this.getShapeCalculator(FACING, AMOUNT));
	}

	@Override
	public BlockState rotate(final BlockState state, final Rotation rotation) {
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
	}

	@Override
	public BlockState mirror(final BlockState state, final Mirror mirror) {
		return state.rotate(mirror.getRotation(state.getValue(FACING)));
	}

	@Override
	public boolean canBeReplaced(final BlockState state, final BlockPlaceContext context) {
		return this.canBeReplaced(state, context, AMOUNT) ? true : super.canBeReplaced(state, context);
	}

	@Override
	public VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
		return this.shapes.apply(state);
	}

	@Override
	public double getShapeHeight() {
		return this.shapeHeight;
	}

	@Override
	public IntegerProperty getSegmentAmountProperty() {
		return AMOUNT;
	}

	@Override
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
		return this.getStateForPlacement(context, this, AMOUNT, FACING);
	}

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

	@Override
	public boolean isValidBonemealTarget(final LevelReader level, final BlockPos pos, final BlockState state, final BonemealSource source) {
		return true;
	}

	@Override
	public boolean isBonemealSuccess(final Level level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
		return true;
	}

	@Override
	public void performBonemeal(final ServerLevel level, final RandomSource random, final BlockPos pos, final BlockState state, final BonemealSource source) {
		int currentAmount = state.getValue(AMOUNT);
		if (currentAmount < 4) {
			level.setBlock(pos, state.setValue(AMOUNT, currentAmount + 1), 2);
		} else {
			popResource(level, pos, new ItemStack(this));
		}
	}
}
