plugins {
    id 'application'
    id 'jvm-test-suite'
    id 'java-library'
}

repositories {
    mavenCentral()
}

apply from: "$rootDir/gradle/gradle_scripts/java.gradle"
apply from: "$rootDir/gradle/gradle_scripts/javafx.gradle"
apply from: "$rootDir/gradle/gradle_scripts/jna.gradle"
apply from: "$rootDir/gradle/gradle_scripts/lombok.gradle"

configurations {
    implementation.extendsFrom(javafx)
    api.extendsFrom(jna)
}

dependencies {
    api project(':core')
    api project(':beacon')

    compileOnly 'org.hamcrest:hamcrest:2.2'
    compileOnly 'org.junit.jupiter:junit-jupiter-api:5.10.3'
    compileOnly 'org.junit.jupiter:junit-jupiter-params:5.10.3'

    api 'com.vladsch.flexmark:flexmark:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-options:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-data:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-ast:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-builder:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-sequence:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-misc:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-dependency:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-collection:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-format:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-html:0.64.8'
    api 'com.vladsch.flexmark:flexmark-util-visitor:0.64.8'
    api 'com.vladsch.flexmark:flexmark-ext-tables:0.64.8'
    api 'com.vladsch.flexmark:flexmark-ext-gfm-strikethrough:0.64.8'
    api 'com.vladsch.flexmark:flexmark-ext-gfm-tasklist:0.64.8'
    api 'com.vladsch.flexmark:flexmark-ext-footnotes:0.64.8'
    api 'com.vladsch.flexmark:flexmark-ext-definition:0.64.8'
    api 'com.vladsch.flexmark:flexmark-ext-anchorlink:0.64.8'
    api 'com.vladsch.flexmark:flexmark-ext-yaml-front-matter:0.64.8'
    api 'com.vladsch.flexmark:flexmark-ext-toc:0.64.8'

    api files("$rootDir/gradle/gradle_scripts/markdowngenerator-1.3.1.1.jar")
    api files("$rootDir/gradle/gradle_scripts/vernacular-1.16.jar")
    api 'org.bouncycastle:bcprov-jdk18on:1.78.1'
    api 'info.picocli:picocli:4.7.6'
    api ('org.kohsuke:github-api:1.323') {
        exclude group: 'org.apache.commons', module: 'commons-lang3'
    }
    api 'org.apache.commons:commons-lang3:3.15.0'
    api 'io.sentry:sentry:7.12.1'
    api 'commons-io:commons-io:2.16.1'
    api group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: "2.17.2"
    api group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: "2.17.2"
    api group: 'org.kordamp.ikonli', name: 'ikonli-material2-pack', version: "12.2.0"
    api group: 'org.kordamp.ikonli', name: 'ikonli-materialdesign2-pack', version: "12.2.0"
    api group: 'org.kordamp.ikonli', name: 'ikonli-javafx', version: "12.2.0"
    api group: 'org.kordamp.ikonli', name: 'ikonli-material-pack', version: "12.2.0"
    api group: 'org.kordamp.ikonli', name: 'ikonli-feather-pack', version: "12.2.0"
    api group: 'org.slf4j', name: 'slf4j-api', version: '2.0.13'
    api group: 'org.slf4j', name: 'slf4j-jdk-platform-logging', version: '2.0.13'
    api 'io.xpipe:modulefs:0.1.5'
    api 'net.synedra:validatorfx:0.4.2'
    api files("$rootDir/gradle/gradle_scripts/atlantafx-base-2.0.2.jar")
}

apply from: "$rootDir/gradle/gradle_scripts/local_junit_suite.gradle"

def extensionJarDepList = project.allExtensions.stream().map(p -> p.getTasksByName('jar', true)).toList();
jar {
    finalizedBy(extensionJarDepList)
}

application {
    mainModule = 'io.xpipe.app'
    mainClass = 'io.xpipe.app.Main'
    applicationDefaultJvmArgs = jvmRunArgs
}

run {
    systemProperty 'io.xpipe.app.useVirtualThreads', 'false'
    systemProperty 'io.xpipe.app.mode', 'gui'
    systemProperty 'io.xpipe.app.writeLogs', "true"
    systemProperty 'io.xpipe.app.writeSysOut', "true"
    systemProperty 'io.xpipe.app.developerMode', "true"
    systemProperty 'io.xpipe.app.logLevel', "trace"
    systemProperty 'io.xpipe.app.fullVersion', rootProject.fullVersion
    systemProperty 'io.xpipe.app.staging', isStage

    // Apply passed xpipe properties
    for (final def e in System.getProperties().entrySet()) {
        if (e.getKey().toString().contains("xpipe")) {
            systemProperty e.getKey().toString(), e.getValue()
        }
    }

    workingDir = rootDir
    jvmArgs += ['-XX:+EnableDynamicAgentLoading']
}

task runAttachedDebugger(type: JavaExec) {
    workingDir = rootDir
    classpath = run.classpath
    mainModule = 'io.xpipe.app'
    mainClass = 'io.xpipe.app.Main'
    modularity.inferModulePath = true
    jvmArgs += jvmRunArgs
    jvmArgs += List.of(
            "-javaagent:${System.getProperty("user.home")}/.attachme/attachme-agent-1.2.4.jar=port:7857,host:localhost".toString(),
            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=127.0.0.1:0"
    )
    jvmArgs += ['-XX:+EnableDynamicAgentLoading']
    systemProperties run.systemProperties
}

processResources {
    doLast {
        def cssFiles = fileTree(dir: "$sourceSets.main.output.resourcesDir/io/xpipe/app/resources/style")
        cssFiles.include "**/*.css"
        cssFiles.each { css ->
            logger.info("converting CSS to BSS ${css}");

            javaexec {
                workingDir = project.projectDir
                jvmArgs += "--module-path=${configurations.javafx.asFileTree.asPath},"
                jvmArgs += "--add-modules=javafx.graphics"
                main = "com.sun.javafx.css.parser.Css2Bin"
                args css
            }

            delete css
        }
    }

    doLast {
        def resourcesDir = new File(sourceSets.main.output.resourcesDir, "io/xpipe/app/resources/third-party")
        resourcesDir.mkdirs()
        copy {
            from "$rootDir/dist/licenses"
            into resourcesDir
        }
    }

    doLast {
        copy {
            from file("$rootDir/openapi.yaml")
            into file("${sourceSets.main.output.resourcesDir}/io/xpipe/app/resources/misc");
        }
    }
}

distTar {
    enabled = false;
}

distZip {
    enabled = false;
}
