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.server.level.ServerPlayer;
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.Entity;
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.Tier;
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 implements ITierTool{
    private final Tier tier;
    public static final String HOVER_KEY = "leashedplayer.lead_breaker.item.desc",
                        MESSAGE_USE_SUF = "leashedplayer.lead_breaker.item.use_suf",
                        MESSAGE_USE_FAI = "leashedplayer.lead_breaker.item.use_fai";
    public LeadBreakerItem(Tier tier, Properties properties) {
        super(properties.durability(tier.getUses()));
        this.tier = tier;
    }

    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);
                    Entity leashHolder = playerLeashable.getLeashHolder();
                    playerLeashable.dropLeash(true, !(leashHolder instanceof LeashRopeArrow));
                    if (leashHolder instanceof LeashRopeArrow leashRopeArrow) {
                        leashRopeArrow.setOwner(null);
                    }
                    if (!player.isCreative()) stack.hurtAndBreak(1, player, LivingEntity.getSlotForHand(hand));
                    if (leashHolder != null) {
                       ((ServerPlayer)player).sendSystemMessage(Component.translatable(MESSAGE_USE_SUF, entity.getDisplayName(), leashHolder.getDisplayName()), true);
                    }
                    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);
                    Entity leashHolder = leashable.getLeashHolder();
                    leashable.dropLeash(true, !(leashHolder instanceof LeashRopeArrow));
                    if (leashable.getLeashHolder() instanceof LeashRopeArrow leashRopeArrow) {
                        leashRopeArrow.setOwner(null);
                    }
                    if (!player.isCreative()) stack.hurtAndBreak(1, player, LivingEntity.getSlotForHand(hand));
                    if (leashHolder != null) {
                        ((ServerPlayer)player).sendSystemMessage(Component.translatable(MESSAGE_USE_SUF, entity.getDisplayName(), leashHolder.getDisplayName()), true);
                    }
                    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()) {
                Entity leashHolder = playerLeashable.getLeashHolder();
                if(!(leashHolder instanceof LeashRopeArrow)) {
                    playerLeashable.dropLeash(true, true);
                    pPlayer.getItemInHand(pUsedHand).hurtAndBreak(10, pPlayer, LivingEntity.getSlotForHand(pUsedHand));
                    if (leashHolder != null) {
                        ((ServerPlayer)pPlayer).sendSystemMessage(Component.translatable(MESSAGE_USE_SUF, pPlayer.getDisplayName(), leashHolder.getDisplayName()), true);
                    }
                    return InteractionResultHolder.success(pPlayer.getItemInHand(pUsedHand));
                }
                ((ServerPlayer)pPlayer).sendSystemMessage(Component.translatable(MESSAGE_USE_FAI, pPlayer.getDisplayName(), leashHolder.getDisplayName()), true);
            }
        }
        if(pLevel.isClientSide) {
            PlayerLeashable playerLeashable = (PlayerLeashable) pPlayer;
            if (playerLeashable.getLeashDataFromEntityData() != null) {
                pPlayer.playSound(SoundEvents.SHEEP_SHEAR, 1.0F, 1.0F);
            }
        }
        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));
    }

    @Override
    public Tier getTier() {
        return this.tier;
    }

    @Override
    public int getEnchantmentValue(@NotNull ItemStack stack) {
        return this.tier.getEnchantmentValue();
    }

    @Override
    public boolean isValidRepairItem(@NotNull ItemStack pStack, @NotNull ItemStack pRepairCandidate) {
        return this.tier.getRepairIngredient().test(pRepairCandidate) || super.isValidRepairItem(pRepairCandidate, pStack);
    }
}
