import java.text.SimpleDateFormat

plugins {
    id "architectury-plugin" version "3.4-SNAPSHOT" // Uses Architectury plugin https://github.com/architectury/architectury-plugin
    id "dev.architectury.loom" version "1.10-SNAPSHOT" apply false // Uses Architectury Loom
    // (fork of Fabric Loom) https://github.com/architectury/architectury-loom - does not apply to root project
    id "net.kyori.indra" version "3.1.3" // Uses Indra plugin
    id "net.kyori.indra.git" version "3.1.3" // Uses Indra Git plugin
    id "net.kyori.indra.checkstyle" version "3.1.3" // Uses Indra Checkstyle plugin
    id "me.shedaniel.unified-publishing" version "0.1.+" apply false // Uses Unified Publishing plugin
}

architectury {
    minecraft = rootProject.minecraft_version // Sets Minecraft version from gradle.properties
}

subprojects {
    apply plugin: "dev.architectury.loom" // Applies Architectury Loom to subprojects
    apply plugin: "me.shedaniel.unified-publishing" // Applies Unified Publishing to subprojects

    loom {
        silentMojangMappingsLicense() // Silences the annoying as hell Mojang License text
    }

    dependencies {
        minecraft "com.mojang:minecraft:${rootProject.minecraft_version}" // Minecraft dependency
        // mappings loom.officialMojangMappings() // Maps Minecraft so we can tell what classes are what - official mappings
        mappings loom.layered() {
            officialMojangMappings()
            // TODO: update this when ParchmentMC for 1.21.8 is out
            // parchment("org.parchmentmc.data:parchment-${rootProject.parchmentmc_minecraft_version}:${rootProject.parchmentmc_version}")
        }
    }

    ext {
        releaseChangelog = {
            def dateFormat = new SimpleDateFormat("yyyy-MM-dd")
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
            def date = dateFormat.format(new Date())
            def changelogFormatText = file("../docs/changelogs/format.md").text.trim()
            def changelogText = file("../docs/changelogs/${rootProject.version}.md").text.trim()
            def lastVersion = changelogText.split("\n")[0].split("=")[1].trim()
            def changelog = changelogFormatText
                    .replace("%date%", date)
                    .replace("%changelog%", changelogText)
                    .replace("%version%", rootProject.version.toString())
                    .replace("%mc_versions%", rootProject.supported_minecraft_versions)
                    .replace("%last_version%", lastVersion)
            StringBuilder changelogBuilder = new StringBuilder()
            for (String line : changelog.split("\n")) {
                if (!line.startsWith("!"))
                    changelogBuilder.append(line).append("\n")
            }
            if (!rootProject.buildDir.exists()) rootProject.buildDir.mkdirs()
            new File(rootProject.buildDir, "CHANGELOG.md").text = changelogBuilder.toString().trim()
            return changelogBuilder.toString().trim()
        }
    }
}

allprojects {
    apply plugin: "java"
    apply plugin: "architectury-plugin" // Applies architectury plugin to all projects
    apply plugin: "maven-publish"
    apply plugin: "net.kyori.indra" // Applies Indra to all projects
    apply plugin: "net.kyori.indra.git" // Applies Indra Git to all projects
    apply plugin: "net.kyori.indra.checkstyle" // Applies Indra Checkstyle to all projects

    archivesBaseName = rootProject.archives_base_name // sets the archive name from gradle.properties
    version = rootProject.mod_version // sets the version from gradle.properties
    group = rootProject.maven_group // sets the maven group from gradle.properties
    indra {
        mitLicense()
        checkstyle("9.3")
        javaVersions {
            target(21) // 1.20.5 now uses Java 21
            testWith(21)
        }
    }

    repositories {
      maven {
          name = 'ParchmentMC'
          url = 'https://maven.parchmentmc.org'
      }
    }

    java {
        withSourcesJar()
    }

    compileJava {
        options.compilerArgs +=	"-Xlint:-processing" // Fixes "no processor claimed any of these annotations" warning
    }

    tasks.withType(JavaCompile).tap {
        configureEach {
            options.release = 21
        }
    }

    tasks.named('sourcesJar', Jar) {
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }
}
