plugins {
    id 'org.beryx.runtime' version '1.12.7'
}

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceCompatibility = targetCompatibility = appJavaCompatibility

ext.appMainClass = "com.shatteredpixel.shatteredpixeldungeon.desktop.DesktopLauncher"
processResources {
    from new File(project(':core').projectDir, "/src/main/assets")
    from new File(project(':desktop').projectDir,"/src/main/assets")
}

def osName = System.getProperty('os.name').toLowerCase(Locale.ROOT)

task debug(type: JavaExec) {
    classpath = sourceSets.main.runtimeClasspath
    ignoreExitValue = true

    main = appMainClass
    systemProperty 'Specification-Title', appName
    systemProperty 'Specification-Version', appVersionName + "-INDEV"
    systemProperty 'Implementation-Version', appVersionCode

    if (osName.contains('mac')) {
        jvmArgs '-XstartOnFirstThread'
    }
}

task release(type: Jar) {
    //FIXME this is now needed as of gradle 7.0, due to our weird sourceSets setup. Should see if there's a better way to do this
    setDuplicatesStrategy(DuplicatesStrategy.EXCLUDE)
    from sourceSets.main.output
    dependsOn configurations.runtimeClasspath
    from { configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) } }

    manifest {
        attributes 'Main-Class': appMainClass
        attributes 'Specification-Title': appName
        attributes 'Specification-Version': appVersionName
        attributes 'Implementation-Version': appVersionCode
    }
}
installDist.dependsOn release
startScripts.dependsOn release
jpackageImage.dependsOn release

runtime {
    modules = ['java.base',
               'java.desktop',
               'jdk.unsupported',
               'jdk.crypto.cryptoki',
               'jdk.management']
    options = ['--strip-debug',
               '--compress', '2',
               '--no-header-files',
               '--no-man-pages',
               '--strip-native-commands',
               '--vm', 'server']

    jpackage {
        mainClass = appMainClass
        appVersion = (appVersionName =~ /\d+\.\d+\.\d+/)[0]
        imageName = appName
    }

    //TODO everything works but there is some jank and default icons, esp. for installers and linux
    // look into --resource-dir flag for further improvements
    if (osName.contains('windows')) {
        targetPlatform("win") {
            jdkHome = jdkDownload("https://github.com/AdoptOpenJDK/openjdk16-binaries/releases/download/jdk-16.0.1%2B9/OpenJDK16U-jdk_x64_windows_hotspot_16.0.1_9.zip")
            javaHome = file("./build/jdks/win/jdk-16.0.1+9").getAbsolutePath()
            jpackage {
                jpackageHome = file("./build/jdks/win/jdk-16.0.1+9")
                imageOptions = ["--icon", file("./src/main/assets/icons/windows.ico")]

                installerType = "msi"
                installerName = appName
                installerOptions = ["--win-dir-chooser", "--win-menu"]
            }
        }
    } else if (osName.contains('linux')) {
        targetPlatform("linux") {
            jdkHome = jdkDownload("https://github.com/AdoptOpenJDK/openjdk16-binaries/releases/download/jdk-16.0.1%2B9/OpenJDK16U-jdk_x64_linux_hotspot_16.0.1_9.tar.gz")
            javaHome = file("./build/jdks/linux/jdk-16.0.1+9").getAbsolutePath()
            jpackage {
                jpackageHome = file("./build/jdks/linux/jdk-16.0.1+9")
                imageOptions = ["--icon", file("./src/main/assets/icons/icon_256.png")]
                //assumes ubuntu linux, changes needed to generate .rpm files
                installerType = "deb"
                installerName = appName
                installerOptions = ["--linux-shortcut"]
            }
        }
    } else if (osName.contains('mac')) {
        targetPlatform("mac") {
            jdkHome = jdkDownload("https://github.com/AdoptOpenJDK/openjdk16-binaries/releases/download/jdk-16.0.1%2B9/OpenJDK16U-jdk_x64_mac_hotspot_16.0.1_9.tar.gz")
            javaHome = file("./build/jdks/mac/jdk-16.0.1+9/Contents/Home/").getAbsolutePath()
            jpackage {
                jpackageHome = file("./build/jdks/mac/jdk-16.0.1+9/Contents/Home/")
                imageOptions = ["--icon", file("./src/main/assets/icons/mac.icns"),
                                "--java-options", "-XstartOnFirstThread",
                                //append .apple because com.shatteredpixel.shatteredpixeldungeon was taken =(
                                "--mac-package-identifier", appPackageName + ".apple",
                                "--mac-package-name", "ShattererdPD"]

                installerType = "dmg"
                installerName = appName
            }
        }
    }

}

dependencies {
    implementation project(':core')

    implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
    implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
    implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    implementation "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
    implementation "com.badlogicgames.gdx-controllers:gdx-controllers-desktop:$gdxControllersVersion"

    //we use LWJGL tinyFD directly to display crash messages and (for now) single-line text input
    implementation "org.lwjgl:lwjgl-tinyfd:3.2.3"
    implementation "org.lwjgl:lwjgl-tinyfd:3.2.3:natives-windows"
    implementation "org.lwjgl:lwjgl-tinyfd:3.2.3:natives-macos"
    implementation "org.lwjgl:lwjgl-tinyfd:3.2.3:natives-linux"

    implementation project(':services:updates:githubUpdates')
    implementation project(':services:news:shatteredNews')
}