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")
}
// 生成时间戳（格式 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.pro.app"
    compileSdk = 36

    defaultConfig {
        applicationId = "com.all.pdfreader.pro.app"
        minSdk = 24
        targetSdk = 36
        versionCode = 2
        versionName = "1.0.2"

        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
    }
}

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)
}