package tech.nevets.jaml;

import tech.nevets.jaml.events.LaunchEvent;
import tech.nevets.jaml.objects.Profile;
import tech.nevets.jaml.objects.Version;
import tech.nevets.jaml.util.CacheUtils;
import tech.nevets.jaml.util.ProfileUtils;
import tech.nevets.jaml.util.VersionUtils;

import java.io.BufferedReader;
import java.io.File;
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;
    private final Profile profile = ProfileUtils.getActiveProfile();
    //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, "1.19", garbageCollection);

            StringBuilder sb = new StringBuilder();

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

            String command2 = sb.toString();

            System.out.println(command2);

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

            ProcessBuilder processBuilder = new ProcessBuilder();
            processBuilder.directory(ProfileUtils.activeProfile.getGamePath());
            processBuilder.command(command2);
            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 version, String garbageCollection) {
        StringBuilder sb = new StringBuilder();
        if (new File(JAML.path + "/cache/" + version + ".json").exists()) {
            String[] libs = CacheUtils.getCache(version);
            for (String lib : libs) {
                if (lib.contains("linux") || lib.contains("unix") || lib.contains("mac")) {
                    continue;
                }
                sb.append("libraries/");
                sb.append(lib);
                sb.append(";");
            }
            sb.setLength(sb.length() - 1);
        } else {
            VersionUtils.getLibs();
            String[] libs = CacheUtils.getCache(version);
            for (String lib : libs) {
                if (lib.contains("linux") || lib.contains("unix") || lib.contains("mac")) {
                    continue;
                }
                sb.append("libraries/");
                sb.append(lib);
                sb.append(";");
            }
            sb.setLength(sb.length() - 1);
        }
        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");
        cmd.add("-Dlog4j.configurationFile=assets/log_configs/client-1.12.xml");
        cmd.add("-Djava.library.path=versions/" + version + "/natives");
        cmd.add("-cp");
        cmd.add(sb.toString());
        cmd.add("net.minecraft.client.main.Main");
        cmd.add("--username " + "SteveO718" + " --version " + version + " --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);
        }
    }
}


