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

plugins {
    id 'fabric-loom' version '1.6-SNAPSHOT' apply false
}

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

group = project.maven_group
version = project.maven_version

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)


allprojects {
    apply plugin: 'maven-publish'
    apply plugin: 'fabric-loom'

    group = project.maven_group
    version = project.maven_version

    repositories {
        maven {
            name 'altarik-snapshots'
            url 'https://repo.altarik.fr/snapshots/'
        }
        maven {
            name 'altarik-releases'
            url 'https://repo.altarik.fr/releases/'
        }
        maven {
            name = 'Fabric'
            url = 'https://maven.fabricmc.net/'
        }
        mavenCentral()
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                 from components.java
            }
        }
        repositories {
            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"))
                }
            }
        }
    }

    processResources {
        inputs.property "version", project.version
        inputs.property "minecraftVersion", project.minecraft_version
        inputs.property "loaderVersion", project.loader_version

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

    tasks.withType(JavaCompile).configureEach {
        it.options.encoding = "UTF-8"
        it.options.release = 17
    }

    dependencies {
        testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junit_version}"
        testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junit_version}"
        minecraft "com.mojang:minecraft:${project.minecraft_version}"
        mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
        modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
        modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
    }

    java {
        withSourcesJar()
        withJavadocJar()

        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    test {
        useJUnitPlatform()
    }

}

dependencies {
    implementation project(path: ":Core", configuration: "namedElements")
    implementation project(path: ":Database", configuration: "namedElements")
    implementation project(path: ":Pagination", configuration: "namedElements")
    implementation project(path: ":Tasks", configuration: "namedElements")
    include subprojects.collect { project ->  project }
}

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

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

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


/*jar {
    dependsOn subprojects.jar 
    subprojects.each { project -> 
        from(project.jar) {
            into("META-INF/jars/")
        }
    }
}*/
