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

import com.r3944realms.leashedplayer.content.entities.LeashRopeArrow;
import com.r3944realms.leashedplayer.modInterface.PlayerLeashable;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
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.TooltipFlag;
import net.minecraft.world.item.component.Tool;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class LeadBreakerItem extends ShearsItem {
    public static final String HOVER_KEY = "leashedplayer.lead_breaker.item.desc";
    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()){
                    entity.playSound(SoundEvents.SHEEP_SHEAR, 1.0F, 1.0F);
                    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()){
                    entity.playSound(SoundEvents.SHEEP_SHEAR, 1.0F, 1.0F);
                    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;
    }

    @Override
    public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level pLevel, @NotNull Player pPlayer, @NotNull InteractionHand pUsedHand) {
        if (!pLevel.isClientSide && pUsedHand == InteractionHand.MAIN_HAND) {
            PlayerLeashable playerLeashable = (PlayerLeashable) pPlayer;
            if (playerLeashable.isLeashed()) {
                if(!(playerLeashable.getLeashHolder() instanceof LeashRopeArrow)) {
                    pPlayer.playSound(SoundEvents.SHEEP_SHEAR, 1.0F, 1.0F);
                    playerLeashable.dropLeash(true, true);
                    pPlayer.getItemInHand(pUsedHand).hurtAndBreak(10, pPlayer, LivingEntity.getSlotForHand(pUsedHand));
                    return InteractionResultHolder.success(pPlayer.getItemInHand(pUsedHand));
                }
            }
        }
        return super.use(pLevel, pPlayer, pUsedHand);
    }

    @Override
    public void appendHoverText(@NotNull ItemStack pStack, @NotNull TooltipContext pContext, @NotNull List<Component> pTooltipComponents, @NotNull TooltipFlag pTooltipFlag) {
        pTooltipComponents.add(Component.translatable(HOVER_KEY));
    }
}
