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

android {
    namespace 'com.meta.w4.godot.toolkit'
    compileSdk versions.compileSdk
    ndkVersion versions.ndkVersion

    base {
        archivesName = "godot_meta_toolkit"
    }

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

        ndk {
            //noinspection ChromeOsAbiSupport
            abiFilters "arm64-v8a"
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }

    sourceSets {
        main.jniLibs.srcDirs += ['../thirdparty/ovr_platform_sdk/Android/libs']

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

    packagingOptions {
        doNotStrip '**/*.so'
    }

    compileOptions {
        sourceCompatibility versions.javaVersion
        targetCompatibility versions.javaVersion
    }
    kotlinOptions {
        jvmTarget = versions.javaVersion
    }

    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
}

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 'godot_meta_toolkit-debug.aar'
    into '../demo/addons/godot_meta_toolkit/.bin/android/debug'
}

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

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

    from '../demo/addons/godot_meta_toolkit/'
    include 'toolkit.gdextension'
    into 'src/main/assets/addons/godot_meta_toolkit/'
}

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