import java.text.SimpleDateFormat
import java.time.LocalDate

plugins {
    id 'fabric-loom' version '1.10-SNAPSHOT'
    id "com.github.johnrengelman.shadow" version "8.1.1"
    id 'com.github.gmazzo.buildconfig' version '5.5.1'
}

//A switch that can be used to enable/disable Baritone integration in the jar file.
project.ext.withBaritone = true
project.ext.devVersion = false
// The version of BThack to be used both in the file name and within BThack
project.ext.releaseVersion = "2.0"
project.ext.version = project.ext.devVersion ? getDevName() : project.ext.releaseVersion

version = project.ext.version + (project.ext.withBaritone ? "" : "-NoBaritone")
group = "com.ferra13671"

String getDevName() {
    LocalDate localDate = LocalDate.now()
    return String.format("dev-%s.%S.%S.%S", localDate.getYear(), localDate.getMonth().getValue(), localDate.getDayOfMonth(), new SimpleDateFormat("k.mm").format(new Date()))
}

void includeBaritone() {
    if (project.ext.withBaritone) {
        dependencies {
            modInclude "meteordevelopment:baritone:${project.baritone_version}-SNAPSHOT"
            library "dev.babbaj:nether-pathfinder:${nether_pathfinder_version}"
        }
    } else {
        dependencies {
            //It is only needed so that gradle does not generate an error
            // because of imports of non-existent classes
            modCompileOnly "meteordevelopment:baritone:${project.baritone_version}-SNAPSHOT"
        }
    }
}

base {
    archivesName = "BThack-" + project.minecraft_version + "-fabric"
}

repositories {

    maven {
        name = "meteor-maven"
        url = "https://maven.meteordev.org/releases"
    }
    maven {
        name = "meteor-maven-snapshots"
        url = "https://maven.meteordev.org/snapshots"
    }
    maven {
        name = "modrinth"
        url = "https://api.modrinth.com/maven"

        content {
            includeGroup "maven.modrinth"
        }
    }
    maven {
        name = 'babbaj-repo'
        url = 'https://babbaj.github.io/maven/'
    }
    maven {
        name = 'ferra13671-maven'
        url = 'https://ferra13671.github.io/maven/'
    }
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }

    mavenCentral()
}

configurations {
    // include mods
    modImplementation.extendsFrom(modInclude)
    include.extendsFrom(modInclude)

    // include libraries
    implementation.extendsFrom(library)
    shadow.extendsFrom(library)
}

dependencies {
    // To change the versions see the gradle.properties file
    minecraft "com.mojang:minecraft:${project.minecraft_version}"
    mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
    modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

    library "com.ferra13671:mega-events:1.3"
    library "com.ferra13671:simple-language-system:1.1"
    library "com.ferra13671:gl-texture-utils:1.4"
    library "com.ferra13671:java-discord-rpc:1.0"

    includeBaritone()
}

loom {
    accessWidenerPath = file("src/main/resources/bthack.accesswidener")
}

shadowJar {
    configurations = [project.configurations.shadow]
}

processResources {
    inputs.property "version", project.version
    inputs.property "minecraft_version", project.minecraft_version
    inputs.property "loader_version", project.loader_version
    filteringCharset "UTF-8"

    filesMatching("fabric.mod.json") {
        expand "version": project.version,
                "minecraft_version": project.minecraft_version,
                "loader_version": project.loader_version,
                "bthack_version": project.ext.version
    }
}

def targetJavaVersion = 21
tasks.withType(JavaCompile).configureEach {
    it.options.encoding = "UTF-8"
    if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
        it.options.release.set(targetJavaVersion)
    }
}

java {
    def javaVersion = JavaVersion.toVersion(targetJavaVersion)
    if (JavaVersion.current() < javaVersion) {
        toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
    }

    withSourcesJar()
}

remapJar {
    dependsOn shadowJar
    inputFile.set(shadowJar.archiveFile)
}

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


buildConfig {
    className("BThackData")
    packageName(group)

    buildConfigField(Boolean, "WITH_BARITONE", project.ext.withBaritone)
    buildConfigField(String, "MC_VERSION", project.minecraft_version)
    buildConfigField(String, "VERSION", project.minecraft_version + "-" + project.ext.version)
    buildConfigField(String, "RELEASE_VERSION", project.minecraft_version + "-" + project.ext.releaseVersion)
}
