apply plugin: 'maven-publish'
apply plugin: 'java-library'
apply plugin: 'jacoco'
apply plugin: 'com.google.protobuf'

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

def baseArtifactId = nativeName
def artifactGroupId = 'org.photonvision'
def javaBaseName = "_GROUP_org_photonvision_${baseArtifactId}_ID_${baseArtifactId}-java_CLS"

def outputsFolder = file("$buildDir/outputs")
def licenseFile = ext.licenseFile

javadoc {
    options {
        encoding = 'UTF-8'
        links "https://github.wpilib.org/allwpilib/docs/release/java/"
    }
}

jar {
    from licenseFile
}

task sourcesJar(type: Jar, dependsOn: classes) {
    archiveClassifier = 'sources'
    from sourceSets.main.allSource
    from licenseFile
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    archiveClassifier = 'javadoc'
    from javadoc.destinationDir
    from licenseFile
}

task outputJar(type: Jar, dependsOn: classes) {
    archiveBaseName = javaBaseName
    destinationDirectory = outputsFolder
    from sourceSets.main.output
    from licenseFile
}

task outputSourcesJar(type: Jar, dependsOn: classes) {
    archiveBaseName = javaBaseName
    destinationDirectory = outputsFolder
    archiveClassifier = 'sources'
    from sourceSets.main.allSource
    from licenseFile
}

task outputJavadocJar(type: Jar, dependsOn: javadoc) {
    archiveBaseName = javaBaseName
    destinationDirectory = outputsFolder
    archiveClassifier = 'javadoc'
    from javadoc.destinationDir
    from licenseFile
}

artifacts {
    archives sourcesJar
    archives javadocJar
    archives outputJar
    archives outputSourcesJar
    archives outputJavadocJar
}

addTaskToCopyAllOutputs(outputSourcesJar)
addTaskToCopyAllOutputs(outputJavadocJar)
addTaskToCopyAllOutputs(outputJar)

build.dependsOn outputSourcesJar
build.dependsOn outputJavadocJar
build.dependsOn outputJar

publishing {
    publications {
        java(MavenPublication) {
            artifact jar
            artifact sourcesJar
            artifact javadocJar

            artifactId = "${baseArtifactId}-java"
            groupId artifactGroupId
            version pubVersion
        }
    }

    repositories {
        maven {
            // If we're trying to copy local outputs, just throw everything into build/maven
            // The problem here is we can't specify which repo to publish to easily, so we have to choose one or the other
            if (project.hasProperty('copyOfflineArtifacts')) {
                url(localMavenURL)
            } else {
                url(photonMavenURL)
                credentials {
                    username 'ghactions'
                    password System.getenv("ARTIFACTORY_API_KEY")
                }
            }
        }
    }
}

test {
    useJUnitPlatform()
    systemProperty 'junit.jupiter.extensions.autodetection.enabled', 'true'
    testLogging {
        events "failed"
        exceptionFormat "full"
    }
    finalizedBy jacocoTestReport
}

wpilibTools.deps.wpilibVersion = wpi.versions.wpilibVersion.get()

dependencies {
    if(project.hasProperty('includePhotonTargeting')) {
        implementation project(":photon-targeting")
    }

    implementation wpilibTools.deps.wpilibJava("wpiutil")
    implementation wpilibTools.deps.wpilibJava("cameraserver")
    implementation wpilibTools.deps.wpilibJava("cscore")
    implementation wpilibTools.deps.wpilibJava("wpinet")
    implementation wpilibTools.deps.wpilibJava("wpimath")
    implementation wpilibTools.deps.wpilibJava("ntcore")
    implementation wpilibTools.deps.wpilibJava("hal")
    implementation wpilibTools.deps.wpilibJava("wpilibj")
    implementation wpilibTools.deps.wpilibJava("apriltag")
    implementation wpilibTools.deps.wpilibJava("wpiunits")
    implementation wpilibTools.deps.wpilibOpenCvJava("frc" + openCVYear, wpi.versions.opencvVersion.get())

    implementation group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: wpi.versions.jacksonVersion.get()
    implementation group: "com.fasterxml.jackson.core", name: "jackson-core", version: wpi.versions.jacksonVersion.get()
    implementation group: "com.fasterxml.jackson.core", name: "jackson-databind", version: wpi.versions.jacksonVersion.get()

    implementation group: "org.ejml", name: "ejml-simple", version: wpi.versions.ejmlVersion.get()
    implementation group: "us.hebi.quickbuf", name: "quickbuf-runtime", version: wpi.versions.quickbufVersion.get();

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}

jacoco {
    toolVersion = "0.8.10"
}

jacocoTestReport {
    reports {
        xml.required = true
        html.required = true
    }
}


protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.21.12'
    }
    plugins {
        quickbuf {
            artifact = 'us.hebi.quickbuf:protoc-gen-quickbuf:1.3.3'
        }
    }
    generateProtoTasks {
        all().configureEach { task ->
            task.builtins {
                // cpp {}
                // The protobuf-java code is bad -- use quickbuf
                remove java
            }
            task.plugins {
                quickbuf {
                    option "gen_descriptors=true"
                }
            }
        }
    }
}
