plugins {
    id 'com.android.application'
}

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

configurations { natives }

android {
    namespace 'com.babylonnative.unittests'
    compileSdk 33
    ndkVersion = "21.4.7075529"

    defaultConfig {
        applicationId "com.babylonnative.unittests"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
                arguments (
                    "-DANDROID_STL=c++_shared",
                    "-DNAPI_JAVASCRIPT_ENGINE=${jsEngine}",
                    "-DJSRUNTIMEHOST_CORE_APPRUNTIME_V8_INSPECTOR=ON",
                    "-DBABYLON_NATIVE_BUILD_APPS=ON"
                )
            }
        }

        if (project.hasProperty("abiFilters")) {
            ndk {
                abiFilters project.getProperty("abiFilters")
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            buildStagingDirectory '../../../../Build/Android'
            version '3.22.1+'
        }
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation 'com.google.ar:core:1.16.0'
    natives 'com.google.ar:core:1.16.0'
    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation group: 'org.java-websocket', name: 'Java-WebSocket', version: '1.5.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

task copyScripts {
    doLast {
        // run copy at execution phase because npm command is not done at configuration phase
        copy
        {
            from "../../../node_modules/chai"
            include "chai.js"
            into 'src/main/assets/Scripts'
        }
        copy
        {
            from "../../../node_modules/mocha"
            include "mocha.js"
            into 'src/main/assets/Scripts'
        }
        copy
        {
            from "../../../node_modules/babylonjs"
            include "babylon.max.js"
            into 'src/main/assets/Scripts'
        }
        copy
        {
            from "../../../node_modules/babylonjs-materials"
            include "babylonjs.materials.js"
            into 'src/main/assets/Scripts'
        }
    }
}

// Run copyScripts task after CMake external build
// And make sure merging assets into output is performed after the scripts copy
tasks.configureEach { task ->
    if (task.name == 'mergeDebugNativeLibs') {
        task.finalizedBy(copyScripts)
    }
    if (task.name == 'mergeDebugAssets') {
        task.dependsOn(copyScripts)
    }
}
