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

import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.item.alchemy.Potions;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Optional;

public class TippedLeashRopeArrow extends LeashRopeArrowItem {
    public static final String TIPPED_LEASH_ROPE_ARROW_NAME = "item.tipped_leash_rope_arrow.name";
    public static final String DESC = "item.tipped_leash_rope_arrow.desc";
    public TippedLeashRopeArrow(Item.Properties pProperties) {
        super(pProperties);
    }
    @Override
    public @NotNull ItemStack getDefaultInstance() {
        ItemStack itemstack = super.getDefaultInstance();
        itemstack.set(DataComponents.POTION_CONTENTS, new PotionContents(Potions.POISON));
        return itemstack;
    }

    @Override
    public void appendHoverText(@NotNull ItemStack pStack, Item.@NotNull TooltipContext pContext, @NotNull List<Component> pTooltipComponents, @NotNull TooltipFlag pTooltipFlag) {
        pTooltipComponents.addAll(List.of(
                Component.translatable(LeashRopeArrowItem.DESC_V_1),
                Component.translatable(LeashRopeArrowItem.DESC_V_2),
                Component.translatable(DESC)
        ));
        PotionContents potioncontents = pStack.get(DataComponents.POTION_CONTENTS);
        if (potioncontents != null) {
            potioncontents.addPotionTooltip(pTooltipComponents::add, 0.125F, pContext.tickRate());
        }

    }

//    @Override
//    public @NotNull String getDescriptionId(ItemStack pStack) {
//        return Potion.getName(pStack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).potion(), this.getDescriptionId() + ".effect.");
//    }

    @Override
    public @NotNull Component getName(@NotNull ItemStack pStack) {
        Optional<Holder<Potion>> pPotion = pStack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY).potion();
        String s = null,s1;
        if (pPotion.isPresent()) {
            s = pPotion.get().value().name;
        }
        boolean flag = s == null;
        s1 = pPotion.flatMap(Holder::unwrapKey).map(p_331494_ -> p_331494_.location().getPath()).orElse("empty");
        String potionTranslateKey = "item.minecraft.potion.effect." + (flag ? s1: s);
        return Component.translatable(TIPPED_LEASH_ROPE_ARROW_NAME, Component.translatable(potionTranslateKey));
    }

}
