plugins {
    id 'application'
}

sourceSets {
    testPlugin
}

def MAIN_CLASS = 'io.github.headlesshq.headlessmc.wrapper.Main'
application {
    mainClass = MAIN_CLASS
}

run {
    standardInput = System.in
}

dependencies {
    jarLibs project(':headlessmc-modlauncher')
    jarLibs group: 'dev.xdark', name: 'deencapsulation', version: '1.0'
    // jarLibs project(':headlessmc-logging') TODO: add logging for wrapper and modlauncher?!

    testPluginCompileOnly project(':headlessmc-launcher')
    testPluginImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.0'

    testPluginCompileOnly 'org.projectlombok:lombok:1.18.34'
    testPluginAnnotationProcessor 'org.projectlombok:lombok:1.18.34'
}

processResources {
    into('headlessmc') {
        from project(':headlessmc-launcher')['shadowJar']
        rename 'headlessmc-launcher(.*).jar', 'headlessmc-launcher.jar'
    }
}

// shadowJar always explodes the contents of jars and cannot distinguish between
// a dependency and a jar in our resources, this works around that.
// https://github.com/johnrengelman/shadow/issues/111#issuecomment-317233031
tasks.register('shadowBugWorkaround', Jar) {
    dependsOn jar
    dependsOn project(':headlessmc-launcher')['shadowJar']

    destinationDirectory.set(layout.buildDirectory.dir('shadow-bug-workaround'))

    into ('headlessmc') {
        from project(':headlessmc-launcher')['shadowJar']
        rename 'headlessmc-launcher(.*).jar', 'headlessmc-launcher.jar'
    }
}

shadowJar {
    dependsOn shadowBugWorkaround
    dependsOn project(':headlessmc-modlauncher')['shadowJar']

    from shadowBugWorkaround
    from layout.buildDirectory.dir('shadow-bug-workaround')
    from project(':headlessmc-modlauncher')['shadowJar']

    // honestly not exactly sure whats going on here its all a bit fishy
    // https://github.com/GradleUp/shadow/issues/299#issuecomment-310663737
    include '*.jar'
    include 'dev/xdark/*'
    include 'dev/xdark/**/*'
    include 'io/github/headlesshq/headlessmc/wrapper/*'
    include 'io/github/headlesshq/headlessmc/wrapper/**/*'
    include 'io/github/headlesshq/headlessmc/modlauncher/**'
    include 'io/github/headlesshq/headlessmc/modlauncher/**/*'
    include 'io/github/headlesshq/headlessmc/logging/**'
    include 'io/github/headlesshq/headlessmc/logging/**/*'
    // include "headlessmc/headlessmc-launcher.jar" TODO: idk why this does not work?
    include '**/headlessmc-launcher*.jar'
    include 'META-INF/MANIFEST.MF'
}

tasks.withType(JacocoReport) {
    afterEvaluate {
        classDirectories.setFrom(files(classDirectories.files.collect {
            def tree = fileTree(dir: it, exclude: [
                    '**/headlessmc/headlessmc-launcher.jar',
                    '**/headlessmc/headlessmc-runtime.jar',
                    '**/headlessmc/headlessmc-lwjgl.jar',
                    '**/headlessmc/forge-cli.jar',
                    'io/github/headlesshq/headlessmc/lwjgl/agent/**',
                    'io/github/headlesshq/headlessmc/web/**',
                    'io/github/headlesshq/headlessmc/web/**/*.*',
                    'io/github/headlesshq/headlessmc/web/cheerpj/**/*.*',
                    'io/github/headlesshq/headlessmc/web/cheerpj/plugin/**',
                    'io/github/headlesshq/headlessmc/web/cheerpj/plugin/*.*'
            ])
            // because 'io/github/headlesshq/headlessmc/web/cheerpj/plugin/**' does not work
            return tree.exclude { details ->
                return details.file.canonicalPath.contains('headlessmc-web')
            }
        }))
    }

    additionalClassDirs(project(':headlessmc-launcher').layout.buildDirectory.dir('classes/java/main').get().asFile)
    additionalClassDirs(project(':headlessmc-auth').layout.buildDirectory.dir('classes/java/main').get().asFile)
    additionalClassDirs(project(':headlessmc-api').layout.buildDirectory.dir('classes/java/main').get().asFile)
    additionalClassDirs(project(':headlessmc-lwjgl').layout.buildDirectory.dir('classes/java/main').get().asFile)
    additionalClassDirs(project(':headlessmc-modlauncher').layout.buildDirectory.dir('classes/java/main').get().asFile)
    additionalClassDirs(project(':headlessmc-runtime').layout.buildDirectory.dir('classes/java/main').get().asFile)

    reports {
        xml.required.set(true)
    }
}

tasks.register('testPluginJar', Jar) {
    from sourceSets.testPlugin.output
    archiveBaseName.set('testPlugin')
    archiveVersion.set('')
}

tasks.withType(Test).configureEach {
    dependsOn testPluginJar
    project.logger.info("Enabling integration test: ${hmc_integration_test_enabled}")
    systemProperties["hmc.integration.test.enabled"] = "${hmc_integration_test_enabled}"
    if (JavaVersion.current() >= JavaVersion.VERSION_21
        && "${hmc_integration_test_enabled}".toBoolean()) {
        systemProperties["java.security.manager"] = "allow"
    }

    testLogging {
        showStandardStreams = true
    }

    jacoco {
        // our classloader potentially includes no ProtectionDomains?
        includeNoLocationClasses true
        jacoco.excludes = ['jdk.internal.*', 'io.github.headlesshq.headlessmc.lwjgl.agent.*']
    }
}

test {
    maxHeapSize = '2048m'
}
