package net.minecraft.world.level.block;

import com.google.common.collect.Maps;
import java.util.Map;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.util.Prediction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.attribute.EnvironmentAttributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
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.gameevent.GameEvent;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;

public class FlowerPotBlock extends Block {
	private static final Map<Block, Block> POTTED_BY_CONTENT = Maps.newHashMap();
	private static final VoxelShape SHAPE = Block.column(6.0, 0.0, 6.0);
	private final Block potted;

	public FlowerPotBlock(final Block potted, final BlockBehaviour.Properties properties) {
		super(properties);
		this.potted = potted;
		POTTED_BY_CONTENT.put(potted, this);
	}

	@Override
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
		return SHAPE;
	}

	@Override
	protected InteractionResult useItemOn(
		final ItemStack itemStack,
		final BlockState state,
		final Level level,
		final BlockPos pos,
		final Player player,
		final InteractionHand hand,
		final BlockHitResult hitResult
	) {
		BlockState newContents = (itemStack.getItem() instanceof BlockItem blockItem ? POTTED_BY_CONTENT.getOrDefault(blockItem.getBlock(), Blocks.AIR) : Blocks.AIR)
			.defaultBlockState();
		if (newContents.isAir()) {
			return InteractionResult.TRY_WITH_EMPTY_HAND;
		}

		if (!this.isEmpty()) {
			return InteractionResult.CONSUME;
		}

		level.setBlockAndUpdate(pos, newContents);
		level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
		player.awardStat(Stats.POT_FLOWER);
		itemStack.consume(1, player);
		return InteractionResult.SUCCESS;
	}

	@Override
	protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
		if (this.isEmpty()) {
			return InteractionResult.CONSUME;
		}

		ItemStack plant = new ItemStack(this.potted);
		if (!player.addItem(plant)) {
			player.drop(plant, false, Prediction.PREDICTED);
		}

		level.setBlockAndUpdate(pos, Blocks.FLOWER_POT.defaultBlockState());
		level.gameEvent(player, GameEvent.BLOCK_CHANGE, pos);
		return InteractionResult.SUCCESS;
	}

	@Override
	protected ItemStack getCloneItemStack(final LevelReader level, final BlockPos pos, final BlockState state, final boolean includeData) {
		return this.isEmpty() ? super.getCloneItemStack(level, pos, state, includeData) : new ItemStack(this.potted);
	}

	private boolean isEmpty() {
		return this.potted == Blocks.AIR;
	}

	@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.DOWN && !state.canSurvive(level, pos)
			? Blocks.AIR.defaultBlockState()
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
	}

	public Block getPotted() {
		return this.potted;
	}

	@Override
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
		return false;
	}

	@Override
	protected boolean isRandomlyTicking(final BlockState state) {
		return state.is(Blocks.POTTED_OPEN_EYEBLOSSOM) || state.is(Blocks.POTTED_CLOSED_EYEBLOSSOM);
	}

	@Override
	protected void randomTick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
		if (this.isRandomlyTicking(state)) {
			boolean isOpen = this.potted == Blocks.OPEN_EYEBLOSSOM;
			boolean shouldBeOpen = level.environmentAttributes().getValue(EnvironmentAttributes.EYEBLOSSOM_OPEN, pos).toBoolean(isOpen);
			if (isOpen != shouldBeOpen) {
				level.setBlockAndUpdate(pos, this.opposite(state));
				EyeblossomBlock.Type newType = EyeblossomBlock.Type.fromBoolean(isOpen).transform();
				newType.spawnTransformParticle(level, pos, random);
				level.playSound(null, pos, newType.longSwitchSound(), SoundSource.BLOCKS, 1.0F, 1.0F);
			}
		}

		super.randomTick(state, level, pos, random);
	}

	public BlockState opposite(final BlockState state) {
		if (state.is(Blocks.POTTED_OPEN_EYEBLOSSOM)) {
			return Blocks.POTTED_CLOSED_EYEBLOSSOM.defaultBlockState();
		} else {
			return state.is(Blocks.POTTED_CLOSED_EYEBLOSSOM) ? Blocks.POTTED_OPEN_EYEBLOSSOM.defaultBlockState() : state;
		}
	}
}
