import org.gradle.internal.os.OperatingSystem

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id 'java-library'
    id("com.gradleup.shadow") version "8.3.5"
}

repositories {
    mavenCentral()
    maven {
        url = uri('https://openjfx.io/repo')
    }

    maven {
        url = uri('https://repo.maven.apache.org/maven2/')
    }
}

dependencies {

    api libs.org.apache.commons.commons.lang3
    api libs.commons.io.commons.io
    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.24.3'
    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.24.3'
    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl
    implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.24.3'
    api libs.org.jsoup.jsoup
    // https://mvnrepository.com/artifact/com.microsoft.playwright/playwright
    implementation group: 'com.microsoft.playwright', name: 'playwright', version: '1.49.0'
    // https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc
    implementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.49.1.0'
    // https://mvnrepository.com/artifact/org.jdbi/jdbi3-core
    implementation group: 'org.jdbi', name: 'jdbi3-core', version: '3.48.0'
    // https://mvnrepository.com/artifact/org.jdbi/jdbi3-testing
    testImplementation group: 'org.jdbi', name: 'jdbi3-testing', version: '3.48.0'
    // https://mvnrepository.com/artifact/org.jdbi/jdbi3-sqlite
    implementation group: 'org.jdbi', name: 'jdbi3-sqlite', version: '3.48.0'
    // https://mvnrepository.com/artifact/info.picocli/picocli
    implementation group: 'info.picocli', name: 'picocli', version: '4.7.6'
    api libs.org.mockito.mockito.core
    api libs.org.openjfx.javafx.controls
    api libs.org.openjfx.javafx.fxml
    api libs.org.openjfx.javafx.graphics
    api libs.org.openjfx.javafx.web
    api libs.org.kordamp.ikonli.ikonli.javafx
    api libs.org.kordamp.ikonli.ikonli.carbonicons.pack
    api libs.io.github.mkpaz.atlantafx.styles
    api libs.io.github.mkpaz.atlantafx.base
    api libs.org.openjfx.javafx.media
    api libs.net.java.dev.jna.jna
    api libs.net.java.dev.jna.jna.platform
    api libs.jakarta.xml.bind.jakarta.xml.bind.api
    api libs.com.sun.xml.bind.jaxb.impl
    api libs.com.sun.xml.bind.jaxb.core
    api libs.org.jetbrains.annotations
    api libs.org.jetbrains.kotlin.kotlin.stdlib.jdk8
    api libs.org.apache.maven.plugins.maven.jlink.plugin
    api libs.org.apache.commons.commons.compress
    api libs.org.iq80.snappy.snappy
    testImplementation libs.org.junit.jupiter.junit.jupiter
    testImplementation libs.org.testfx.testfx.core
    testImplementation libs.org.jetbrains.kotlin.kotlin.test
    compileOnly libs.org.projectlombok.lombok
    annotationProcessor libs.org.projectlombok.lombok
}

group = 'com.gearshiftgaming.se_mod_manager'
version = '0.8.0.12'
description = 'Space-Engineers-Mod-Manager'
java.sourceCompatibility = JavaVersion.VERSION_21

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8'
}

tasks.withType(Javadoc).configureEach {
    options.encoding = 'UTF-8'
}

jar {
    duplicatesStrategy = 'EXCLUDE'
}

shadowJar {
    archiveBaseName = 'Space-Engineers-Mod-Manager'
    archiveVersion.set(version.toString())
    archiveClassifier.set("")
    manifest {
        attributes 'Main-Class': 'com.gearshiftgaming.se_mod_manager.SpaceEngineersModManager'
    }
    // You can also add exclusions or customizations here
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

tasks.register('removeForeignBrowserBinaries') {
    def jarFile = file("build/libs/Space-Engineers-Mod-Manager-${version}.jar")
    def tempDir = file("build/tempJar")

    copy {
        from zipTree(jarFile)
        into tempDir
    }

    if (OperatingSystem.current().isWindows()) {
        delete "$tempDir/driver/mac/"
        delete "$tempDir/driver/mac-arm64/"
        delete "$tempDir/driver/linux/"
        delete "$tempDir/driver/linux-arm64/"
    } else {
        delete "$tempDir/driver/mac/"
        delete "$tempDir/driver/mac-arm64/"
        delete "$tempDir/driver/win32_x64/"
    }
}

tasks.register('removeForeignOsBrowsers', Jar) {
    dependsOn removeForeignBrowserBinaries
    manifest {
        attributes 'Main-Class': 'com.gearshiftgaming.se_mod_manager.SpaceEngineersModManager'
    }
    destinationDirectory.set(layout.buildDirectory.dir("libs"))
    from("build/tempJar")
}

def generatedVersionDir = "./src/main/resources"

sourceSets {
    main {
        output.dir(generatedVersionDir, builtBy: 'generateVersionProperties')
    }
}


tasks.register('generateVersionProperties') {
    doLast {
        def propertiesFile = file "$generatedVersionDir/version.properties"
        propertiesFile.parentFile.mkdirs()
        def properties = new Properties()
        properties.setProperty("version", rootProject.version.toString())
        propertiesFile.withWriter { properties.store(it, null) }
    }
}
processResources.dependsOn generateVersionProperties
compileJava.dependsOn generateVersionProperties
shadowJar.dependsOn generateVersionProperties
test.dependsOn generateVersionProperties

tasks.register('printVersionInformation') {
    println(version)
}

tasks.withType(Test).configureEach {
    useJUnitPlatform()
}

tasks.register('remoteTest', Test) {
    description = "Runs all tests except for ones that won't work on GH actions."
    exclude("**/ModInfoServiceTest.class")
}

remoteTest.dependsOn generateVersionProperties