package fr.koka99cab.ibelievechickenfly.chickenfly;

import net.minecraft.core.Direction;
import net.minecraft.core.Position;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.entity.projectile.throwableitemprojectile.ThrownEgg;
import net.minecraft.world.item.EggItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public class GoldenEggItem extends EggItem {
	public GoldenEggItem(Properties properties) {
		super(properties);
	}

	@Override
	public InteractionResult use(Level level, Player player, InteractionHand hand) {
		applyBlueVariant(level, player.getItemInHand(hand));
		return super.use(level, player, hand);
	}

	@Override
	public Projectile asProjectile(Level level, Position position, ItemStack stack, Direction direction) {
		return new ThrownEgg(level, position.x(), position.y(), position.z(), stackWithBlueVariant(level, stack));
	}

	private static void applyBlueVariant(Level level, ItemStack stack) {
		ModChickenVariants.getBlue(level.registryAccess()).ifPresent(variant -> stack.set(DataComponents.CHICKEN_VARIANT, variant));
	}

	private static ItemStack stackWithBlueVariant(Level level, ItemStack stack) {
		ItemStack copy = stack.copyWithCount(1);
		applyBlueVariant(level, copy);
		return copy;
	}
}
