package net.minecraft.world.item;

import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.StringTag;
import net.minecraft.world.entity.Entity;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.LecternBlock;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.InteractionResult;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TranslatableComponent;
import java.util.List;
import net.minecraft.world.level.Level;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.util.StringUtil;
import net.minecraft.network.chat.Component;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;

public class WrittenBookItem extends Item {
    public WrittenBookItem(final Properties a) {
        super(a);
    }
    
    public static boolean makeSureTagIsValid(@Nullable final CompoundTag le) {
        if (!WritableBookItem.makeSureTagIsValid(le)) {
            return false;
        }
        if (!le.contains("title", 8)) {
            return false;
        }
        final String string2 = le.getString("title");
        return string2.length() <= 32 && le.contains("author", 8);
    }
    
    public static int getGeneration(final ItemStack bki) {
        return bki.getTag().getInt("generation");
    }
    
    public static int getPageCount(final ItemStack bki) {
        final CompoundTag le2 = bki.getTag();
        return (le2 != null) ? le2.getList("pages", 8).size() : 0;
    }
    
    @Override
    public Component getName(final ItemStack bki) {
        if (bki.hasTag()) {
            final CompoundTag le3 = bki.getTag();
            final String string4 = le3.getString("title");
            if (!StringUtil.isNullOrEmpty(string4)) {
                return new TextComponent(string4);
            }
        }
        return super.getName(bki);
    }
    
    @Override
    public void appendHoverText(final ItemStack bki, @Nullable final Level bqb, final List<Component> list, final TooltipFlag bls) {
        if (bki.hasTag()) {
            final CompoundTag le6 = bki.getTag();
            final String string7 = le6.getString("author");
            if (!StringUtil.isNullOrEmpty(string7)) {
                list.add(new TranslatableComponent("book.byAuthor", new Object[] { string7 }).withStyle(ChatFormatting.GRAY));
            }
            list.add(new TranslatableComponent("book.generation." + le6.getInt("generation")).withStyle(ChatFormatting.GRAY));
        }
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext blv) {
        final Level bqb3 = blv.getLevel();
        final BlockPos fu4 = blv.getClickedPos();
        final BlockState cfj5 = bqb3.getBlockState(fu4);
        if (cfj5.is(Blocks.LECTERN)) {
            return LecternBlock.tryPlaceBook(bqb3, fu4, cfj5, blv.getItemInHand()) ? InteractionResult.sidedSuccess(bqb3.isClientSide) : InteractionResult.PASS;
        }
        return InteractionResult.PASS;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        final ItemStack bki5 = bec.getItemInHand(anf);
        bec.openItemGui(bki5, anf);
        bec.awardStat(Stats.ITEM_USED.get(this));
        return InteractionResultHolder.<ItemStack>sidedSuccess(bki5, bqb.isClientSide());
    }
    
    public static boolean resolveBookComponents(final ItemStack bki, @Nullable final CommandSourceStack cz, @Nullable final Player bec) {
        final CompoundTag le4 = bki.getTag();
        if (le4 == null || le4.getBoolean("resolved")) {
            return false;
        }
        le4.putBoolean("resolved", true);
        if (!makeSureTagIsValid(le4)) {
            return false;
        }
        final ListTag lk5 = le4.getList("pages", 8);
        for (int integer6 = 0; integer6 < lk5.size(); ++integer6) {
            final String string7 = lk5.getString(integer6);
            Component mr8;
            try {
                mr8 = Component.Serializer.fromJsonLenient(string7);
                mr8 = ComponentUtils.updateForEntity(cz, mr8, bec, 0);
            }
            catch (Exception exception9) {
                mr8 = new TextComponent(string7);
            }
            lk5.set(integer6, StringTag.valueOf(Component.Serializer.toJson(mr8)));
        }
        le4.put("pages", lk5);
        return true;
    }
    
    @Override
    public boolean isFoil(final ItemStack bki) {
        return true;
    }
}
