/*
 * Copyright (C) 2018 Google LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.bmuschko:gradle-docker-plugin:6.2.0'
    }
}

plugins {
    id 'java'
    id 'application'
    id 'com.github.hierynomus.license' version '0.16.1'
}

def transcoderJvmArgs = ["-Dorg.dcm4che3.imageio.codec.ImageReaderFactory=com/google/cloud/healthcare/imaging/dicomadapter/transcoder/ImageReaderFactory.properties",
                         "-Dorg.dcm4che3.imageio.codec.ImageWriterFactory=com/google/cloud/healthcare/imaging/dicomadapter/transcoder/ImageWriterFactory.properties"]

mainClassName = "com.google.cloud.healthcare.imaging.dicomadapter.ImportAdapter"
run {
    // Accept command line arguments via gradle run -Dexec.args="<args>"
    args System.getProperty("exec.args", "").split()
    jvmArgs(transcoderJvmArgs)
}

test {
    jvmArgs(transcoderJvmArgs)
}

buildDir = '/tmp/gradle_build/dicom_adapter/import'

sourceCompatibility = 1.11
version = '0.2'

compileJava {
    // Print out detailed deprecation warnings.
    options.deprecation = true
}

repositories {
    mavenCentral()
    maven { url 'https://www.dcm4che.org/maven2/' }
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation platform('com.google.cloud:libraries-bom:26.43.0')
    implementation 'com.google.guava:guava:33.1.0-jre'
    implementation 'com.google.auth:google-auth-library-oauth2-http'
    implementation "com.google.api-client:google-api-client"
    implementation "org.json:json:20240303"

    implementation "com.beust:jcommander:1.72"
    implementation "org.dcm4che:dcm4che-core:5.31.2"
    implementation "org.dcm4che:dcm4che-net:5.31.2"

    implementation "org.dcm4che:dcm4che-imageio:5.31.2"
    runtimeOnly "org.dcm4che:dcm4che-imageio-rle:5.31.2"
    implementation project(":jai-imageio-jpeg2000")

    implementation project(":dicom_util")
    implementation project(":util")

    implementation project(":deid-redactor")

    implementation "com.google.cloud:google-cloud-storage"
    implementation 'com.google.cloud:google-cloud-nio'

    testImplementation "com.google.truth:truth:1.0"
    testImplementation "org.mockito:mockito-core:3.+"
    testImplementation "junit:junit:4.2"
    testImplementation project(path: ":dicom_util", configuration: 'testArtifacts')
    testImplementation project(path: ":util", configuration: 'testArtifacts')

    testImplementation 'org.eclipse.jetty:jetty-http:9.4.55.v20240627'
}

apply plugin: 'com.bmuschko.docker-java-application'
docker {
    javaApplication {
        baseImage = 'eclipse-temurin:11-jammy'
        images = [project.findProperty("docker_tag") ?: "import_adapter"]
        jvmArgs = transcoderJvmArgs
    }
}

// TODO(b/77952311): Factor common gradle tasks between import/export adapters.
// Download open source license texts.
task downloadOpensSourceLicenses(type: nl.javadude.gradle.plugins.license.DownloadLicenses) {
  // These libraries have their license in a non-standard location.
  licenses = specialCaseLicenses
  dependencyConfiguration = 'runtimeClasspath'
  doLast {
    downloadLicensesText(buildDir)
  }
}

// Download full source jar for all dependencies.
// https://stackoverflow.com/questions/16154458/how-to-copy-all-source-jars-using-gradle
task downloadSourceJars( type: Copy ) {
  def sources = configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.collect { artifact ->
    project.dependencies.create( [
      group: artifact.moduleVersion.id.group,
      name: artifact.moduleVersion.id.name,
      version: artifact.moduleVersion.id.version,
      classifier: 'sources'
    ] )
  }
  from configurations.detachedConfiguration( sources as Dependency[] )
    .resolvedConfiguration.lenientConfiguration.getFiles( Specs.SATISFIES_ALL )
  into file( buildDir.toString() + "/docker/source" )
}

// OSS release requires license and source code to be included in Docker image.
task copySourceAndLicenseToDockerImage {
  dependsOn tasks.downloadOpensSourceLicenses
  dependsOn tasks.downloadSourceJars
}

task printVersion {
    doLast {
        println project.version
    }
}

dockerSyncBuildContext.finalizedBy tasks.copySourceAndLicenseToDockerImage
dockerCreateDockerfile {
    instruction 'ADD license /usr/share/oss-license'
    instruction 'ADD source /usr/share/oss-source'
}

