plugins {
	id 'java'
	id 'java-library'
	id 'maven-publish'
	id 'xyz.wagyourtail.unimined' version '1.1.2'
	id 'idea'
}

archivesBaseName  = 'phobot'
version = "$rootProject.mc_version-$rootProject.project_version"
group = 'me.earth.phobot'

sourceSets {
	fabric
	neoforge
	lexforge
}

repositories {
	mavenCentral()
	maven {
		url = "https://files.minecraftforge.net/maven"
	}
	maven {
		name = "sponge"
		url = "https://repo.spongepowered.org/maven"
	}
	maven {
		url = "https://maven.wagyourtail.xyz/releases"
	}

	/*maven {
        name 'local'
        url project.rootDir.toURI().toURL().toString() + '/libs'
    }*/

	maven {
		name 'PingBypassSuperDirMaven'
		url project.rootDir.parentFile.toURI().toURL().toString() + '/pingbypass/build/maven'
	}

	maven {
		name = '3arthMaven'
		url = 'https://3arthqu4ke.github.io/maven'
	}

	maven {
		name = "ParchmentmcMaven"
		url = "https://maven.parchmentmc.net/"
	}
}

unimined.minecraft {
	version project.mc_version

	mappings {
		//intermediary()
		mojmap()
		//parchment("1.20.4", "2024.02.25-nightly-SNAPSHOT")
		devFallbackNamespace "mojmap"
		//devFallbackNamespace "intermediary"
	}

	defaultRemapJar = false
}

unimined.minecraft(sourceSets.fabric) {
	combineWith(sourceSets.main)

	fabric {
		loader project.fabric_version
	}

	defaultRemapJar = true
}

unimined.minecraft(sourceSets.neoforge) {
	combineWith(sourceSets.main)

	neoForged {
		loader project.neoforge_version
		//mixinConfig "mixins.phobot.json"
	}

	minecraftRemapper.config {
		ignoreConflicts(true)
	}

	defaultRemapJar = true
}

unimined.minecraft(sourceSets.lexforge) {
	combineWith(sourceSets.main)

	minecraftForge {
		loader project.lexforge_version
		mixinConfig "mixins.phobot.json"
	}

	minecraftRemapper.config {
		ignoreConflicts(true)
	}

	defaultRemapJar = true
}

configurations {
	mainImplementation

	lwjglAgent
	runtimeOnly.extendsFrom lwjglAgent
	runClientFabric
	runClientLexForge
	runClientNeoForge
}

dependencies {
	compileOnly 'org.spongepowered:mixin:0.8.5-SNAPSHOT'
	compileOnly 'me.earth.headlessmc:headlessmc:1.8.1'
	lwjglAgent 'me.earth.headlessmc:headlessmc-lwjgl:1.8.1'

	api ("me.earth.pingbypass:pb-api:$rootProject.pb_version") {
		exclude module: 'gson'
	}

	runClientFabric "me.earth.pingbypass:pb-api:$rootProject.pb_version:fabric-dev"
	//runClientLexForge "me.earth.pingbypass:pb-api:$rootProject.pb_version:lexforge-dev"
	runClientNeoForge "me.earth.pingbypass:pb-api:$rootProject.pb_version:neoforge-dev"

	if (rootProject.property('pb.server').toBoolean()) {
		runClientFabric "me.earth.pingbypass:pb-server:$rootProject.pb_version:fabric-dev"
		runClientLexForge "me.earth.pingbypass:pb-server:$rootProject.pb_version:lexforge-dev"
		runClientNeoForge "me.earth.pingbypass:pb-server:$rootProject.pb_version:neoforge-dev"
	} else {
		runClientFabric "me.earth.pingbypass:pb-client:$rootProject.pb_version:fabric-dev"
		runClientLexForge "me.earth.pingbypass:pb-client:$rootProject.pb_version:lexforge-dev"
		runClientNeoForge "me.earth.pingbypass:pb-client:$rootProject.pb_version:neoforge-dev"
	}

	compileOnly 'org.jetbrains:annotations:24.0.1'
	compileOnly group: 'org.ow2.asm', name: 'asm-tree', version: '9.5'

	compileOnly 'org.projectlombok:lombok:1.18.26'
	annotationProcessor 'org.projectlombok:lombok:1.18.26'

	testCompileOnly 'org.projectlombok:lombok:1.18.26'
	testAnnotationProcessor 'org.projectlombok:lombok:1.18.26'

	testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
	testImplementation "me.earth.pingbypass:pb-api:$rootProject.pb_version:test-fixtures"
	testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
}

afterEvaluate {
	fabricRunClient {
		standardInput = System.in
		if (rootProject.property('hmc.lwjgl').toBoolean()) {
			jvmArgs += ["-javaagent:${configurations.lwjglAgent.files.iterator().next()}"]
			systemProperties['joml.nounsafe'] = 'true'
			systemProperties['fabric.systemLibraries'] = "${configurations.lwjglAgent.files.iterator().next()}"
		}
	}
}

processFabricResources {
	inputs.property "version", project.version

	filesMatching("fabric.mod.json") {
		expand "version": project.version
	}
}

processNeoforgeResources {
	inputs.property "version", project.version

	filesMatching("META-INF/mods.toml") {
		expand "version": project.version
	}
}

processLexforgeResources {
	inputs.property "version", project.version

	filesMatching("META-INF/mods.toml") {
		expand "version": project.version
	}
}

/* TODO: java {
    withSourcesJar()
    withJavadocJar()
}*/

tasks.withType(org.gradle.jvm.tasks.Jar).configureEach {
	from("LICENSE") {
		duplicatesStrategy = DuplicatesStrategy.INCLUDE
		rename { "${it}_${project.archivesBaseName}" }
	}

	manifest {
		attributes(
				'PingBypassPluginConfig': 'PhobotPluginConfig.json',
				'MixinConfigs': "mixins.phobot.json",
				'Implementation-Title': 'Phobot',
				'Implementation-Version': project.version,
		)
	}
}

tasks.withType(JavaCompile).configureEach {
	it.options.encoding = "UTF-8"
	if (JavaVersion.current().isJava9Compatible()) {
		it.options.release.set(17)
	}
}

afterEvaluate {
	publishing {
		publications {
			"${name.toLowerCase()}"(MavenPublication) {
				((MavenPublication) it).groupId "${group}"
				((MavenPublication) it).artifactId "${archivesBaseName.toLowerCase()}"
				((MavenPublication) it).version "${version}"
				from components.java
				for (String platform: ['Fabric', 'Lexforge', 'Neoforge']) {
					String platform_lower = platform.toLowerCase()
					artifact tasks.named("${platform_lower}Jar").get()
					artifact tasks.named("remap${platform}Jar").get()
				}
			}
		}

		repositories {
			if (System.getenv('DEPLOY_TO_GITHUB_PACKAGES_URL') == null) {
				maven {
					name = 'BuildDirMaven'
					url = rootProject.layout.buildDirectory.dir('maven')
				}
			} else {
				maven {
					name = 'GithubPagesMaven'
					url = System.getenv('DEPLOY_TO_GITHUB_PACKAGES_URL')
					credentials {
						username = System.getenv('GITHUB_USER')
						password = System.getenv('GITHUB_TOKEN')
					}
				}
			}
		}
	}

	publish {
		dependsOn(build)
	}
}

sourceSets {
	test {
		compileClasspath += main.compileClasspath + main.runtimeClasspath + main.output
		runtimeClasspath += main.compileClasspath + main.runtimeClasspath + main.output
	}
}

test {
	useJUnitPlatform()
}

// TODO: fabricRunClient gradle task works just fine. But the Intellij run task needs the pingbypass mod jar in the mods folder
//  same goes for neoforge. I suspect that Intellij does not understand our gradle configuration, and does not add these jars to the classpath.
//  makes sense since the Intellij run configuration does not have anything to do with these gradle tasks.
//  I tried tinkering around with the idea gradle plugin, but it just does not work?!
// Forge Runs seem to have problems running from the build/classes folder
// So instead we just run from the built jar
afterEvaluate {
	fabricRunClient {
		classpath += files(configurations.runClientFabric.files)
		systemProperties['pb.classpath.plugins'] = 'PhobotPluginConfig.json'
	}

	// Forge Runs seem to have problems running from the build/classes folder
	// So instead we just run from the built jar
	lexforgeRunClient {
		dependsOn(lexforgeJar)
		classpath += files(configurations.runClientLexForge.files)
		systemProperties['pb.classpath.plugins'] = 'PhobotPluginConfig.json'
		classpath = classpath.filter {
			!it.toString().contains('phobot/build/classes/java/'.replace('/', File.separator))
					&& !it.toString().contains('phobot/build/resources/'.replace('/', File.separator))
		}

		classpath += files("${projectDir}/build/libs/phobot-${version}-lexforge-dev.jar".replace('/', File.separator))
	}

	neoforgeRunClient {
		dependsOn(neoforgeJar)
		classpath += files(configurations.runClientNeoForge.files)
		systemProperties['pb.classpath.plugins'] = 'PhobotPluginConfig.json'
		classpath = classpath.filter {
			!it.toString().contains('phobot/build/classes/java/'.replace('/', File.separator))
					&& !it.toString().contains('phobot/build/resources/'.replace('/', File.separator))
		}

		classpath += files("${projectDir}/build/libs/phobot-${version}-neoforge-dev.jar".replace('/', File.separator))
	}
}
