import fr.altarik.ReportDiscord
import fr.altarik.CreateTag


plugins {
	id "com.modrinth.minotaur" version "2.+"
	id 'fabric-loom' version '1.6-SNAPSHOT'
	id 'maven-publish'
}


Properties local = new Properties()
try {
	local.load(new FileInputStream(rootProject.file("local.properties")))
} catch (IOException ignored) {}

version = project.mod_version
group = project.maven_group


String webhookId = getEnv("DISCORD_PUB_ID", local.getProperty("discord_pub_id"))
String webhookToken = getEnv("DISCORD_PUB_TOKEN", local.getProperty("discord_pub_token"))
String repoUrl = "https://repo.altarik.fr/#/" + (project.version.endsWith('SNAPSHOT') ? 'snapshots/' : 'releases/') + project.group.replace(".", "/") + "/" + project.rootProject.name + "/" + project.version

var reportConfig = new ReportDiscord.ReportData("https://discord.com/api/", webhookId, webhookToken, project.rootProject.name, "Update " + project.version + " has been published", repoUrl)

String giteaToken = getEnv("GITEA_TOKEN", local.getProperty("gitea_token"))

var releaseConfig = new CreateTag.CreateReleaseData("https://git.altarik.fr", project.git_owner, project.git_repo, "v" + project.version as String, giteaToken)


base {
	archivesName = project.archives_base_name
}

sourceSets {
	main {
		resources {
			srcDirs += {
				'src/generated'
			}
		}
	}
}

loom {
	runs {
		// This adds a new gradle task that runs the datagen API: "gradlew runDatagen"
		datagen {
			inherit server
			name "Data Generation"
			vmArg "-Dfabric-api.datagen"
			vmArg "-Dfabric-api.datagen.output-dir=${file("src/generated")}"
			vmArg "-Dfabric-api.datagen.modid=${mod_id}"

			runDir "build/datagen"
		}
	}
}

repositories {
	maven {
		name 'altarik-snapshots'
		url 'https://repo.altarik.fr/snapshots/'
	}
	maven {
		name 'altarik-releases'
		url 'https://repo.altarik.fr/releases/'
	}
	maven {
		url "https://maven.teamresourceful.com/repository/maven-public/"
	}
	maven {
		url "https://maven.resourcefulbees.com/repository/maven-public/"
	}
	exclusiveContent {
		forRepository {
			maven {
				name = "Modrinth"
				url = "https://api.modrinth.com/maven"
			}
		}
		filter {
			includeGroup "maven.modrinth"
		}
	}
    maven {
        url = "https://jitpack.io"
    }
	mavenCentral()

	// Add repositories to retrieve artifacts from in here.
	// You should only use this when depending on other mods because
	// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
	// See https://docs.gradle.org/current/userguide/declaring_repositories.html
	// for more information about repositories.
}

dependencies {
	// To change the versions see the gradle.properties file
	include(implementation(annotationProcessor("io.github.llamalad7:mixinextras-fabric:0.2.2")))
	minecraft "com.mojang:minecraft:${project.minecraft_version}"
	mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
	modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

	// Indium and sodium for sodium support
	modCompileOnly "maven.modrinth:indium:${project.indium_version}+mc${project.minecraft_version}"
	modCompileOnly "maven.modrinth:sodium:mc${project.minecraft_version}-${project.sodium_version}"
//	modRuntimeOnly "maven.modrinth:indium:${project.indium_version}+mc${project.minecraft_version}"
//	modRuntimeOnly "maven.modrinth:sodium:mc${project.minecraft_version}-${project.sodium_version}"

	// Athena for connected textures
	modCompileOnly "earth.terrarium.athena:athena-fabric-${project.minecraft_version}:${project.athena_version}"
	modRuntimeOnly "earth.terrarium.athena:athena-fabric-${project.minecraft_version}:${project.athena_version}"

	// Continuity for connected textures
	modCompileOnly "maven.modrinth:continuity:${project.continuity_version}"
	modRuntimeOnly "maven.modrinth:continuity:${project.continuity_version}"

	// Chipped to test athena implementation
	modRuntimeOnly "com.teamresourceful.resourcefullib:resourcefullib-fabric-${project.minecraft_version}:2.4.7"
	modRuntimeOnly "earth.terrarium.chipped:Chipped-fabric-${project.minecraft_version}:3.1.2"

	// Axiom for blueprint support
	modCompileOnly "maven.modrinth:N6n5dqoA:YxeYxQyz"
    modCompileOnly "com.github.moulberry:AxiomClientAPI:1.0.5.3"

	// Fabric API.
	modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
}

processResources {
	inputs.property "version", project.version
	inputs.property "minecraft_version", project.minecraft_version
	inputs.property "loader_version", project.loader_version
	inputs.property "mod_id", project.mod_id
	inputs.property "athena_version", project.athena_version
	inputs.property "indium_version", project.indium_version
	inputs.property "sodium_version", project.sodium_version
	inputs.property "continuity_version", project.continuity_version
	filteringCharset "UTF-8"

	filesMatching("fabric.mod.json") {
		expand inputs.properties
	}
}

def targetJavaVersion = 17
tasks.withType(JavaCompile).configureEach {
	// ensure that the encoding is set to UTF-8, no matter what the system default is
	// this fixes some edge cases with special characters not displaying correctly
	// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
	// If Javadoc is generated, this must be specified in that task too.
	it.options.encoding = "UTF-8"
	if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
		it.options.release.set(targetJavaVersion)
	}
}

java {
	sourceCompatibility = JavaVersion.VERSION_17
	targetCompatibility = JavaVersion.VERSION_17

	def javaVersion = JavaVersion.toVersion(targetJavaVersion)
	if (JavaVersion.current() < javaVersion) {
		toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
	}
	// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
	// if it is present.
	// If you remove this line, sources will not be generated.
	withSourcesJar()
}

jar {
	from("LICENSE") {
		rename { "${it}_${project.archivesBaseName}"}
	}
}

// configure the maven publication
publishing {
	publications {
		mavenJava(MavenPublication) {
			from components.java
		}
	}

	// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
	repositories {
		// Add repositories to publish to here.
		// Notice: This block does NOT have the same function as the block in the top level.
		// The repositories here will be used for publishing your artifact, not for
		// retrieving dependencies.
		maven {
			name 'altarik'
			url 'https://repo.altarik.fr/'.concat(project.version.endsWith('SNAPSHOT') ? 'snapshots/' : 'releases/')
			credentials {
				username = getEnv("REPO_USERNAME", local.getProperty("repo_username"))
				password = getEnv("REPO_PASSWORD", local.getProperty("repo_password"))
			}
		}

	}
}

// configure modrinth publication
modrinth {
	token = getEnv("MODRINTH_TOKEN", local.getProperty("modrinth_token"))
	projectId = project.modrinth_id
	versionNumber = "${project.mod_version}-${project.minecraft_version}"
	versionName = "${project.archives_base_name} ${project.mod_version}-${project.minecraft_version}"
	versionType = project.mod_version.endsWith('SNAPSHOT') ? 'beta' : 'release'
	uploadFile = remapJar
	gameVersions = [project.minecraft_version]
	loaders = ["fabric"]
	dependencies {
		required.project "fabric-api"
		optional.version "b1ZV3DIJ", "${project.athena_version}"
		optional.version "Orvt0mRa", "${project.indium_version}+mc${project.minecraft_version}"
		optional.version "1IjD5062", "${project.continuity_version}"
	}
}

tasks.register("reportToDiscord", ReportDiscord) {
	config.set(reportConfig)
}

tasks.register("createTag", CreateTag) {
	config.set(releaseConfig)
}

static def getEnv(String envName, String defaultValue) {
	String r = System.getenv(envName)
	if(r != null) {
		return r
	} else {
		return defaultValue
	}
}

