apply plugin: 'com.android.application'
apply from: project(':libaxmol').projectDir.toString() + "/../dsl/axmol.gradle"

// Resolve build profiles
def buildProfiles = AxmolUtils.resolveBuildProfiles(project)

android {
    def packageName = buildProfiles['packageName']
    def cmakeVer = buildProfiles['cmakeVer']
    def cmakeOptions = Eval.me(buildProfiles['cmakeOptions'])
    def minSdk = buildProfiles['minSdk']
    def targetSdk = buildProfiles['targetSdk']

    // Apply build profiles
    namespace = packageName
    compileSdk = targetSdk.toInteger()
    ndkVersion = buildProfiles['ndkVer']
    if(buildProfiles.containsKey('ndkPath')) {
        ndkPath = buildProfiles['ndkPath']
    }

    defaultConfig {
        applicationId packageName
        minSdkVersion minSdk
        targetSdkVersion targetSdk
        versionCode 1
        versionName "1.0"

        externalNativeBuild {
            cmake {
                arguments = []
                arguments.addAll(cmakeOptions)
                cppFlags "-frtti -fexceptions -fsigned-char"
            }
        }

        ndk {
            // noinspection ChromeOsAbiSupport
            abiFilters = __1K_ARCHS.split(':').collect{it as String}
        }
    }

    sourceSets.main {
        java.srcDir "src"
        res.srcDir "res"
        manifest.srcFile "AndroidManifest.xml"
        assets.srcDir "build/assets"
    }

    externalNativeBuild {
        cmake {
            version = cmakeVer
            path "../../CMakeLists.txt"
        }
    }

    signingConfigs {
       release {
            if (project.hasProperty("KEY_STORE_FILE")) {
                storeFile file(KEY_STORE_FILE)
                storePassword KEY_STORE_PASSWORD
                keyAlias KEY_ALIAS
                keyPassword KEY_PASSWORD
            }
        }
    }

    buildTypes {
        release {
            debuggable false
            jniDebuggable false
            minifyEnabled true
            shrinkResources = true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            proguardFile file(project(':libaxmol').projectDir.toString() + '/proguard-rules.pro')
            if (project.hasProperty("KEY_STORE_FILE")) {
                signingConfig signingConfigs.release
            }
        }

        debug {
            debuggable true
            jniDebuggable true
        }
    }

    aaptOptions {
       noCompress 'mp3','ogg','wav','mp4','ttf','ttc','otf'
    }

    buildFeatures {
        aidl = true
    }
}

android.applicationVariants.configureEach { variant ->
    def variantName = variant.name.capitalize()
    tasks.register("copy${variantName}ContentToAssets") {
        doFirst {
            delete "${projectDir}/build/assets"
        }
        doLast {
            copy {
                from "${projectDir}/../../Content"
                into "${projectDir}/build/assets"
                exclude "**/*.gz"
            }
            copy {
                from "${projectDir}/build/runtime/axslc"
                into "${projectDir}/build/assets/axslc"
            }
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation project(':libaxmol')
}

project.afterEvaluate {
    android.applicationVariants.configureEach { variant ->
        def variantName = variant.name.capitalize()
        def copyTask = tasks.named("copy${variantName}ContentToAssets")

        // !!!IMPORTANT: This copy task must depend on the native build task.
        // Without this dependency, the copy step may run before native libraries
        // are built, resulting in incomplete or outdated assets being packaged
        // into the APK. Do not remove or modify this dependency, as it ensures
        // the correct build order: native build -> copy assets -> merge assets -> AAB/APK.
        copyTask.configure {
            dependsOn(tasks.named("externalNativeBuild${variantName}"))
        }

        tasks.named("merge${variantName}Assets").configure {
            dependsOn(copyTask)
        }
    }
}
