// JDK 11 JavaFX
plugins {
    id 'org.openjfx.javafxplugin' version '0.0.7' apply false
}

apply plugin: 'org.openjfx.javafxplugin'
javafx {
    version = "11.0.2"
    modules = [
        'javafx.controls',
        'javafx.fxml',
        'javafx.swing'
    ]
}

ext {
    tornadofx_version = '1.7.15'
    jna_version = '4.5.2'
    jediterm_version = '2.22'
    pty4j_version = '0.9.6'

    java_home = System.properties.'java.home'
    pkg_source = "$buildDir/packagesrc"
    pkg_outDir = "$buildDir/javapackage"
    dist_source = "$pkg_source/demobench-$version"
    pkg_version = version.indexOf('-') >= 0 ? version.substring(0, version.indexOf('-')) : version
    pkg_macosxKeyUserName = 'R3CEV'
}

apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'application'

evaluationDependsOn(':tools:explorer:capsule')
evaluationDependsOn(':testing:testserver:testcapsule:')

mainClassName = 'net.corda.demobench.DemoBench'
applicationDefaultJvmArgs = [
    '-Djava.util.logging.config.class=net.corda.demobench.config.LoggingConfig',
    '-Dorg.jboss.logging.provider=slf4j'
]

configurations {
    implementation {
        // We don't need Hibernate just for its @Type annotation.
        exclude group: 'org.hibernate', module: 'hibernate-core'
    }
}

dependencies {
    implementation project(':core')
    implementation project(':node')
    implementation project(':node-api')
    implementation project(':serialization')
    implementation project(':common-configuration-parsing')
    implementation project(':common-validation')
    implementation project(':client:rpc')

    implementation project(':finance:contracts')
    implementation project(':finance:workflows')
    implementation project(':tools:worldmap')

    // TornadoFX: A lightweight Kotlin framework for working with JavaFX UI's.
    implementation "no.tornado:tornadofx:$tornadofx_version"

    // Controls FX: more java FX components http://fxexperience.com/controlsfx/
    implementation "org.controlsfx:controlsfx:$controlsfx_version"

    implementation "com.h2database:h2:$h2_version"
    implementation "net.java.dev.jna:jna-platform:$jna_version"
    implementation "com.google.guava:guava:$guava_version"
    implementation "io.reactivex:rxjava:$rxjava_version"

    implementation "org.slf4j:log4j-over-slf4j:$slf4j_version"
    implementation "org.slf4j:jul-to-slf4j:$slf4j_version"
    implementation "org.apache.logging.log4j:log4j-slf4j2-impl:$log4j_version"
    implementation "org.apache.logging.log4j:log4j-core:$log4j_version"
    implementation "com.typesafe:config:$typesafe_config_version"

    // FontAwesomeFX: icons in the form of a font.
    implementation "de.jensd:fontawesomefx-fontawesome:$fontawesomefx_fontawesome_version"
    implementation "de.jensd:fontawesomefx-commons:$fontawesomefx_commons_version"

    implementation "org.jetbrains.jediterm:jediterm-pty:$jediterm_version"
    implementation("org.jetbrains.pty4j:pty4j:$pty4j_version") {
        exclude group: 'log4j'
    }

    testImplementation project(':core-test-utils')
    testImplementation project(':test-utils')
    testImplementation project(':testing:testserver')

    testRuntimeOnly "org.junit.vintage:junit-vintage-engine:${junit_vintage_version}"
    testRuntimeOnly "org.junit.platform:junit-platform-launcher:${junit_platform_version}"

    testImplementation "org.mockito.kotlin:mockito-kotlin:$mockito_kotlin_version"
    testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    testImplementation "org.assertj:assertj-core:$assertj_version"
    testImplementation "junit:junit:$junit_version"
}

tasks.withType(JavaCompile).configureEach {
    // Resolves a Gradle warning about not scanning for pre-processors.
    options.compilerArgs << '-proc:none'
}

jar {
    manifest {
        attributes(
            'Main-Class': mainClassName,
            'Class-Path': configurations.runtimeClasspath.collect { it.name }.join(' '),
        )
    }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}


test {
    systemProperty 'java.util.logging.config.class', 'net.corda.demobench.config.LoggingConfig'
    systemProperty 'org.jboss.logging.provider', 'slf4j'
}

distributions {
    main() {
        contents {
            from(project(':tools:explorer:capsule').tasks.buildExplorerJAR) {
                rename 'node-explorer-(.*)', 'node-explorer.jar'
                into 'explorer'
                fileMode = 0444
            }
            from(project(':node:capsule').tasks.buildCordaJAR) {
                rename 'corda-(.*)', 'corda.jar'
                into 'corda'
                fileMode = 0444
            }
            from(project(':testing:testserver:testcapsule:').tasks.buildWebserverJar) {
                rename 'corda-testserver-(.*)', 'corda-testserver.jar'
                into 'corda'
                fileMode = 0444
            }
            from(project(':finance:contracts').tasks.jar) {
                rename 'corda-finance-contracts(.*)', 'corda-finance-contracts.jar'
                into 'cordapps'
                fileMode = 0444
            }
            from(project(':finance:workflows').tasks.jar) {
                rename 'corda-finance-workflows(.*)', 'corda-finance-workflows.jar'
                into 'cordapps'
                fileMode = 0444
            }
        }
    }
}

/*
 * Bundles the application using JavaPackager,
 * using the ZIP distribution as source.
 */
tasks.register('javapackage') {
    dependsOn distZip

    doLast {
        delete([pkg_source, pkg_outDir])

        copy {
            from(zipTree(distZip.outputs.files.singleFile))
            into pkg_source
        }

        copy {
            /*
             * Copy non-text formats "as-is".
             */
            from("$projectDir/package") {
                exclude '**/*.spec'
                exclude '**/*.sh'
                exclude '**/*.wsf'
                exclude '**/*.manifest'
            }
            into "$pkg_source/package"
        }

        copy {
            /*
             * Expand tokens for text formats.
             */
            from("$projectDir/package") {
                include '**/*.spec'
                include '**/*.sh'
                include '**/*.wsf'
                include '**/*.manifest'
            }
            filter { line ->
                line.replaceAll('@pkg_version@', pkg_version).replaceAll('@signingKeyUserName@', pkg_macosxKeyUserName)
            }
            into "$pkg_source/package"
        }

        ant.taskdef(
                resource: 'com/sun/javafx/tools/ant/antlib.xml',
                classpath: "$pkg_source:$java_home/../lib/ant-javafx.jar"
        )

        ant.deploy(nativeBundles: packageType, outdir: pkg_outDir, outfile: 'DemoBench', verbose: 'true') {
            application(name: 'Corda DemoBench', version: pkg_version, mainClass: mainClassName)
            info(title: 'Corda DemoBench', vendor: 'R3', description: 'A sales and educational tool for demonstrating Corda.')
            //noinspection GroovyAssignabilityCheck
            resources {
                fileset(dir: "$dist_source/lib", type: 'jar') {
                    include(name: '*.jar')
                }

                fileset(dir: dist_source, type: 'data') {
                    include(name: 'corda/*.jar')
                    include(name: 'cordapps/*.jar')
                    include(name: 'explorer/*.jar')
                }

                fileset(dir: "$pkg_source/package", type: 'data') {
                    include(name: "bugfixes/**")
                }
            }

            // This is specific to MacOSX packager.
            bundleArgument(arg: 'mac.signing-key-user-name', value: pkg_macosxKeyUserName)

            platform {
                //noinspection GroovyAssignabilityCheck
                property(name: 'java.util.logging.config.class', value: 'net.corda.demobench.config.LoggingConfig')
                //noinspection GroovyAssignabilityCheck
                property(name: 'org.jboss.logging.provider', value: 'slf4j')
            }

            preferences(install: true)
        }
    }
}
