layout.buildDirectory = layout.projectDirectory.dir("build_android")

subprojects {
    apply plugin: "com.android.application"

    dependencies {
        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.4'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
    }

    project.layout.buildDirectory = rootProject.layout.buildDirectory.dir(project.name)

    ext {
        assets = []
        thirdPartyAssets = []
    }

    // copyTask copies project specific assets into a build asset folder,
    // so they can be packaged into the APK.
    task copyTask {
        // doLast to make sure we get subproject assets value, and
        // execute in execution phase.
        doLast {
            project.assets.each {
                def fromFile = "${rootProject.projectDir}/assets/${it}"
                def intoFile = layout.buildDirectory.dir("assets/${it}")
                copy {
                    from fromFile
                    into intoFile
                }
            }
            // Copy the rest of the assets - generated shaders.
            project.assets.each {
                def fromFile = "${rootProject.projectDir}/build_android/assets/${it}"
                def intoFile = layout.buildDirectory.dir("assets/${it}")
                copy {
                    from fromFile
                    into intoFile
                }
            }
            project.thirdPartyAssets.each {
                def fromFile = "${rootProject.projectDir}/third_party/assets/${it}"
                def intoFile = layout.buildDirectory.dir("assets/${it}")
                copy {
                    from fromFile
                    into intoFile
                }
            }
        }
    }

    android {
        namespace 'com.google.bigwheels'
        compileSdk 32

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        buildFeatures {
            prefab true
        }
        externalNativeBuild {
            cmake {
                path file("${rootProject.projectDir}/CMakeLists.txt")
                version '3.22.1+'
                buildStagingDirectory "${rootProject.projectDir}/.cxx/${project.name}"
            }
        }

        buildTypes {
            release {
                minifyEnabled false
            }
            debug {
              applicationIdSuffix '.debug'
              debuggable true
            }
        }

        defaultConfig {
            applicationId "com.google.bigwheels.project_${project.name}"
            minSdk 29
            targetSdk 32
            versionCode 1
            versionName "1.0"

            manifestPlaceholders = [
                sampleLibraryName : "vk_${project.name}",
                appLabel : "vk_${project.name}"
            ]


            if (project.findProperty('ENABLE_32BIT_ARM')) {
              ndk.abiFilters = ['armeabi-v7a', 'arm64-v8a']
            } else {
              ndk.abiFilters = ['arm64-v8a']
            }

            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            externalNativeBuild {
                cmake {
                    if (project.name.contains("_xr") || project.name.contains("graphics_pipeline")) {
                        arguments '-DPPX_ANDROID=TRUE',
                                  '-DPPX_BUILD_TESTS=FALSE',
                                  '-DBUILD_TESTS=FALSE', // Required for OpenXR
                                  '-DPPX_BUILD_XR=TRUE',
                                  '-DBUILD_LOADER=TRUE',
                                  '-DBUILD_ALL_EXTENSIONS=TRUE'
                    }
                    else {
                        arguments '-DPPX_ANDROID=TRUE',
                                  '-DPPX_BUILD_TESTS=FALSE'
                    }
                    if (project.name.contains("_xr") || project.name.contains("graphics_pipeline")) {
                        cppFlags '-std=c++17',
                                 '-DXR_USE_PLATFORM_ANDROID'
                    }
                    else {
                        cppFlags '-std=c++17'
                    }

                    if (project.hasProperty('DXC_PATH')) {
                      arguments "-DDXC_PATH=$DXC_PATH"
                    }
                    targets "vk_${project.name}"
                }
            }
            multiDexEnabled true
            it.buildConfigField "String", "sample_library_name", '"vk_' + project.name + '"'
            sourceSets.main {
                java.srcDirs = [ "${rootProject.projectDir}/src/android" ]
                if (project.name.contains("_xr") || project.name.contains("graphics_pipeline")) {
                    manifest.srcFile "${rootProject.projectDir}/src/android/AndroidManifest.XR.xml"
                } else {
                    manifest.srcFile "${rootProject.projectDir}/src/android/AndroidManifest.xml"
                }
            }
        }

        sourceSets.main.assets.srcDirs += layout.buildDirectory.dir("assets")

        applicationVariants.all { variant ->
            // copyTask copies generated shaders, so it must be run after
            // they are built. mustRunAfter only makes sure copyTask executes
            // after the task if that task is already supposed to execute,
            // but does not force it to be executed.
            tasks["copyTask"].mustRunAfter("externalNativeBuild${variant.name.capitalize()}")
            tasks["generate${variant.name.capitalize()}Assets"]
              .dependsOn(copyTask)
        }
    }
}

tasks.register("clean", Delete) {
    delete layout.buildDirectory
}