package com.r3944realms.leashedplayer.content.items.type;

import com.r3944realms.leashedplayer.content.entities.LeashRopeArrow;
import com.r3944realms.leashedplayer.modInterface.PlayerLeashable;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Leashable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ShearsItem;
import net.minecraft.world.item.component.Tool;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class LeadBreakerItem extends ShearsItem {

    public LeadBreakerItem(Properties properties) {
        super(properties);
    }

    public static Tool createToolProperties() {
        return new Tool(
                List.of(
                        Tool.Rule.minesAndDrops(List.of(Blocks.COBWEB), 18.0F),
                        Tool.Rule.overrideSpeed(BlockTags.LEAVES, 25.0F),
                        Tool.Rule.overrideSpeed(BlockTags.WOOL, 8.0F),
                        Tool.Rule.overrideSpeed(List.of(Blocks.VINE, Blocks.GLOW_LICHEN), 4.0F)
                ),
                1.0F,
                1
        );
    }
    @Override
    public @NotNull InteractionResult interactLivingEntity(@NotNull ItemStack stack, @NotNull Player player, @NotNull LivingEntity entity, net.minecraft.world.@NotNull InteractionHand hand) {
        if(!entity.level().isClientSide) {
            if (entity instanceof PlayerLeashable playerLeashable) {
                if (playerLeashable.isLeashed()){
                    playerLeashable.dropLeash(true, !(playerLeashable.getLeashHolder() instanceof LeashRopeArrow));
                    if (playerLeashable.getLeashHolder() instanceof LeashRopeArrow leashRopeArrow) {
                        leashRopeArrow.setOwner(null);
                    }
                    if (!player.isCreative()) stack.hurtAndBreak(1, player, LivingEntity.getSlotForHand(hand));
                    return InteractionResult.SUCCESS;
                } else return InteractionResult.PASS;
            } else if (entity instanceof Leashable leashable) {
                if (leashable.isLeashed()){
                    leashable.dropLeash(true, !(leashable.getLeashHolder() instanceof LeashRopeArrow));
                    if (leashable.getLeashHolder() instanceof LeashRopeArrow leashRopeArrow) {
                        leashRopeArrow.setOwner(null);
                    }
                    if (!player.isCreative()) stack.hurtAndBreak(1, player, LivingEntity.getSlotForHand(hand));
                    return InteractionResult.SUCCESS;
                } else return InteractionResult.PASS;
            }
        }
        return InteractionResult.PASS;
    }

}
