plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.plugin.serialization' version '2.1.21'

}

task downloadAndUnzipNativeLibs {
    group = 'Pre-build'
    description = 'Downloads and unzips libsherpa-mnn-jni native library from CDN.'
    def nativeLibsUrl = 'https://meta.alicdn.com/data/mnn/libs/libsherpa-mnn-jni-16k.zip'
    def zipFileName = 'libsherpa-mnn-jni-16k.zip'
    def outputDir = file('src/main/jniLibs/arm64-v8a')
    def downloadedZip = new File(project.buildDir, zipFileName)
    def checkFile = new File(outputDir, 'libsherpa-mnn-jni.so')
    
    inputs.property('url', nativeLibsUrl)
    outputs.file(checkFile)
    
    doLast {
        println "-> Executing downloadAndUnzipNativeLibs task..."
        println "   Downloading from ${nativeLibsUrl}"
        
        // Create output directory if it doesn't exist
        outputDir.mkdirs()
        
        ant.get(src: nativeLibsUrl, dest: downloadedZip)

        if (!downloadedZip.exists()) {
            throw new GradleException("Download failed: ${downloadedZip} not found.")
        }
        println "   Download complete."
        println "   Unzipping ${downloadedZip.name} to ${outputDir}..."
        
        copy {
            from(zipTree(downloadedZip))
            into(outputDir)
        }
        println "   Unzip complete."
        downloadedZip.delete()
    }
    
    onlyIf {
        println "-> Checking if native libs exist... [Exists: ${checkFile.exists()}]"
        return !checkFile.exists()
    }
}

preBuild.dependsOn downloadAndUnzipNativeLibs

android {
    namespace 'com.alibaba.mnnllm.android'
    compileSdk 35
    ndkVersion "27.2.12479018"
    
    flavorDimensions "store"
    
    defaultConfig {
        applicationId "com.alibaba.mnnllm.android"
        minSdk 26
        targetSdk 35
        versionCode 731
        versionName "0.7.3.1"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags '-std=c++17'
                // Enable 16KB page size support for NDK r27
                arguments "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
            }
        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        kotlinOptions {
            jvmTarget = "1.8"
        }

        lintOptions {
            disable 'TimberArgCount', 'TimberArgTypes', 'TimberTagLength', 'BinaryOperationInTimber',
                    'LogNotTimber', 'StringFormatInTimber', 'ThrowableNotAtBeginning'
        }

        ndk {
            //noinspection ChromeOsAbiSupport
            abiFilters "arm64-v8a" // Include only arm64-v8a
        }

        signingConfigs {
            release {
                def keystoreFile = System.getenv("KEYSTORE_FILE")
                if (keystoreFile) {
                    storeFile file(keystoreFile)
                    storePassword System.getenv("KEYSTORE_PASSWORD")
                    keyAlias System.getenv("KEY_ALIAS")
                    keyPassword System.getenv("KEY_PASSWORD")
                }
            }
        }
        buildTypes {
            release {
                if (System.getenv("KEYSTORE_FILE")) {
                    signingConfig signingConfigs.release
                }
                applicationIdSuffix ".release"
            }
        }
    }
    
    productFlavors {
        standard {
            dimension "store"
            buildConfigField "boolean", "IS_GOOGLE_PLAY_BUILD", "false"
        }
        googleplay {
            dimension "store"
            buildConfigField "boolean", "IS_GOOGLE_PLAY_BUILD", "true"
            versionNameSuffix ".gp"
        }
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.22.1'
        }
    }
    buildFeatures {
        viewBinding true
        buildConfig true
    }
    sourceSets {
        main {
            res.srcDirs = ['src/main/res', 'src/main/res-icons']
        }
    }

    packagingOptions {
        resources.excludes.addAll([
                'META-INF/INDEX.LIST',
                'META-INF/io.netty.versions.properties',
                'META-INF/DEPENDENCIES',
                'META-INF/LICENSE',
                'META-INF/MANIFEST.MF'
        ])
    }

    testOptions {
        unitTests.all {
            jvmArgs '--add-opens', 'java.base/java.lang=ALL-UNNAMED'
        }
    }
}

dependencies {

//    https://developer.android.com/jetpack/androidx/releases/compose
    def jetpack_compose_verion = '1.7.8'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation 'androidx.appcompat:appcompat:1.7.0'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
    implementation 'com.github.techinessoverloaded:progress-dialog:1.5.1'
    implementation 'com.squareup.okhttp3:okhttp:4.12.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.google.code.gson:gson:2.10.1'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3'
    implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
    implementation 'com.github.ybq:Android-SpinKit:1.4.0'
    implementation 'com.nambimobile.widgets:expandable-fab:1.2.1'
    implementation 'com.github.squti:Android-Wave-Recorder:2.0.1'
    implementation 'androidx.preference:preference-ktx:1.2.1'
    
    // MNN TTS Framework
    implementation project(':mnn_tts')

    androidTestImplementation 'androidx.test.ext:junit:1.2.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
    implementation "io.noties.markwon:core:4.6.2"
    implementation "io.noties.markwon:ext-latex:4.6.2"
    implementation 'ru.noties:jlatexmath-android:0.2.0'
    implementation 'ru.noties:jlatexmath-android-font-cyrillic:0.2.0'
    implementation 'ru.noties:jlatexmath-android-font-greek:0.2.0'
    implementation "androidx.compose.foundation:foundation:${jetpack_compose_verion}"
    implementation "androidx.compose.material:material-icons-extended:${jetpack_compose_verion}"
    implementation "androidx.compose.ui:ui:${jetpack_compose_verion}"
    implementation "androidx.activity:activity-compose:1.10.1"
    implementation "androidx.compose.ui:ui-tooling-preview:$jetpack_compose_verion"
    debugImplementation "androidx.compose.ui:ui-tooling:$jetpack_compose_verion"
    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
    implementation "androidx.compose.material3:material3:1.3.1"

    def ktor_version = '3.1.3' // Ktor 版本
    // Koin 依赖
    implementation "io.insert-koin:koin-android:3.5.0"
    implementation "io.insert-koin:koin-core:3.5.0"

    // Ktor 相关依赖
    implementation "io.ktor:ktor-client-android:${ktor_version}"
    implementation "io.ktor:ktor-server-auth:${ktor_version}"
    implementation "io.ktor:ktor-server-call-logging:${ktor_version}"
    implementation "io.ktor:ktor-server-content-negotiation:${ktor_version}"
    implementation "io.ktor:ktor-server-cors:${ktor_version}"
    implementation "io.ktor:ktor-server-double-receive:${ktor_version}"
    implementation "io.ktor:ktor-server-netty:${ktor_version}"
    implementation "io.ktor:ktor-server-request-validation:${ktor_version}"
    implementation "io.ktor:ktor-server-sse:${ktor_version}"
    implementation "io.ktor:ktor-server-status-pages:${ktor_version}"
    implementation "io.ktor:ktor-serialization-kotlinx-json:${ktor_version}"
    implementation "io.ktor:ktor-utils:${ktor_version}"

    // Kotlin 协程相关依赖
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2"

    // Kotlin 序列化相关依赖
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.1"

    // DataStore 相关依赖
    implementation "androidx.datastore:datastore-preferences:1.0.0"

    // 日志相关依赖
    implementation "com.jakewharton.timber:timber:5.0.1"
//    implementation(name: 'android_device_names', ext: 'aar')
    implementation 'com.github.Juude:AndroidDeviceNames:0.0.1'
    testImplementation 'androidx.test:core:1.5.0'
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.robolectric:robolectric:4.12.1'
    testImplementation 'org.mockito:mockito-core:5.11.0'
    testImplementation 'org.mockito:mockito-inline:5.2.0'
    testImplementation 'org.mockito.kotlin:mockito-kotlin:5.2.0'
    testImplementation "io.mockk:mockk:1.13.10"
}