#include "forge1_12_2.h"

#include <QDebug>
#include <QObject>
#include <QWidget>
#include <QDir>
#include <QCoreApplication>
#include <QString>
#include <QList>
#include <QOperatingSystemVersion>
#include <QStringList>


forge1_12_2::forge1_12_2() {
	#ifdef Q_OS_WIN
		classpath_separator = ';';
	#else
		classpath_separator = ':';
	#endif
}

forge1_12_2::~forge1_12_2() {
}

QStringList forge1_12_2::getCrossPlatformArgs(QString &RAM, QString &username, QString &uuid, QString &access_token, QString &garbarge_collector) {
    QStringList jarPaths = {
		   "../libraries/1.12.2/net/minecraftforge/forge/1.12.2-14.23.5.2860/forge-1.12.2-14.23.5.2860.jar",
		   "../libraries/1.12.2/org/ow2/asm/asm-debug-all/5.2/asm-debug-all-5.2.jar",
		   "../libraries/1.12.2/net/minecraft/launchwrapper/1.12/launchwrapper-1.12.jar",
		   "../libraries/1.12.2/org/jline/jline/3.5.1/jline-3.5.1.jar",
		   "../libraries/1.12.2/com/typesafe/akka/akka-actor_2.11/2.3.3/akka-actor_2.11-2.3.3.jar",
		   "../libraries/1.12.2/com/typesafe/config/1.2.1/config-1.2.1.jar",
		   "../libraries/1.12.2/org/scala-lang/scala-actors-migration_2.11/1.1.0/scala-actors-migration_2.11-1.1.0.jar",
		   "../libraries/1.12.2/org/scala-lang/scala-compiler/2.11.1/scala-compiler-2.11.1.jar",
		   "../libraries/1.12.2/org/scala-lang/plugins/scala-continuations-library_2.11/1.0.2_mc/scala-continuations-library_2.11-1.0.2_mc.jar",
		   "../libraries/1.12.2/org/scala-lang/plugins/scala-continuations-plugin_2.11.1/1.0.2_mc/scala-continuations-plugin_2.11.1-1.0.2_mc.jar",
		   "../libraries/1.12.2/org/scala-lang/scala-library/2.11.1/scala-library-2.11.1.jar",
		   "../libraries/1.12.2/org/scala-lang/scala-parser-combinators_2.11/1.0.1/scala-parser-combinators_2.11-1.0.1.jar",
		   "../libraries/1.12.2/org/scala-lang/scala-reflect/2.11.1/scala-reflect-2.11.1.jar",
		   "../libraries/1.12.2/org/scala-lang/scala-swing_2.11/1.0.1/scala-swing_2.11-1.0.1.jar",
		   "../libraries/1.12.2/org/scala-lang/scala-xml_2.11/1.0.2/scala-xml_2.11-1.0.2.jar",
		   "../libraries/1.12.2/lzma/lzma/0.0.1/lzma-0.0.1.jar",
		   "../libraries/1.12.2/java3d/vecmath/1.5.2/vecmath-1.5.2.jar",
		   "../libraries/1.12.2/net/sf/trove4j/trove4j/3.0.3/trove4j-3.0.3.jar",
		   "../libraries/1.12.2/org/apache/maven/maven-artifact/3.5.3/maven-artifact-3.5.3.jar",
		   "../libraries/1.12.2/net/sf/jopt-simple/jopt-simple/5.0.3/jopt-simple-5.0.3.jar",
		   "../libraries/1.12.2/org/apache/logging/log4j/log4j-api/2.15.0/log4j-api-2.15.0.jar",
		   "../libraries/1.12.2/org/apache/logging/log4j/log4j-core/2.15.0/log4j-core-2.15.0.jar",
		   "../libraries/1.12.2/com/mojang/patchy/1.3.9/patchy-1.3.9.jar",
		   "../libraries/1.12.2/oshi-project/oshi-core/1.1/oshi-core-1.1.jar",
		   "../libraries/1.12.2/net/java/dev/jna/jna/4.4.0/jna-4.4.0.jar",
		   "../libraries/1.12.2/net/java/dev/jna/platform/3.4.0/platform-3.4.0.jar",
		   "../libraries/1.12.2/com/ibm/icu/icu4j-core-mojang/51.2/icu4j-core-mojang-51.2.jar",
		   "../libraries/1.12.2/com/paulscode/codecjorbis/20101023/codecjorbis-20101023.jar",
		   "../libraries/1.12.2/com/paulscode/codecwav/20101023/codecwav-20101023.jar",
		   "../libraries/1.12.2/com/paulscode/libraryjavasound/20101123/libraryjavasound-20101123.jar",
		   "../libraries/1.12.2/com/paulscode/librarylwjglopenal/20100824/librarylwjglopenal-20100824.jar",
		   "../libraries/1.12.2/com/paulscode/soundsystem/20120107/soundsystem-20120107.jar",
		   "../libraries/1.12.2/io/netty/netty-all/4.1.9.Final/netty-all-4.1.9.Final.jar",
		   "../libraries/1.12.2/com/google/guava/guava/21.0/guava-21.0.jar",
		   "../libraries/1.12.2/org/apache/commons/commons-lang3/3.5/commons-lang3-3.5.jar",
		   "../libraries/1.12.2/commons-io/commons-io/2.5/commons-io-2.5.jar",
		   "../libraries/1.12.2/commons-codec/commons-codec/1.10/commons-codec-1.10.jar",
		   "../libraries/1.12.2/net/java/jinput/jinput/2.0.5/jinput-2.0.5.jar",
		   "../libraries/1.12.2/net/java/jutils/jutils/1.0.0/jutils-1.0.0.jar",
		   "../libraries/1.12.2/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar",
		   "../libraries/1.12.2/com/mojang/authlib/1.5.25/authlib-1.5.25.jar",
		   "../libraries/1.12.2/com/mojang/realms/1.10.22/realms-1.10.22.jar",
		   "../libraries/1.12.2/org/apache/commons/commons-compress/1.8.1/commons-compress-1.8.1.jar",
		   "../libraries/1.12.2/org/apache/httpcomponents/httpclient/4.3.3/httpclient-4.3.3.jar",
		   "../libraries/1.12.2/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar",
		   "../libraries/1.12.2/org/apache/httpcomponents/httpcore/4.3.2/httpcore-4.3.2.jar",
		   "../libraries/1.12.2/it/unimi/dsi/fastutil/7.1.0/fastutil-7.1.0.jar",
		   "../libraries/1.12.2/org/lwjgl/lwjgl/lwjgl/2.9.4-nightly-20150209/lwjgl-2.9.4-nightly-20150209.jar",
		   "../libraries/1.12.2/org/lwjgl/lwjgl/lwjgl_util/2.9.4-nightly-20150209/lwjgl_util-2.9.4-nightly-20150209.jar",
		   "../libraries/1.12.2/com/mojang/text2speech/1.10.3/text2speech-1.10.3.jar",
		   "../versions/1.12.2-forge-14.23.5.2860/1.12.2-forge-14.23.5.2860.jar"
    };

    QStringList nativePaths;
    for (const QString &path : jarPaths) { nativePaths << QDir::toNativeSeparators(path); }
    QString classPath = nativePaths.join(classpath_separator);

	QStringList arguments;
	arguments
		<< "-Djava.library.path=" + QDir::toNativeSeparators("../versions/1.12.2-forge-14.23.5.2860/natives")
		<< "-Xmx" + RAM
		<< "-cp"
		<< classPath
		<< "-Dfml.ignoreInvalidMinecraftCertificates=true"
		<< "-Dfml.ignorePatchDiscrepancies=true"
		<< "-Djava.net.preferIPv4Stack=true"
		<< "-Dminecraft.applet.TargetDirectory=" + QDir::toNativeSeparators("./")
		<< "-DlibraryDirectory=" + QDir::toNativeSeparators("../libraries/1.12.2")
		<< "-Dlog4j.configurationFile=" + QDir::toNativeSeparators("./assets/1.12.2/log_configs/client-1.12.xml")
		// Garbarge Collector общие настройки
		<< "-XX:+UnlockExperimentalVMOptions"
		//<< "-XX:+UnlockDiagnosticVMOptions"
		<< "-XX:+DisableExplicitGC"
		<< "-XX:ReservedCodeCacheSize=400M"
		<< "-XX:+AlwaysPreTouch"
		<< "-XX:+UseNUMA"
		//<< "-XX:+UseFastUnorderedTimeStamps" // Не работает
		//<< "-server" // На счёт него не уверен
		<< "-XX:+PerfDisableSharedMem"
		<< "-XX:AllocatePrefetchStyle=3";
		
	// Garbarge Collector
	if (garbarge_collector == "CMS (Concurrent Mark-Sweep)") {
		// CMS (Concurrent Mark-Sweep)
		arguments
			<< "-XX:+UseConcMarkSweepGC" 
			<< "-XX:CMSInitiatingOccupancyFraction=75" 
			<< "-XX:+UseCMSInitiatingOccupancyOnly"
			<< "-XX:CMSWaitDuration=1000" 
			<< "-XX:+CMSConcurrentMTEnabled";
	}
	else if (garbarge_collector == "G1GC (Garbage First Garbage Collector)") {
		// G1GC (Garbage First Garbage Collector)
		arguments
			<< "-XX:+UseG1GC"
			<< "-XX:G1NewSizePercent=20"
			<< "-XX:G1ReservePercent=20"
			<< "-XX:MaxGCPauseMillis=50"
			<< "-XX:G1HeapRegionSize=32M";
	}
	else if (garbarge_collector == "Azul's Continuously Concurrent Compacting Collector (C4)") {
		// C4 - Не поддерживается java8
		arguments
			<< "-XX:+UseC4GC" 
			<< "-XX:MaxHeapSize=2G" 
			<< "-XX:InitialHeapSize=1G";
	}
 
	arguments
		<< "net.minecraft.launchwrapper.Launch"
		<< "--username" << username
		<< "--version" << "1.12.2-forge-14.23.5.2860"
		<< "--gameDir" << QDir::toNativeSeparators("./")
		<< "--assetsDir" << QDir::toNativeSeparators("../assets/1.12.2")
		<< "--assetIndex" << "1.12"
		<< "--uuid" << uuid
		<< "--accessToken" << access_token
		<< "--userType" << "mojang"

		<< "--tweakClass" << "net.minecraftforge.fml.common.launcher.FMLTweaker"

		<< "--versionType" << "Forge"
		<< "--height" << "480"
		<< "--width" << "854";


	return arguments;
}
