/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  it.unimi.dsi.fastutil.io.FastByteArrayOutputStream
 *  net.minecraft.class_2487
 *  net.minecraft.class_2499
 *  net.minecraft.class_2505
 *  net.minecraft.class_2507
 *  net.minecraft.class_2519
 *  net.minecraft.class_2520
 *  net.minecraft.class_2561
 *  net.minecraft.class_364
 *  net.minecraft.class_4185$class_7840
 *  net.minecraft.class_437
 *  net.minecraft.class_473
 *  org.lwjgl.glfw.GLFW
 *  org.spongepowered.asm.mixin.Final
 *  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.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.List;
import meteordevelopment.meteorclient.MeteorClient;
import net.minecraft.class_2487;
import net.minecraft.class_2499;
import net.minecraft.class_2505;
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_4185;
import net.minecraft.class_437;
import net.minecraft.class_473;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Final;
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_473.class})
public abstract class BookEditScreenMixin
extends class_437 {
    @Shadow
    @Final
    private List<String> field_17116;
    @Shadow
    private int field_2840;

    @Shadow
    protected abstract void method_71537();

    public BookEditScreenMixin(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();
            this.field_17116.stream().map(class_2519::method_23256).forEach(arg_0 -> listTag.add(arg_0));
            class_2487 tag = new class_2487();
            tag.method_10566("pages", (class_2520)listTag);
            tag.method_10569("currentPage", this.field_2840);
            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();
            }
            try {
                GLFW.glfwSetClipboardString((long)MeteorClient.mc.method_22683().method_4490(), (CharSequence)Base64.getEncoder().encodeToString(bytes.array));
            }
            catch (OutOfMemoryError exception) {
                GLFW.glfwSetClipboardString((long)MeteorClient.mc.method_22683().method_4490(), (CharSequence)exception.toString());
            }
        }).method_46433(4, 4).method_46437(120, 20).method_46431());
        this.method_37063((class_364)new class_4185.class_7840((class_2561)class_2561.method_43470((String)"Paste"), button -> {
            byte[] bytes;
            String clipboard = GLFW.glfwGetClipboardString((long)MeteorClient.mc.method_22683().method_4490());
            if (clipboard == null) {
                return;
            }
            try {
                bytes = Base64.getDecoder().decode(clipboard);
            }
            catch (IllegalArgumentException ignored) {
                return;
            }
            DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes));
            try {
                class_2487 tag = class_2507.method_10629((InputStream)in, (class_2505)class_2505.method_53898());
                class_2499 listTag = tag.method_68569("pages").method_10612();
                this.field_17116.clear();
                for (int i = 0; i < listTag.size(); ++i) {
                    this.field_17116.add(listTag.method_68577(i, ""));
                }
                if (this.field_17116.isEmpty()) {
                    this.field_17116.add("");
                }
                this.field_2840 = tag.method_68083("currentPage", 0);
                this.method_71537();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }).method_46433(4, 26).method_46437(120, 20).method_46431());
    }
}

