package net.minecraft.world.item;

import java.util.AbstractList;
import java.util.Iterator;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.core.NonNullList;
import net.minecraft.world.level.ItemLike;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import net.minecraft.world.item.enchantment.EnchantmentInstance;
import net.minecraft.network.chat.Component;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.world.level.Level;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;

public class EnchantedBookItem extends Item {
    public EnchantedBookItem(final Properties a) {
        super(a);
    }
    
    @Override
    public boolean isFoil(final ItemStack bki) {
        return true;
    }
    
    @Override
    public boolean isEnchantable(final ItemStack bki) {
        return false;
    }
    
    public static ListTag getEnchantments(final ItemStack bki) {
        final CompoundTag le2 = bki.getTag();
        if (le2 != null) {
            return le2.getList("StoredEnchantments", 10);
        }
        return new ListTag();
    }
    
    @Override
    public void appendHoverText(final ItemStack bki, @Nullable final Level bqb, final List<Component> list, final TooltipFlag bls) {
        super.appendHoverText(bki, bqb, list, bls);
        ItemStack.appendEnchantmentNames(list, getEnchantments(bki));
    }
    
    public static void addEnchantment(final ItemStack bki, final EnchantmentInstance bnz) {
        final ListTag lk3 = getEnchantments(bki);
        boolean boolean4 = true;
        final ResourceLocation uh5 = Registry.ENCHANTMENT.getKey(bnz.enchantment);
        for (int integer6 = 0; integer6 < lk3.size(); ++integer6) {
            final CompoundTag le7 = lk3.getCompound(integer6);
            final ResourceLocation uh6 = ResourceLocation.tryParse(le7.getString("id"));
            if (uh6 != null && uh6.equals(uh5)) {
                if (le7.getInt("lvl") < bnz.level) {
                    le7.putShort("lvl", (short)bnz.level);
                }
                boolean4 = false;
                break;
            }
        }
        if (boolean4) {
            final CompoundTag le8 = new CompoundTag();
            le8.putString("id", String.valueOf(uh5));
            le8.putShort("lvl", (short)bnz.level);
            ((AbstractList<CompoundTag>)lk3).add(le8);
        }
        bki.getOrCreateTag().put("StoredEnchantments", lk3);
    }
    
    public static ItemStack createForEnchantment(final EnchantmentInstance bnz) {
        final ItemStack bki2 = new ItemStack(Items.ENCHANTED_BOOK);
        addEnchantment(bki2, bnz);
        return bki2;
    }
    
    @Override
    public void fillItemCategory(final CreativeModeTab biy, final NonNullList<ItemStack> gi) {
        if (biy == CreativeModeTab.TAB_SEARCH) {
            for (final Enchantment bnw5 : Registry.ENCHANTMENT) {
                if (bnw5.category != null) {
                    for (int integer6 = bnw5.getMinLevel(); integer6 <= bnw5.getMaxLevel(); ++integer6) {
                        gi.add(createForEnchantment(new EnchantmentInstance(bnw5, integer6)));
                    }
                }
            }
        }
        else if (biy.getEnchantmentCategories().length != 0) {
            for (final Enchantment bnw5 : Registry.ENCHANTMENT) {
                if (biy.hasEnchantmentCategory(bnw5.category)) {
                    gi.add(createForEnchantment(new EnchantmentInstance(bnw5, bnw5.getMaxLevel())));
                }
            }
        }
    }
}
