package com.astatin3.meteorbot.modules;

import com.astatin3.meteorbot.Addon;
import meteordevelopment.meteorclient.settings.BoolSetting;
import meteordevelopment.meteorclient.settings.Setting;
import meteordevelopment.meteorclient.settings.SettingGroup;
import meteordevelopment.meteorclient.settings.StringSetting;
import meteordevelopment.meteorclient.systems.modules.Module;
import net.minecraft.util.math.Vec3d;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


public class ModuleExample extends Module {
    public ModuleExample() {
        super(Addon.CATEGORY, "Meteor-Bot", "An example module in a custom category.");
    }

    private final SettingGroup sgGeneral = settings.getDefaultGroup();

    private final Setting<String> botpath = sgGeneral.add(new StringSetting.Builder()
        .name("Path")
        .description("The location of the bot .minecraft folder")
        .defaultValue("/home/astatin3/GitHub/meteorbot/instance")
        .visible(() -> true)
        .build()
    );

    private final Setting<Boolean> verboseMode = sgGeneral.add(new BoolSetting.Builder()
        .name("Verbose")
        .description("Enable verbose output")
        .defaultValue(true)
        .visible(() -> true)
        .build()
    );

    private Process process;
    private BufferedOutputStream stdin;

    private Thread cmdThread = null;


    public void startBot(){
        try {
            // Set up the classpath and main class
            String minecraftJar = "path/to/minecraft.jar";
            String mainClass = "net.minecraft.client.main.Main";

            // Set up JVM arguments
            List<String> jvmArgs = new ArrayList<>();
            jvmArgs.add("java");
            jvmArgs.add("-Xmx2G"); // Adjust max heap size as needed
            jvmArgs.add("-XX:+UnlockExperimentalVMOptions");
            jvmArgs.add("-XX:+UseG1GC");
            jvmArgs.add("-XX:G1NewSizePercent=20");
            jvmArgs.add("-XX:G1ReservePercent=20");
            jvmArgs.add("-XX:MaxGCPauseMillis=50");
            jvmArgs.add("-XX:G1HeapRegionSize=32M");
            jvmArgs.add("-cp");
            jvmArgs.add(minecraftJar);
            jvmArgs.add(mainClass);

            // Add Minecraft arguments
            jvmArgs.add("--username");
            jvmArgs.add("Player");
            jvmArgs.add("--version");
            jvmArgs.add("1.19.4"); // Adjust to your Minecraft version
            jvmArgs.add("--gameDir");
            jvmArgs.add(".");
            jvmArgs.add("--assetsDir");
            jvmArgs.add("assets");
            jvmArgs.add("--assetIndex");
            jvmArgs.add("1.19"); // Adjust to your asset index
            jvmArgs.add("--uuid");
            jvmArgs.add(java.util.UUID.randomUUID().toString());
            jvmArgs.add("--accessToken");
            jvmArgs.add("0"); // Use a valid access token for online play
            jvmArgs.add("--userType");
            jvmArgs.add("mojang");

            // Create process builder
            ProcessBuilder pb = new ProcessBuilder(jvmArgs);
            pb.directory(new File("."));
            pb.inheritIO();

            // Start the process
            process = pb.start();

            // Wait for the process to finish
            int exitCode = process.waitFor();
            info("Minecraft exited with code: " + exitCode);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void startThread(){
        cmdThread = new Thread(() -> {
            startBot();
        });
        cmdThread.start();
    }

    public void stopThread(){
        if(cmdThread != null) {
            cmdThread.interrupt();
            cmdThread = null;
        }
    }


    public void onActivate() {
        startThread();
    }

    @Override
    public void onDeactivate() {
        stopThread();
    }


}

