plugins {
    id 'edu.wpi.first.WpilibTools' version '1.3.0'
}

import java.nio.file.Path

ext.licenseFile = file("$rootDir/LICENSE")
apply from: "${rootDir}/shared/common.gradle"

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

def nativeConfigName = 'wpilibNatives'
def nativeConfig = configurations.create(nativeConfigName)

def nativeTasks = wpilibTools.createExtractionTasks {
    configurationName = nativeConfigName
}

nativeTasks.addToSourceSetResources(sourceSets.main)

nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpimath")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpinet")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpiutil")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("ntcore")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("cscore")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("apriltag")
nativeConfig.dependencies.add wpilibTools.deps.wpilib("hal")
nativeConfig.dependencies.add wpilibTools.deps.wpilibOpenCv("frc" + openCVYear, wpi.versions.opencvVersion.get())

dependencies {
    // JOGL stuff (currently we only distribute for aarch64, which is Pi 4)
    implementation "org.jogamp.gluegen:gluegen-rt:$joglVersion"
    implementation "org.jogamp.jogl:jogl-all:$joglVersion"
    implementation "org.jogamp.gluegen:gluegen-rt:$joglVersion:natives-linux-aarch64"
    implementation "org.jogamp.jogl:jogl-all:$joglVersion:natives-linux-aarch64"

    // Zip
    implementation 'org.zeroturnaround:zt-zip:1.14'

    implementation "org.xerial:sqlite-jdbc:3.41.0.0"
    implementation("org.photonvision:rknn_jni-jni:$rknnVersion:linuxarm64") {
        transitive = false
    }
    implementation("org.photonvision:rknn_jni-java:$rknnVersion") {
        transitive = false
    }
    implementation("org.photonvision:photon-libcamera-gl-driver-jni:$libcameraDriverVersion:linuxarm64") {
        transitive = false
    }
    implementation "org.photonvision:photon-libcamera-gl-driver-java:$libcameraDriverVersion"

    implementation "org.photonvision:photon-mrcal-java:$mrcalVersion"

    // Only include mrcal natives on platforms that we build for
    if (!(jniPlatform in [
                "osxx86-64",
                "osxarm64"
            ])) {
        implementation("org.photonvision:photon-mrcal-jni:$mrcalVersion:$wpilibNativeName") {
            transitive = false
        }
    }

    testImplementation group: 'org.junit-pioneer' , name: 'junit-pioneer', version: '2.2.0'
}

task writeCurrentVersion {
    doLast {
        def versionFileIn = file("${rootDir}/shared/PhotonVersion.java.in")
        writePhotonVersionFile(versionFileIn, Path.of("$buildDir", "generated", "java", "org", "photonvision", "PhotonVersion.java"),
                versionString)
    }
}
// https://github.com/wpilibsuite/allwpilib/blob/main/wpilibj/build.gradle#L52
sourceSets.main.java.srcDir "${buildDir}/generated/java/"

compileJava.dependsOn writeCurrentVersion
