package fr.koka99cab.spiderman.item;

import fr.koka99cab.spiderman.grapple.GrappleController;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent;

public class GrapplingRodItem extends Item {
	public GrapplingRodItem(Properties properties) {
		super(properties);
	}

	@Override
	public InteractionResult use(Level level, Player player, InteractionHand hand) {
		ItemStack stack = player.getItemInHand(hand);

		if (level instanceof ServerLevel serverLevel && player instanceof ServerPlayer serverPlayer) {
			if (!GrappleController.start(serverLevel, serverPlayer, hand, stack)) {
				return InteractionResult.FAIL;
			}
		}

		player.awardStat(Stats.ITEM_USED.get(this));
		stack.causeUseVibration(player, GameEvent.ITEM_INTERACT_START);
		return InteractionResult.CONSUME;
	}
}
