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 jt) {
        if (!WritableBookItem.makeSureTagIsValid(jt)) {
            return false;
        }
        if (!jt.contains("title", 8)) {
            return false;
        }
        final String string2 = jt.getString("title");
        return string2.length() <= 32 && jt.contains("author", 8);
    }
    
    public static int getGeneration(final ItemStack ben) {
        return ben.getTag().getInt("generation");
    }
    
    public static int getPageCount(final ItemStack ben) {
        final CompoundTag jt2 = ben.getTag();
        return (jt2 != null) ? jt2.getList("pages", 8).size() : 0;
    }
    
    @Override
    public Component getName(final ItemStack ben) {
        if (ben.hasTag()) {
            final CompoundTag jt3 = ben.getTag();
            final String string4 = jt3.getString("title");
            if (!StringUtil.isNullOrEmpty(string4)) {
                return new TextComponent(string4);
            }
        }
        return super.getName(ben);
    }
    
    @Override
    public void appendHoverText(final ItemStack ben, @Nullable final Level bjw, final List<Component> list, final TooltipFlag bfw) {
        if (ben.hasTag()) {
            final CompoundTag jt6 = ben.getTag();
            final String string7 = jt6.getString("author");
            if (!StringUtil.isNullOrEmpty(string7)) {
                list.add(new TranslatableComponent("book.byAuthor", new Object[] { string7 }).withStyle(ChatFormatting.GRAY));
            }
            list.add(new TranslatableComponent("book.generation." + jt6.getInt("generation"), new Object[0]).withStyle(ChatFormatting.GRAY));
        }
    }
    
    @Override
    public InteractionResult useOn(final UseOnContext bfz) {
        final Level bjw3 = bfz.getLevel();
        final BlockPos fk4 = bfz.getClickedPos();
        final BlockState byj5 = bjw3.getBlockState(fk4);
        if (byj5.getBlock() == Blocks.LECTERN) {
            return LecternBlock.tryPlaceBook(bjw3, fk4, byj5, bfz.getItemInHand()) ? InteractionResult.SUCCESS : InteractionResult.PASS;
        }
        return InteractionResult.PASS;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bjw, final Player ayj, final InteractionHand ajk) {
        final ItemStack ben5 = ayj.getItemInHand(ajk);
        ayj.openItemGui(ben5, ajk);
        ayj.awardStat(Stats.ITEM_USED.get(this));
        return InteractionResultHolder.<ItemStack>success(ben5);
    }
    
    public static boolean resolveBookComponents(final ItemStack ben, @Nullable final CommandSourceStack cq, @Nullable final Player ayj) {
        final CompoundTag jt4 = ben.getTag();
        if (jt4 == null || jt4.getBoolean("resolved")) {
            return false;
        }
        jt4.putBoolean("resolved", true);
        if (!makeSureTagIsValid(jt4)) {
            return false;
        }
        final ListTag jz5 = jt4.getList("pages", 8);
        for (int integer6 = 0; integer6 < jz5.size(); ++integer6) {
            final String string7 = jz5.getString(integer6);
            Component lf8;
            try {
                lf8 = Component.Serializer.fromJsonLenient(string7);
                lf8 = ComponentUtils.updateForEntity(cq, lf8, ayj, 0);
            }
            catch (Exception exception9) {
                lf8 = new TextComponent(string7);
            }
            jz5.set(integer6, StringTag.valueOf(Component.Serializer.toJson(lf8)));
        }
        jt4.put("pages", jz5);
        return true;
    }
    
    @Override
    public boolean isFoil(final ItemStack ben) {
        return true;
    }
}
