package tech.nevets.jaml;

import tech.nevets.jaml.events.LaunchEvent;
import tech.nevets.jaml.util.ProfileUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Launcher {
    private int jdkVersion;
    private int memoryAmount;
    private String garbageCollection;
    private boolean printConsole;
    //TODO add progressbar for launch progress
    //TODO add console output option

    public Launcher() {
    }

    public void setJdkVersion(int jdkVersion) {
        this.jdkVersion = jdkVersion;
    }

    public int getJdkVersion() {
        return jdkVersion;
    }

    public void setMemoryAmount(int memoryAmount) {
        this.memoryAmount = memoryAmount;
    }

    public int getMemoryAmount() {
        return memoryAmount;
    }

    public void setGarbageCollection(String garbageCollection) {
        this.garbageCollection = garbageCollection;
    }

    public String getGarbageCollection() {
        return garbageCollection;
    }

    public void setPrintConsole(boolean printConsole) {
        this.printConsole = printConsole;
    }

    public boolean getPrintConsole() {
        return printConsole;
    }

    public void launch() {
        try {
            ArrayList<String> commandArray = parseCommand(jdkVersion, memoryAmount, garbageCollection);

            StringBuilder sb = new StringBuilder();

            for (String commandString : commandArray) {
                sb.append(commandString);
                sb.append(" ");
            }

            String command = sb.toString();

            System.out.println(command);

            LaunchEvent e = new LaunchEvent();
            e.triggerEvent();

            ProcessBuilder processBuilder = new ProcessBuilder();
            processBuilder.directory(ProfileUtils.activeProfile.getGamePath());
            processBuilder.command(command);
            Process process = processBuilder.start();

            if (printConsole) {
                printResults(process);
            }

            process.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ArrayList<String> parseCommand(int jdkVersion, int memoryAmount, String garbageCollection) {
        ArrayList<String> cmd = new ArrayList<>();
        if (jdkVersion == 8) {
            cmd.add((Config.getConfig().getString("java.jdk8") + "\\java.exe\""));
        } else if (jdkVersion == 16) {
            cmd.add((Config.getConfig().getString("java.jdk16") + "\\java.exe\""));
        } else if (jdkVersion == 17) {
            cmd.add((Config.getConfig().getString("java.jdk17") + "\\java.exe\""));
        }

        cmd.add(("-Xmx" + memoryAmount + "M"));
        cmd.add(("-Xms" + memoryAmount + "M"));
        //cmd.add((garbageCollection));
        cmd.add("-Dlog4j2.formatMsgNoLookups=true -Dlog4j.configurationFile=assets\\log_configs\\client-1.12.xml");
        cmd.add("-Djava.library.path=versions\\1.18.1\\natives\" \"-cp\" \"libraries\\com\\mojang\\blocklist\\1.0.6\\blocklist-1.0.6.jar;libraries\\com\\mojang\\patchy\\2.1.6\\patchy-2.1.6.jar;libraries\\com\\github\\oshi\\oshi-core\\5.8.2\\oshi-core-5.8.2.jar;libraries\\net\\java\\dev\\jna\\jna\\5.9.0\\jna-5.9.0.jar;libraries\\net\\java\\dev\\jna\\jna-platform\\5.9.0\\jna-platform-5.9.0.jar;libraries\\org\\slf4j\\slf4j-api\\1.8.0-beta4\\slf4j-api-1.8.0-beta4.jar;libraries\\org\\apache\\logging\\log4j\\log4j-slf4j18-impl\\2.14.1\\log4j-slf4j18-impl-2.14.1.jar;libraries\\com\\ibm\\icu\\icu4j\\69.1\\icu4j-69.1.jar;libraries\\com\\mojang\\javabridge\\1.2.24\\javabridge-1.2.24.jar;libraries\\net\\sf\\jopt-simple\\jopt-simple\\5.0.4\\jopt-simple-5.0.4.jar;libraries\\io\\netty\\netty-all\\4.1.68.Final\\netty-all-4.1.68.Final.jar;libraries\\com\\google\\guava\\failureaccess\\1.0.1\\failureaccess-1.0.1.jar;libraries\\com\\google\\guava\\guava\\31.0.1-jre\\guava-31.0.1-jre.jar;libraries\\org\\apache\\commons\\commons-lang3\\3.12.0\\commons-lang3-3.12.0.jar;libraries\\commons-io\\commons-io\\2.11.0\\commons-io-2.11.0.jar;libraries\\commons-codec\\commons-codec\\1.15\\commons-codec-1.15.jar;libraries\\com\\mojang\\brigadier\\1.0.18\\brigadier-1.0.18.jar;libraries\\com\\mojang\\datafixerupper\\4.0.26\\datafixerupper-4.0.26.jar;libraries\\com\\google\\code\\gson\\gson\\2.8.8\\gson-2.8.8.jar;libraries\\com\\mojang\\authlib\\3.2.38\\authlib-3.2.38.jar;libraries\\org\\apache\\commons\\commons-compress\\1.21\\commons-compress-1.21.jar;libraries\\org\\apache\\httpcomponents\\httpclient\\4.5.13\\httpclient-4.5.13.jar;libraries\\commons-logging\\commons-logging\\1.2\\commons-logging-1.2.jar;libraries\\org\\apache\\httpcomponents\\httpcore\\4.4.14\\httpcore-4.4.14.jar;libraries\\it\\unimi\\dsi\\fastutil\\8.5.6\\fastutil-8.5.6.jar;libraries\\org\\apache\\logging\\log4j\\log4j-api\\2.14.1\\log4j-api-2.14.1.jar;libraries\\org\\apache\\logging\\log4j\\log4j-core\\2.14.1\\log4j-core-2.14.1.jar;libraries\\org\\lwjgl\\lwjgl\\3.2.2\\lwjgl-3.2.2.jar;libraries\\org\\lwjgl\\lwjgl-jemalloc\\3.2.2\\lwjgl-jemalloc-3.2.2.jar;libraries\\org\\lwjgl\\lwjgl-openal\\3.2.2\\lwjgl-openal-3.2.2.jar;libraries\\org\\lwjgl\\lwjgl-opengl\\3.2.2\\lwjgl-opengl-3.2.2.jar;libraries\\org\\lwjgl\\lwjgl-glfw\\3.2.2\\lwjgl-glfw-3.2.2.jar;libraries\\org\\lwjgl\\lwjgl-stb\\3.2.2\\lwjgl-stb-3.2.2.jar;libraries\\org\\lwjgl\\lwjgl-tinyfd\\3.2.2\\lwjgl-tinyfd-3.2.2.jar;libraries\\com\\mojang\\text2speech\\1.11.3\\text2speech-1.11.3.jar;versions\\1.18.1\\1.18.1.jar\"");
        cmd.add("net.minecraft.client.main.Main");
        cmd.add("--username Name --version 1.18.1 --gameDir . --assetsDir assets --assetIndex 1.18 --uuid 0f28983a46ce33b1aed45cdc95bf44c3 --accessToken 00000000000000000000000000000000 --clientId 0000 --xuid 0000 --userType mojang --versionType release");

        return cmd;
    }

    private static void printResults(Process process) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }
}


