import net.neoforged.moddevgradle.internal.RunGameTask

plugins {
	id 'java-library'
	id 'eclipse'
	id 'idea'
	id 'maven-publish'
	// Version List: https://projects.neoforged.net/neoforged/ModDevGradle
	id 'net.neoforged.moddev' version "${mdg_version}"
	id "com.dorongold.task-tree" version "4.0.0"
}

def secrets = new Properties()
def secretsFile = file('secrets.properties')
if (secretsFile.exists()) {
	secretsFile.withInputStream {
		stream -> secrets.load(stream)
	}

	fileTree("secrets").matching {
		include "**/*.properties"
	}.each {
		File file ->
			file.withInputStream {
				stream -> secrets.load(stream)
			}
	}
}

idea {
	module {
		downloadJavadoc = true
		downloadSources = true
	}
}

version = (hasProperty("CIRevision") ? CIRevision : project.mod_version)
group = group_name

java {
	toolchain {
		languageVersion.set(JavaLanguageVersion.of(21))
	}
}

tasks.withType(JavaCompile).configureEach {
	options.compilerArgs << "-Xlint:all,-classfile,-processing,-deprecation,-this-escape" << "-Werror"
	options.encoding = 'UTF-8'
}

base {
	archivesName = "${project.mod_id}-${project.minecraft_version}"
}

subprojects {
	version = project(":").version
}

neoForge {
	evaluationDependsOnChildren() // we need the subprojects to evaluate first so that the sourceSets are properly constructed

	version = project.neo_version

	validateAccessTransformers = true

	mods {
		twilightforest {
			sourceSet this.sourceSets.main
		}

		'tf-asm' {
			sourceSet project(":tf-asm").sourceSets.main
		}
	}

	unitTest {
		enable()
		testedMod = mods.twilightforest
	}

	runs {
		configureEach {
			gameDirectory = project.file('run')
			systemProperty 'forge.logging.console.level', 'debug'
			logLevel = org.slf4j.event.Level.DEBUG
		}

		client {
			client()
			systemProperty 'forge.enabledGameTestNamespaces', mod_id
			programArguments.addAll '--username', secrets.getProperty("username") ?: 'Dev', secrets.getProperty("uuid") ? '--uuid' : '', secrets.getProperty("uuid") ?: ''
		}

		server {
			server()
			systemProperty 'forge.enabledGameTestNamespaces', mod_id
			programArgument '--nogui'
		}

		data {
			data()
			gameDirectory = project.file('run-data')
			programArguments.addAll '--mod', mod_id, '--all', '--output', file('src/generated/resources/').getAbsolutePath(), '--existing', file('src/main/resources/').getAbsolutePath()
		}
	}
}

afterEvaluate { // DO NOT ASK... it fixes the runClient and family tasks :harold:
	tasks.withType(RunGameTask).configureEach {
		classpathProvider.setFrom(classpathProvider.files.stream().filter(f -> !f.toString().contains('tf-asm')).toList())
	}
}

jar {
	exclude 'data/twilightforest/function/**'
	if (hasProperty("CIRevision"))
		exclude 'data/twilightforest/structure/lich_tower'
}

sourceSets.main.resources { srcDir 'src/generated/resources' }

repositories {
	mavenLocal()
	maven {
		name 'Jared\'s Maven' // JEI
		url 'https://maven.blamejared.com/'
	}
	maven {
		name = "OctoStudios" //Curios
		url = uri("https://maven.octo-studios.com/releases")
	}
	maven {
		name 'Shedaniel\'s Maven' //REI
		url "https://maven.shedaniel.me"
	}
	maven {
		name = "TerraformersMC" //EMI
		url = "https://maven.terraformersmc.com/"
	}
	maven { // TOP
		url "https://maven.k-4u.nl"
	}
	maven { //CS
		url "https://nexus.resourcefulbees.com/repository/maven-public/"
	}
    maven {
        name 'Curseforge Maven' // Jade
        url "https://www.cursemaven.com"
        content {
            includeGroup "curse.maven"
        }
    }
}

dependencies {
	// Purely so we can see MDG sources for debugging purposes
	compileOnly "net.neoforged.moddev:net.neoforged.moddev.gradle.plugin:${project.mdg_version}"

	jarJar implementation(project(":tf-asm"))

	//make sure to only pick one of these when testing (switch others to compileOnly)
//	implementation "mezz.jei:jei-${project.base_minecraft_version}-neoforge:${project.jei_version}"
//	compileOnly "me.shedaniel:RoughlyEnoughItems-neoforge:${project.rei_version}"
//	compileOnly "dev.emi:emi-neoforge:${project.emi_version}+${project.base_minecraft_version}"

	// theillusivec4
//	implementation "top.theillusivec4.curios:curios-neoforge:${project.curios_version}+${project.base_minecraft_version}"

	//curse maven
//	implementation "curse.maven:jade-324717:5529595"
//	implementation "curse.maven:cosmetic-armor-reworked-237307:5427303"
//	implementation "curse.maven:world-edit-225608:5613180"
//	runtimeOnly "curse.maven:jeed-532286:5483315"
//	runtimeOnly "curse.maven:museum-curator-859070:4629894"

//	compileOnly "mcjty.theoneprobe:theoneprobe:${project.top_version}"

    //mods we dont have compat with but are nice to have
//	runtimeOnly "com.telepathicgrunt:CommandStructures-Neoforge:4.3.2+1.20.6"

	testImplementation group: 'net.neoforged', name: 'testframework', version: project.neo_version
	testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter', version: '5.10.3'
	testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.3'
	testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '5.12.0'
}

configurations.testImplementation {
	exclude module: 'tf-asm'
}

test {
	useJUnitPlatform()
	testLogging {
		events "failed"

		showExceptions true
		exceptionFormat "full"
		showCauses true
		showStackTraces true

		showStandardStreams = false
	}
}

jar {
	manifest {
		archiveClassifier = 'universal'
		attributes([
			'Specification-Title'     : mod_id,
			'Specification-Vendor'    : "TeamTwilight",
			'Specification-Version'   : "1",
			'Implementation-Title'    : this.project.name,
			'Implementation-Version'  : this.project.jar.archiveVersion,
			'Implementation-Vendor'   : "TeamTwilight",
			'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
		])
	}
}

publishing {
	publications {
		create('mavenJava', MavenPublication) {
			afterEvaluate {
				artifact this.project.jar
				artifact this.project.sourceJar
			}
			groupId 'team-twilight'
			artifactId this.project.mod_id
		}
	}
	repositories {
		maven {
			//url "file:///${project.projectDir}/mcmodsrepo"
			url "https://maven.tamaized.com/releases"
			credentials {
				username System.getenv('ARTIFACTORY_USER')
				password System.getenv('ARTIFACTORY_PASS')
			}
		}
	}
}

tasks.register('sourceJar', Jar) {
	dependsOn 'classes'
	from sourceSets.main.allSource
	archiveClassifier = 'sources'
}
