plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

ext {
    PUBLISH_ARTIFACT_ID = 'godot-openxr-vendors'
}

apply from: "../scripts/publish-module.gradle"

def khronosOpenxrLoaderAarDir = file("${buildDir}/temp/khronos_openxr_loader_aar")
def khronosOpenxrLoaderTargetDir = file("${buildDir}/temp/khronos_openxr_sdk")

task extractKhronosOpenxrLoader {
    def khronosOpenxrLoader = configurations.detachedConfiguration(dependencies.create("org.khronos.openxr:openxr_loader_for_android:$versions.openxrVersion"))

    doLast {
        khronosOpenxrLoaderAarDir.mkdirs()

        copy {
            from zipTree(khronosOpenxrLoader.singleFile)
            into khronosOpenxrLoaderAarDir
        }
    }
}

task copyKhronosOpenxrLoader(dependsOn: extractKhronosOpenxrLoader) {
    def khronosOpenxrLoaderTargetDirArm64 = file("${khronosOpenxrLoaderTargetDir}/arm64-v8a/arm64-v8a")
    def khronosOpenxrLoaderTargetDirX86_64 = file("${khronosOpenxrLoaderTargetDir}/x86_64/x86_64")

    doLast {
        khronosOpenxrLoaderTargetDirArm64.mkdirs()
        khronosOpenxrLoaderTargetDirX86_64.mkdirs()

        copy {
            from "${khronosOpenxrLoaderAarDir}/prefab/modules/openxr_loader/libs/android.arm64-v8a/libopenxr_loader.so"
            into khronosOpenxrLoaderTargetDirArm64
        }
        copy {
            from "${khronosOpenxrLoaderAarDir}/prefab/modules/openxr_loader/libs/android.x86_64/libopenxr_loader.so"
            into khronosOpenxrLoaderTargetDirX86_64
        }
    }
}

task cleanKhronosOpenxrLoader(type: Delete) {
    delete khronosOpenxrLoaderAarDir
    delete khronosOpenxrLoaderTargetDir
}

android {
    compileSdk versions.compileSdk
    ndkVersion versions.ndkVersion

    defaultConfig {
        minSdk versions.minSdk
        targetSdk versions.targetSdk
        versionName getReleaseVersion()

        setProperty("archivesBaseName", "godotopenxr")

        ndk {
            //noinspection ChromeOsAbiSupport
            abiFilters "arm64-v8a"
        }
    }
    externalNativeBuild {
        cmake {
            path file('CMakeLists.txt')
        }
    }

    buildFeatures {
        buildConfig = true
    }

    namespace = "org.godotengine.openxr.vendors"

    flavorDimensions = ["vendor"]
    productFlavors {
        khronos {
            dimension "vendor"
            ndk {
                //noinspection ChromeOsAbiSupport
                abiFilters 'arm64-v8a', 'x86_64'
            }
        }
        lynx {
            dimension "vendor"
        }
        magicleap {
            dimension "vendor"
            ndk {
                //noinspection ChromeOsAbiSupport
                abiFilters 'arm64-v8a', 'x86_64'
            }
        }
        meta {
            dimension "vendor"
            ndk {
                //noinspection ChromeOsAbiSupport
                abiFilters 'arm64-v8a', 'x86_64'
            }
        }
        pico {
            dimension "vendor"
        }
    }

    sourceSets {
        debug.jniLibs.srcDirs += ['src/main/libs/debug/arm64-v8a']
        release.jniLibs.srcDirs += ['src/main/libs/release/arm64-v8a']

        khronos.jniLibs.srcDirs += [
                "${khronosOpenxrLoaderTargetDir}/arm64-v8a",
                "${khronosOpenxrLoaderTargetDir}/x86_64",
        ]
        khronosDebug.jniLibs.srcDirs += ['src/main/libs/debug/x86_64']
        khronosRelease.jniLibs.srcDirs += ['src/main/libs/release/x86_64']

        lynx.jniLibs.srcDirs += ["${khronosOpenxrLoaderTargetDir}/arm64-v8a"]

        magicleap.jniLibs.srcDirs += [
                "${khronosOpenxrLoaderTargetDir}/arm64-v8a",
                "${khronosOpenxrLoaderTargetDir}/x86_64",
        ]
        magicleapDebug.jniLibs.srcDirs += ['src/main/libs/debug/x86_64']
        magicleapRelease.jniLibs.srcDirs += ['src/main/libs/release/x86_64']

        meta.jniLibs.srcDirs += [
                "${khronosOpenxrLoaderTargetDir}/arm64-v8a",
                "${khronosOpenxrLoaderTargetDir}/x86_64",
        ]
        metaDebug.jniLibs.srcDirs += ['src/main/libs/debug/x86_64']
        metaRelease.jniLibs.srcDirs += ['src/main/libs/release/x86_64']

        pico.jniLibs.srcDirs += ["${khronosOpenxrLoaderTargetDir}/arm64-v8a"]
    }

    packagingOptions {
        if (shouldNotStrip()) {
            doNotStrip '**/*.so'
        }
    }

    compileOptions {
        sourceCompatibility versions.javaVersion
        targetCompatibility versions.javaVersion
    }

    kotlinOptions {
        jvmTarget = versions.javaVersion
    }

    publishing {
        singleVariant("khronosDebug") {
            withSourcesJar()
            withJavadocJar()
        }
        singleVariant("khronosRelease") {
            withSourcesJar()
            withJavadocJar()
        }

        singleVariant("lynxDebug") {
            withSourcesJar()
            withJavadocJar()
        }
        singleVariant("lynxRelease") {
            withSourcesJar()
            withJavadocJar()
        }

        singleVariant("magicleapDebug") {
            withSourcesJar()
            withJavadocJar()
        }
        singleVariant("magicleapRelease") {
            withSourcesJar()
            withJavadocJar()
        }

        singleVariant("metaDebug") {
            withSourcesJar()
            withJavadocJar()
        }
        singleVariant("metaRelease") {
            withSourcesJar()
            withJavadocJar()
        }

        singleVariant("picoDebug") {
            withSourcesJar()
            withJavadocJar()
        }
        singleVariant("picoRelease") {
            withSourcesJar()
            withJavadocJar()
        }
    }

    tasks.matching { it.name.contains("CMake") || it.name.contains("externalNativeBuild") }.all { task ->
        def taskPrefix = ""
        if (project.path != ":") {
            taskPrefix = project.path + ":"
        }

        // Disable the externalNativeBuild* and *CMake* tasks as they would cause build failures since
        // the cmake build files are only setup for editing support.
        gradle.startParameter.excludedTaskNames += taskPrefix + task.name
    }
}

dependencies {
    compileOnly libraries.godotAndroidLib

    implementation "androidx.fragment:fragment:$versions.fragmentVersion"
    implementation "androidx.core:core-splashscreen:$versions.splashscreenVersion"
}

task cleanAssets(type: Delete) {
    // Delete the 'addons' directory in the 'assets' folder
    delete("src/main/assets/addons")
}

task cleanCxx(type: Delete) {
    delete(".cxx")
}

task copyDebugAARToAddons(type: Copy) {
    from 'build/outputs/aar'
    include 'godotopenxr-*-debug.aar'
    into '../demo/addons/godotopenxrvendors/.bin/android/debug'
}

task copyReleaseAARToAddons(type: Copy) {
    from 'build/outputs/aar'
    include 'godotopenxr-*-release.aar'
    into '../demo/addons/godotopenxrvendors/.bin/android/release'
}

task copyGdExtensionConfigToAssets(type: Copy) {
    description 'Copy plugin.gdextension from the addons directory to the assets directory'

    from '../demo/addons/godotopenxrvendors/'
    include 'plugin.gdextension'
    into 'src/main/assets/addons/godotopenxrvendors/'
}

preBuild.dependsOn(copyGdExtensionConfigToAssets)
assemble.dependsOn(copyGdExtensionConfigToAssets)
preBuild.dependsOn(copyKhronosOpenxrLoader)
assemble.dependsOn(copyKhronosOpenxrLoader)
assemble.finalizedBy(copyDebugAARToAddons)
assemble.finalizedBy(copyReleaseAARToAddons)
clean.dependsOn(cleanAssets)
clean.dependsOn(cleanCxx)
clean.dependsOn(cleanKhronosOpenxrLoader)
