/*
 * STAROPENSOURCE ENGINE SOURCE FILE
 * Copyright (c) 2024 The StarOpenSource Engine Contributors
 * Licensed under the GNU Affero General Public License v3
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import org.mikeneck.graalvm.BuildTypeSelector

// Plugins
plugins {
    id("java")
    id("application")
    id("io.freefair.lombok") version("${pluginLombok}")
    id("io.github.goooler.shadow") version("${pluginShadow}")
    id('org.mikeneck.graalvm-native-image') version("${pluginNativeImage}")
}

// Dependencies
dependencies {
    // -> Runtime <-
    // Lombok
    compileOnly("org.projectlombok:lombok:${dependencyLombok}")
    annotationProcessor("org.projectlombok:lombok:${dependencyLombok}")

    // JetBrains Annotations
    compileOnly("org.jetbrains:annotations:${dependencyJetbrainsAnnotations}")

    // -> Project <-
    implementation(project(":base"))
    implementation(project(":ansi"))
    implementation(project(":slf4j-compat"))
    implementation(project(":graphics"))
    implementation(project(":graphics:vulkan"))
    implementation(project(":graphics:opengl"))
}

// Fix delombok task
delombok.doFirst {
    File target = file("${project.projectDir}/src/main/module-info.java")
    File source = file("${project.projectDir}/src/main/java/module-info.java")

    target.delete()
    source.renameTo(target)
}
delombok.doLast {
    File target = file("${project.projectDir}/src/main/java/module-info.java")
    File source = file("${project.projectDir}/src/main/module-info.java")

    target.delete()
    source.renameTo(target)
}

// Configure output jar
jar {
    manifest {
        attributes(
                "Main-Class": "de.staropensource.sosengine.testapp.Main"
        )
    }
}

// Configure application run task
application {
    mainClass.set("de.staropensource.sosengine.testapp.Main")
    applicationDefaultJvmArgs = [  // List of nice development configuration overrides
            // Set log level to DIAGNOSTIC
            "-Dsosengine.base.loggerLevel=diagnostic",

            // Force writing to standard output
            "-Dsosengine.base.loggerForceStandardOutput=true",

            // Force Jansi to write escape sequences
            "-Djansi.mode=force",
    ]
}

// GraalVM native-image plugin configuration
nativeImage {
    outputs.upToDateWhen { true }

    graalVmHome = project.hasProperty("graalHome") ? project.property("graalHome") as String : System.getProperty("java.home")
    executableName = "sosengine-testapp"
    outputDirectory = file("build/bin")
    setClasspath(shadowJar)
    buildType { BuildTypeSelector build ->
        build.executable {
            main = 'de.staropensource.sosengine.testapp.Main'
        }
    }

    arguments {
        add "-H:+UnlockExperimentalVMOptions"
        add "--color=always"
        add "-march=native"
        add "-O3"
        add "--no-fallback"
        add "--report-unsupported-elements-at-runtime"
        add "--enable-http"
        add "--enable-https"
        add "-H:IncludeResources=.*properties\$"
    }
}

// Register task for executing the generated binary
tasks.register('runNativeImage', Exec) {
    outputs.upToDateWhen { false }
    dependsOn(nativeImage)

    args(
            "-Dsosengine.base.loggerLevel=diagnostic",
            "-Dsosengine.base.loggerForceStandardOutput=true",
            "-Djansi.mode=force"
    )
    executable("build/bin/sosengine-testapp")
}