
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.Date
import java.text.SimpleDateFormat
import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream
import org.gradle.kotlin.dsl.accessors.runtime.addDependencyTo
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

val mod_version: String by project
val mc_version: String by project
val forge_version: String by project
val mod_id: String by project
val handle_deps: String by project
val use_commit_hash_in_version: String by project
val handleDeps = handle_deps.toBoolean()
val caffeine_cache_version: String by project

plugins {
	java
	kotlin
	idea
	`maven-publish`
	id("net.neoforged.gradle.userdev")
	id("net.neoforged.gradle.mixin")
}

data class GitInfo(val version: String, val tag: String, val buildNumber: String) {
	// val tagIsVersion: Boolean get() = tag != "" && tag.matches(Regex("v[0-9]+\\.[0-9]\\.[0-9]"))

	val publishVersion: String get() {
		if (tag != "")
			return mod_version
		else
			return "$mod_version-SNAPSHOT"
	}

	val jarName: String get() {
		val buildNumber = if (buildNumber != "") "-$buildNumber" else ""

		if (tag != "")
			return "$mod_version-$version$buildNumber"

		// if (count != "")
		// 	return "$mod_version-SNAPSHOT-${version}_$count"

		if (version != "") {
			return "$mod_version-SNAPSHOT-$version$buildNumber"
		} else {
			return "$mod_version-SNAPSHOT$buildNumber"
		}
	}

	val modVersion: String get() {
		return if (buildNumber != "") "$mod_version.$buildNumber" else mod_version
	}
}

val gitVersion = getCommitVersion() ?: GitInfo("", "", "")

version = gitVersion.modVersion
group = "ru.dbotthepony"

fun getCommitVersion(): GitInfo? {
	try {
		val versionStream = FastByteArrayOutputStream()
		val tagStream = FastByteArrayOutputStream()

		val gotVersion = exec {
			commandLine("git", "rev-parse", "--short", "HEAD")
			workingDir(".")
			standardOutput = versionStream
		}.exitValue == 0

		val gotTag = exec {
			commandLine("git", "tag", "--points-at", "HEAD")
			workingDir(".")
			standardOutput = tagStream
		}.exitValue == 0

		if (!gotVersion || !gotTag) {
			return null
		}

		val version = versionStream.array.copyOfRange(0, versionStream.length).toString(Charsets.UTF_8).trim()
		val tag = tagStream.array.copyOfRange(0, tagStream.length).toString(Charsets.UTF_8).trim()

		return GitInfo(version, tag, System.getenv("BUILD_NUMBER") ?: "")
	} catch(err: Throwable) {
		println("Error getting git version")
		println(err)
	}

	return null
}

java.toolchain.languageVersion.set(JavaLanguageVersion.of(21))
println("Targeting Java ${java.toolchain.languageVersion.get()}")

tasks.withType(KotlinCompile::class.java) {
	compilerOptions {
		freeCompilerArgs = listOf("-Xjvm-default=all")
		jvmTarget.set(JvmTarget.JVM_21)
	}
}

tasks.withType(JavaCompile::class.java) {
	options.compilerArgs.add("-Xlint:all")
}

sourceSets {
	create("data") {
		compileClasspath += sourceSets["main"].output
		compileClasspath += sourceSets["main"].compileClasspath
		runtimeClasspath += sourceSets["main"].output
		runtimeClasspath += sourceSets["main"].runtimeClasspath
	}

	this["main"].resources {
		srcDir("src/data/resources")
		exclude("**/.cache/**")
	}
}

tasks.test {
	useJUnitPlatform()
	maxHeapSize = "4G"
}

configurations {
	create("embeddedLibs")
}

jarJar.enable()

dependencies {
	val jupiter_version: String by project
	val kotlin_for_forge_version: String by project
	val mixin_version: String by project
	val kommons_version: String by project

	implementation("net.neoforged:neoforge:$forge_version")
	testImplementation("org.junit.jupiter:junit-jupiter:${jupiter_version}")

	implementation("thedarkcolour:kotlinforforge-neoforge:$kotlin_for_forge_version")

	jarJar(implementation("com.github.ben-manes.caffeine:caffeine:[$caffeine_cache_version,)")!!)

	jarJar(implementation("ru.dbotthepony.kommons:kommons:[$kommons_version,)") { setTransitive(false) })
	jarJar(implementation("ru.dbotthepony.kommons:kommons-gson:[$kommons_version,)") { setTransitive(false) })
	jarJar(implementation("ru.dbotthepony.kommons:kommons-guava:[$kommons_version,)") { setTransitive(false) })

	compileOnly("yalter.mousetweaks:MouseTweaks:2.23:api")
	annotationProcessor("org.spongepowered:mixin:${mixin_version}:processor")

	if (handleDeps) {
		val jei_version: String by project
		val cosmetic_armor_reworked_id: String by project
		val jade_id: String by project
		val configured_id: String by project
		val curios_version: String by project
		val jei_mc_version: String by project
		val curios_mc_version: String by project
		val resourceful_lib_id: String by project
		val resourceful_config_id: String by project
		val botarium_id: String by project
		val ad_astra_id: String by project
		val worldedit_id: String by project
		val item_borders_id: String by project
		val iceberg_id: String by project
		val prism_lib_id: String by project
		val cloth_config_version: String by project
		val condensed_creative_version: String by project
		val mekanism_version: String by project

		compileOnly("top.theillusivec4.curios:curios-neoforge:${curios_version}+${curios_mc_version}")
		implementation("curse.maven:cosmetic-armor-reworked-237307:$cosmetic_armor_reworked_id")

		compileOnly("mezz.jei:jei-${jei_mc_version}-common-api:${jei_version}")
		compileOnly("mezz.jei:jei-${jei_mc_version}-neoforge-api:${jei_version}")
		runtimeOnly("mezz.jei:jei-${jei_mc_version}-neoforge:${jei_version}")

		// runtimeOnly("ru.dbotthepony:particle-collider:0.4.5")

		implementation("curse.maven:jade-324717:${jade_id}")
		//runtimeOnly("curse.maven:configured-457570:${configured_id}")

		compileOnly("curse.maven:resourceful-lib-570073:${resourceful_lib_id}")
		compileOnly("curse.maven:resourceful-config-714059:${resourceful_config_id}")
		compileOnly("curse.maven:botarium-704113:${botarium_id}")
		compileOnly("curse.maven:ad-astra-635042:${ad_astra_id}")
		runtimeOnly("curse.maven:worldedit-225608:${worldedit_id}")

		runtimeOnly("me.shedaniel.cloth:cloth-config-neoforge:${cloth_config_version}")
		implementation("io.wispforest:condensed_creative-neoforge:${condensed_creative_version}")

		compileOnly("curse.maven:item-borders-513769:${item_borders_id}")
		// implementation("curse.maven:item-borders-513769:${item_borders_id}")
		// runtimeOnly("curse.maven:iceberg-520110:${iceberg_id}")
		// runtimeOnly("curse.maven:prism-lib-638111:${prism_lib_id}")

		// runtimeOnly("curse.maven:worldedit-225608:${worldedit_fileid}")
		// runtimeOnly("at.ridgo8.moreoverlays:MoreOverlays-updated:${more_overlays_version}")

		// runtimeOnly("curse.maven:cyclops-core-232758:4392602")
		// runtimeOnly("curse.maven:integrated-dynamics-236307:4391535")
		// runtimeOnly("curse.maven:integrated-crafting-287357:4391487")
		// runtimeOnly("curse.maven:integrated-terminals-295910:4400924")
		// runtimeOnly("curse.maven:common-capabilities-247007:4391468")
		// runtimeOnly("curse.maven:integrated-tunnels-251389:4344632")

		implementation("mekanism:Mekanism:${mc_version}-${mekanism_version}")
	}
}

minecraft {
	accessTransformers {
		files("src/main/resources/META-INF/accesstransformer.cfg")
	}

	runs {
		configureEach {
			// "SCAN": For mods scan.
			// "REGISTRIES": For firing of registry events.
			// "REGISTRYDUMP": For getting the contents of all registries.
			systemProperty("forge.logging.markers", "REGISTRIES")

			// Log4j console level
			systemProperty("forge.logging.console.level", "debug")

			dependencies {
				runtime("com.github.ben-manes.caffeine:caffeine:[$caffeine_cache_version,)")
			}
		}

		getByName("client") {
			val usernameStream = FastByteArrayOutputStream()

			val gotUsername = exec {
				commandLine("git", "config", "--global", "--get", "user.name")
				workingDir(".")
				standardOutput = usernameStream
			}.exitValue == 0

			if (gotUsername) {
				val originalUsername = usernameStream.array.copyOfRange(0, usernameStream.length).toString(Charsets.UTF_8).trim()

				if (originalUsername.isNotEmpty()) {
					programArguments.addAll("--username", originalUsername)
				} else {
					programArguments.addAll("--username", "Dev_${System.getProperty("user.name")}")
				}
			} else {
				programArguments.addAll("--username", "Dev_${System.getProperty("user.name")}")
			}
		}

		getByName("server") {
			programArguments.addAll("nogui")
		}

		getByName("data") {
			programArguments.addAll("--mod", mod_id, "--all", "--output", file("src/data/resources/").absolutePath, "--existing", file("src/main/resources/").absolutePath)

			modSources(sourceSets["main"], sourceSets["data"])
		}
	}
}

mixin {
	config("$mod_id.mixins.json")
	// config("$mod_id.ad_astra.mixins.json")
}

repositories {
	// If you have mod jar dependencies in ./libs, you can declare them as a repository like so:
	flatDir {
		dir("libs")
	}

	maven {
		url = uri("https://maven.dbotthepony.ru")

		content {
			includeGroup("yalter.mousetweaks")
			includeGroup("lain.mods.cos")
			includeGroup("at.ridgo8.moreoverlays")
			includeGroup("ru.dbotthepony")
			includeGroup("curse.maven")
			includeGroup("ru.dbotthepony.kommons")
		}
	}

	maven(url = "https://maven.neoforged.net/releases") {
		name = "Neoforge"

		content {
			includeGroup("net.neoforged.gradle")
			includeGroup("net.neoforged")
		}
	}

	maven {
		name = "Kotlin for Forge"
		url = uri("https://thedarkcolour.github.io/KotlinForForge/")

		content {
			includeGroup("thedarkcolour")
			includeGroup("org.jetbrains.kotlin")
			includeGroup("org.jetbrains.kotlinx")
		}
	}

	maven {
		name = "Jared's Maven"
		url = uri("https://maven.blamejared.com/")

		content {
			includeGroup("mezz.jei")
		}
	}

	maven {
		url = uri("https://maven.octo-studios.com/releases")

		content {
			includeGroup("top.theillusivec4.curios")
		}
	}

	maven {
		url = uri("https://maven.shedaniel.me/")

		content {
			includeGroup("me.shedaniel.cloth")
		}
	}

	maven {
		url = uri("https://maven.wispforest.io")

		content {
			includeGroup("io.wispforest")
		}
	}

	maven {
		url = uri("https://modmaven.dev/")

		content {
			includeGroup("mekanism")
		}
	}

	// mavenCentral()
}

fun org.gradle.jvm.tasks.Jar.attachManifest() {
	manifest {
		attributes(mapOf(
			"Specification-Title"      to project.name,
			"Specification-Vendor"     to "DBotThePony",
			"Specification-Version"    to "1", // We are version 1 of ourselves
			"Implementation-Title"     to project.name,
			"Implementation-Version"   to gitVersion.modVersion,
			"Implementation-Vendor"    to "DBotThePony",
			"Implementation-Timestamp" to SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(Date())
		))
	}
}

tasks.jar.configure {
	attachManifest()
	archiveClassifier.set("slim")
	archiveVersion.set(gitVersion.jarName)
}

tasks.jarJar.configure {
	archiveClassifier.set("")
	archiveVersion.set(gitVersion.jarName)
}

tasks.assemble.configure {
	dependsOn(tasks.jarJar)
}

tasks.withType(ProcessResources::class.java) {
	val replaceProperties = mapOf(
			"mc_version"	to mc_version,
			"mod_version"	to gitVersion.modVersion
	)
	inputs.properties(replaceProperties)

	filesMatching(arrayListOf("META-INF/neoforge.mods.toml", "pack.mcmeta")) {
		expand(replaceProperties)
	}
}

tasks {
	create("sourceJar", org.gradle.jvm.tasks.Jar::class.java) {
		archiveClassifier.set("sources")
		from(sourceSets.main.get().allSource)
	}
}

if (project.hasProperty("mavenUser") && project.hasProperty("mavenPassword") && project.hasProperty("mavenUrl")) {
	val mavenUser: String by project
	val mavenPassword: String by project
	val mavenUrl: String by project

	publishing {
		publications {
			create<MavenPublication>("mavenJava") {
				// from(components["java"])
				artifact(tasks["jar"])
				artifact(tasks["sourceJar"])

				version = gitVersion.publishVersion

				pom {
					scm {
						url.set("https://gitlab.com/DBotThePony/overdrive-that-matters.git")
					}

					issueManagement {
						system.set("gitlab")
						url.set("https://gitlab.com/DBotThePony/overdrive-that-matters/issues")
					}
				}
			}
		}

		repositories {
			maven {
				url = uri(mavenUrl)

				credentials {
					username = mavenUser
					password = mavenPassword
				}
			}
		}
	}
}

idea {
	module {
		isDownloadSources = true
		isDownloadJavadoc = true
	}
}
