/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  it.unimi.dsi.fastutil.io.FastByteArrayOutputStream
 *  net.minecraft.class_1268
 *  net.minecraft.class_1799
 *  net.minecraft.class_1802
 *  net.minecraft.class_2487
 *  net.minecraft.class_2499
 *  net.minecraft.class_2507
 *  net.minecraft.class_2519
 *  net.minecraft.class_2520
 *  net.minecraft.class_2561
 *  net.minecraft.class_364
 *  net.minecraft.class_3872
 *  net.minecraft.class_3872$class_3931
 *  net.minecraft.class_4185$class_7840
 *  net.minecraft.class_437
 *  org.lwjgl.glfw.GLFW
 *  org.lwjgl.system.MemoryStack
 *  org.lwjgl.system.MemoryUtil
 *  org.spongepowered.asm.mixin.Mixin
 *  org.spongepowered.asm.mixin.Shadow
 *  org.spongepowered.asm.mixin.injection.At
 *  org.spongepowered.asm.mixin.injection.Inject
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfo
 */
package meteordevelopment.meteorclient.mixin;

import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.gui.GuiThemes;
import meteordevelopment.meteorclient.gui.screens.EditBookTitleAndAuthorScreen;
import meteordevelopment.meteorclient.utils.player.ChatUtils;
import net.minecraft.class_1268;
import net.minecraft.class_1799;
import net.minecraft.class_1802;
import net.minecraft.class_2487;
import net.minecraft.class_2499;
import net.minecraft.class_2507;
import net.minecraft.class_2519;
import net.minecraft.class_2520;
import net.minecraft.class_2561;
import net.minecraft.class_364;
import net.minecraft.class_3872;
import net.minecraft.class_4185;
import net.minecraft.class_437;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(value={class_3872.class})
public abstract class BookScreenMixin
extends class_437 {
    @Shadow
    private class_3872.class_3931 field_17418;
    @Shadow
    private int field_17119;

    public BookScreenMixin(class_2561 title) {
        super(title);
    }

    @Inject(method={"method_25426"}, at={@At(value="TAIL")})
    private void onInit(CallbackInfo info) {
        this.method_37063((class_364)new class_4185.class_7840((class_2561)class_2561.method_43470((String)"Copy"), button -> {
            class_2499 listTag = new class_2499();
            for (int i = 0; i < this.field_17418.method_17560(); ++i) {
                listTag.add((Object)class_2519.method_23256((String)this.field_17418.method_17563(i).getString()));
            }
            class_2487 tag = new class_2487();
            tag.method_10566("pages", (class_2520)listTag);
            tag.method_10569("currentPage", this.field_17119);
            FastByteArrayOutputStream bytes = new FastByteArrayOutputStream();
            DataOutputStream out = new DataOutputStream((OutputStream)bytes);
            try {
                class_2507.method_55324((class_2520)tag, (DataOutput)out);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            String encoded = Base64.getEncoder().encodeToString(bytes.array);
            long available = MemoryStack.stackGet().getPointer();
            long size = MemoryUtil.memLengthUTF8((CharSequence)encoded, (boolean)true);
            if (size > available) {
                ChatUtils.error("Could not copy to clipboard: Out of memory.", new Object[0]);
            } else {
                GLFW.glfwSetClipboardString((long)MeteorClient.mc.method_22683().method_4490(), (CharSequence)encoded);
            }
        }).method_46433(4, 4).method_46437(120, 20).method_46431());
        class_1799 itemStack = MeteorClient.mc.field_1724.method_6047();
        class_1268 hand = class_1268.field_5808;
        if (itemStack.method_7909() != class_1802.field_8360) {
            itemStack = MeteorClient.mc.field_1724.method_6079();
            hand = class_1268.field_5810;
        }
        if (itemStack.method_7909() != class_1802.field_8360) {
            return;
        }
        class_1799 book = itemStack;
        class_1268 hand2 = hand;
        this.method_37063((class_364)new class_4185.class_7840((class_2561)class_2561.method_43470((String)"Edit title & author"), button -> MeteorClient.mc.method_1507((class_437)new EditBookTitleAndAuthorScreen(GuiThemes.get(), book, hand2))).method_46433(4, 26).method_46437(120, 20).method_46431());
    }
}

