import groovy.xml.XmlParser
import org.gradle.kotlin.dsl.implementation
import java.text.SimpleDateFormat
import java.util.Date

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.devtools.ksp)
    id("kotlin-parcelize")
    id("com.google.gms.google-services")
    id("com.google.firebase.crashlytics")
}
// 生成时间戳（格式 MMddHHmm）
val timestamp: String = SimpleDateFormat("MMddHHmm").format(Date())

fun getAppNameFromStringsFile(): String {
    val stringsFile = file("src/main/res/values/strings.xml")
    if (!stringsFile.exists()) {
        return "UnknownApp"
    }

    val xmlParser = XmlParser()
    val rootNode = xmlParser.parse(stringsFile)

    return rootNode.children()
        .filterIsInstance<groovy.util.Node>()
        .find { it.attribute("name") == "app_name" }
        ?.text() ?: "UnknownApp"
}
android {
    namespace = "com.all.pdfreader.pdf.reader"
    compileSdk = 36

    defaultConfig {
        applicationId = "com.all.pdfreader.pdf.reader"
        minSdk = 24
        targetSdk = 36
        versionCode = 4
        versionName = "1.0.4"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

        setProperty("archivesBaseName", "${getAppNameFromStringsFile()}-$versionName($versionCode)-${timestamp}")
    }

    buildTypes {
        release {
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = "11"
    }
    buildFeatures {
        viewBinding = true
        buildConfig = true
    }

    // 有 flavorDimensions 才能使用 productFlavors
    flavorDimensions += "market"

    productFlavors {
        create("google") {
            dimension = "market"
            buildConfigField("String", "CHANNEL", "\"google\"")
        }
        create("samsung") {
            dimension = "market"
            buildConfigField("String", "CHANNEL", "\"samsung\"")
        }
        create("xiaomi") {
            dimension = "market"
            buildConfigField("String", "CHANNEL", "\"xiaomi\"")
        }
    }
}

dependencies {
    implementation(libs.androidx.fragment.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.androidx.core.ktx)
    implementation(libs.androidx.lifecycle.runtime.ktx)
    implementation(libs.androidx.swiperefreshlayout)
    implementation(libs.androidx.recyclerview)
    implementation(libs.protolite.well.known.types)
    implementation(libs.material)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    implementation(libs.immersionbar)
    implementation(libs.immersionbar.ktx)
    implementation(libs.androidx.room.runtime)
    ksp(libs.androidx.room.compiler)
    implementation(libs.androidx.room.ktx)
    implementation(libs.glide)
    implementation(libs.androidpdfviewer)
    implementation(libs.pdfbox.android)
//    implementation(libs.jp2forandroid)
    implementation(libs.flexbox)
    implementation(libs.pictureselector)
    implementation(libs.ucrop)
    implementation(libs.compress)
    // Import the Firebase BoM
    implementation(platform("com.google.firebase:firebase-bom:34.6.0"))
    implementation("com.google.firebase:firebase-crashlytics-ndk")
    implementation("com.google.firebase:firebase-analytics")
}