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

import com.r3944realms.leashedplayer.content.entities.LeashRopeArrow;
import com.r3944realms.leashedplayer.datagen.LanguageAndOtherData.ModItemTags;
import com.r3944realms.leashedplayer.modInterface.PlayerLeashable;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.HolderSet;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
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.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.ToolMaterial;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.Tool;
import net.minecraft.world.item.component.TooltipDisplay;
import net.minecraft.world.item.enchantment.Enchantable;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Consumer;

public class LeadBreakerItem extends ShearsItem {
    private final ToolMaterial toolMaterial;
    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(ToolMaterial toolMaterial, Properties properties) {
        super(properties.durability(toolMaterial.durability())
                .component(DataComponents.ENCHANTABLE, new Enchantable(toolMaterial.enchantmentValue()))
                .repairable(ModItemTags.AMETHYST_TOOL_MATERIALS)
                .enchantable(toolMaterial.enchantmentValue()));
        this.toolMaterial = toolMaterial;
    }

    public static Tool createToolProperties() {
        HolderGetter<Block> holdergetter = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.BLOCK);
        return new Tool(
                List.of(
                        Tool.Rule.minesAndDrops(HolderSet.direct(BuiltInRegistries.BLOCK.wrapAsHolder(Blocks.COBWEB)), 18.0F),
                        Tool.Rule.overrideSpeed(holdergetter.getOrThrow(BlockTags.LEAVES), 25.0F),
                        Tool.Rule.overrideSpeed(holdergetter.getOrThrow(BlockTags.WOOL), 8.0F),
                        Tool.Rule.overrideSpeed(HolderSet.direct(BuiltInRegistries.BLOCK.wrapAsHolder(Blocks.VINE), BuiltInRegistries.BLOCK.wrapAsHolder(Blocks.GLOW_LICHEN)), 4.0F)
                ),
                1.0F,
                1,
                true
        );
    }
    @Override
    public @NotNull InteractionResult interactLivingEntity(@NotNull ItemStack stack, @NotNull Player player, @NotNull LivingEntity entity, @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();
                    Leashable.dropLeash((Entity & Leashable) playerLeashable,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((Entity & Leashable) leashable, 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.SUCCESS;
    }

    @Override
    public @NotNull InteractionResult 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)) {
                    Leashable.dropLeash((Entity & Leashable) playerLeashable,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 InteractionResult.SUCCESS;
                }
                ((ServerPlayer)pPlayer).sendSystemMessage(Component.translatable(MESSAGE_USE_FAI, pPlayer.getDisplayName(), leashHolder.getDisplayName()), true);
            }
        }
        if(pLevel.isClientSide) {
            PlayerLeashable playerLeashable = (PlayerLeashable) pPlayer;
            if (playerLeashable.getLeashData() != null) {
                pPlayer.playSound(SoundEvents.SHEEP_SHEAR, 1.0F, 1.0F);
                return InteractionResult.SUCCESS;
            }
        }
        return super.use(pLevel, pPlayer, pUsedHand);
    }

    @Override
    public void appendHoverText(@NotNull ItemStack pItemStack, @NotNull TooltipContext pTooltipComponents, @NotNull TooltipDisplay pTooltipDisplay, Consumer<Component> pConsumer, @NotNull TooltipFlag pTooltipFlag) {
        pConsumer.accept(Component.translatable(HOVER_KEY));
    }
}
