apply plugin: 'com.android.application'

def jsEngine = "V8"
if (project.hasProperty("jsEngine")) {
    jsEngine = project.property("jsEngine")
}

def graphics_api = "OpenGL"
// min 24 for Vulkan
def platformVersion = 25

def unity_build = "false"
if (project.hasProperty("UNITY_BUILD")) {
    unity_build = project.property("UNITY_BUILD")
}
def arcore_libpath = "${buildDir}/arcore-native"

configurations { natives }

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.android.babylonnative.playground"
        minSdkVersion "${platformVersion}"
        targetSdkVersion 29
        ndkVersion "23.1.7779620"
        if (project.hasProperty("NDK_VERSION")) {
            def NDKVersion = project.property("NDK_VERSION")
            ndkVersion  "${NDK_VERSION}"
        }
    }

    packagingOptions {
        jniLibs {
            pickFirsts += ['lib/*/libv8android.so', 'lib/*/libjsc.so', 'lib/*/libBabylonNativeJNI.so', 'lib/*/libc++_shared.so']
        }
    }

    buildTypes {
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled true
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            debuggable true
        }
    }

    namespace 'com.android.babylonnative.playground'
}

repositories {
    // Add flatDir repository
    flatDir {
        dirs '../BabylonNative/build/outputs/aar'
    }
}

dependencies {
    implementation project(':BabylonNative')
    natives 'com.google.ar:core:1.14.0'

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
}

// Extracts the shared libraries from aars in the natives configuration.
// This is done so that NDK builds can access these libraries.
task extractNativeLibraries() {
    // Always extract, this insures the native libs are updated if the version changes.
    outputs.upToDateWhen { false }
    doFirst {
        configurations.natives.files.each { f ->
            copy {
                from zipTree(f)
                into arcore_libpath
                include "jni/**/*"
            }
        }
    }
}

tasks.configureEach { task ->
    if ((task.name.contains("buildCMake") || task.name.contains("configureCMake")) && !task.name.contains("Clean")) {
        task.dependsOn(extractNativeLibraries)
    }
}

preBuild.dependsOn ':BabylonNative:assembleRelease'

task copyFiles {
    copy
    {
        from '../../../node_modules/babylonjs'
        include "babylon.max.js"
        into 'src/main/assets/Scripts'
    }
    copy
    {
        from '../../../node_modules/babylonjs-loaders'
        include "babylonjs.loaders.js"
        into 'src/main/assets/Scripts'
    }
    copy
    {
        from '../../../node_modules/babylonjs-materials'
        include "babylonjs.materials.js"
        into 'src/main/assets/Scripts'
    }
    copy
    {
        from '../../../node_modules/babylonjs-gui'
        include "babylon.gui.js"
        into 'src/main/assets/Scripts'
    }
    copy
    {
        from '../../../Dependencies'
        include "*.js"
        into 'src/main/assets/Scripts'
    }
    copy
    {
        from '../../Scripts'
        include "*.js"
        into 'src/main/assets/Scripts'
    }
}

preBuild.dependsOn(copyFiles)

