plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'androidx.navigation.safeargs.kotlin'
}

configurations { natives }

android {
    ndkVersion '25.2.9519653'
    compileSdk 33

    defaultConfig {
        applicationId "com.vladd11.arshop"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++17", "-Wall"
                arguments "-DANDROID_STL=c++_static",
                        "-DARCORE_LIBPATH=${buildDir}/arcore-native/jni",
                        "-DARCORE_INCLUDE=${project.rootDir}/libraries/include",
                        "-DGLM_INCLUDE=${buildDir}/glm/glm",
                        "-DLUA_INCLUDE=${buildDir}/luasocket-master",
                        "-DLUA_BUILDDIR=${buildDir}/luasocket-master"
            }
        }
        ndk {
            abiFilters "arm64-v8a", "x86_64"
        }
    }

    aaptOptions {
        noCompress "tflite"
    }

    buildTypes {
        debug {
            externalNativeBuild {
                cmake {
                    cppFlags "-DGLDEBUG"
                }
            }
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.22.1'
        }
    }
}

dependencies {
    implementation 'com.google.ar:core:1.36.0'
    implementation 'com.google.android.gms:play-services-auth:20.3.0'
    natives 'com.google.ar:core:1.36.0'

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    /*implementation 'org.tensorflow:tensorflow-lite-task-vision-play-services:0.4.2'
    implementation 'com.google.android.gms:play-services-tflite-gpu:16.0.0'*/
}

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 "${buildDir}/arcore-native"
                include "jni/**/*"
            }
        }
    }
}

task downloadLibs(type: Download) {
    mkdir "${buildDir}/lua"
    src([
            "https://github.com/vladd11/luasocket/archive/refs/heads/${LUA_FILE}",
            "https://github.com/g-truc/glm/releases/download/0.9.9.8/${GLM_FILE}"
    ])
    dest buildDir
    overwrite false
}

task unzipLibs(type: Copy, dependsOn: downloadLibs) {
    from zipTree("${buildDir}/${GLM_FILE}")
    from zipTree("${buildDir}/${LUA_FILE}")
    into buildDir
}

tasks.whenTaskAdded {
    task ->
        {
            if (!(file("${buildDir}/glm").exists() && file("${buildDir}/luasocket-master").exists())) {
                task.dependsOn(unzipLibs)
            }

            if ((task.name.contains("external") || task.name.contains("CMake")) && !task.name.contains("Clean")) {
                task.dependsOn(extractNativeLibraries)
            }
        }
}
