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:3.0'
    compileOnly 'org.junit.jupiter:junit-jupiter-api:5.14.2'
    compileOnly 'org.junit.jupiter:junit-jupiter-params:5.14.2'

    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 ('io.modelcontextprotocol.sdk:mcp-core:1.0.0')  {
        exclude group: "com.ethlo.time", module: "itu"
    }
    api ('io.modelcontextprotocol.sdk:mcp-json-jackson2:1.0.0')  {
        exclude group: "com.ethlo.time", module: "itu"
        exclude group: "com.fasterxml.jackson.dataformat", module: "jackson-dataformat-yaml"
    }

    api "io.projectreactor:reactor-core:3.7.9"
    api "org.reactivestreams:reactive-streams:1.0.4"
    api ("com.networknt:json-schema-validator:1.5.8") {
        exclude group: "com.ethlo.time", module: "itu"
        exclude group: "com.fasterxml.jackson.dataformat", module: "jackson-dataformat-yaml"
    }

    api "com.github.weisj:jsvg:1.7.2"
    api 'io.xpipe:vernacular:1.16'
    api 'org.bouncycastle:bcprov-jdk18on:1.83'
    api 'info.picocli:picocli:4.7.7'
    api 'org.apache.commons:commons-lang3:3.20.0'
    api 'io.sentry:sentry:8.20.0'
    api 'commons-io:commons-io:2.21.0'
    api "com.fasterxml.jackson.core:jackson-databind:2.21.1"
    api "com.fasterxml.jackson.core:jackson-annotations:2.21"
    api "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.21.1"
    api "org.kordamp.ikonli:ikonli-material2-pack:12.4.0"
    api "org.kordamp.ikonli:ikonli-materialdesign2-pack:12.4.0"
    api 'org.kordamp.ikonli:ikonli-bootstrapicons-pack:12.4.0'
    api "org.kordamp.ikonli:ikonli-javafx:12.4.0"
    api "org.slf4j:slf4j-api:2.0.17"
    api "org.slf4j:slf4j-jdk-platform-logging:2.0.17"
    api 'io.xpipe:modulefs:0.1.8'
    api 'net.synedra:validatorfx:0.4.2'
    api files("$rootDir/gradle/gradle_scripts/atlantafx-base-2.0.2.jar")

    api("org.int4.fx:fx-values:0.4")
    api files("$rootDir/gradle/gradle_scripts/fx-builders-1.0.0-SNAPSHOT.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 = groupName + '.app'
    mainClass = groupName + '.app.Main'
    applicationDefaultJvmArgs = jvmRunArgs
}

run {
    systemProperty propertyName('useVirtualThreads'), 'false'
    systemProperty propertyName('mode'), 'gui'
    systemProperty propertyName('writeLogs'), "true"
    systemProperty propertyName('writeSysOut'), "true"
    systemProperty propertyName('developerMode'), "true"
    systemProperty propertyName('logLevel'), "trace"
    systemProperty propertyName('fullVersion'), fullVersion
    systemProperty propertyName('staging'), isStage

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

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

    def exts = files(project.allExtensions.stream().map(p -> p.getTasksByName('jar', true)[0].outputs.files.singleFile).toList())
    classpath += exts

    dependsOn(project.allExtensions.stream().map(p -> p.getTasksByName('jar', true)[0]).toList())
}

tasks.register('runAttachedDebugger', JavaExec) {
    workingDir = rootDir
    classpath = run.classpath
    mainModule = groupName + '.app'
    mainClass = groupName + '.app.Main'
    modularity.inferModulePath = true
    jvmArgs += jvmRunArgs
    jvmArgs += List.of(
            "-javaagent:${System.getProperty("user.home")}/.attachme/attachme-agent-1.2.9.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

    def exts = files(project.allExtensions.stream().map(p -> p.getTasksByName('jar', true)[0].outputs.files.singleFile).toList())
    classpath += exts
    dependsOn(project.allExtensions.stream().map(p -> p.getTasksByName('jar', true)[0]).toList())

}

processResources {
    doLast {
        def cssFiles = fileTree(dir: "$sourceSets.main.output.resourcesDir/io/xpipe/app/resources/style")
        cssFiles.include "**/*.css"
        cssFiles.each { css ->
            providers.javaexec {
                workingDir = projectDir
                jvmArgs += ["--module-path=${configurations.javafx.asFileTree.asPath},", "--add-modules=javafx.graphics"]
                mainClass = "com.sun.javafx.css.parser.Css2Bin"
                args css
            }.result.get()

            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
        }
    }
}

distTar {
    enabled = false
}

distZip {
    enabled = false
}

assembleDist {
    enabled = false
}