import java.util.Properties

val localPropertiesFile = rootProject.file("key.properties")
val localProperties = Properties()

if (localPropertiesFile.exists()) {
    localPropertiesFile.inputStream().use { inputStream ->
        localProperties.load(inputStream)
    }
}

plugins {
    id("com.android.application")
    id("kotlin-android")
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    id("dev.flutter.flutter-gradle-plugin")
    id("com.google.gms.google-services")
}

android {
    namespace = "jigsort.solitaire.jigsaw.match.games"
    // compileSdk = flutter.compileSdkVersion
    // ndkVersion = flutter.ndkVersion
    compileSdk = 36
    ndkVersion = "27.0.12077973"
    
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11.toString()
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "jigsort.solitaire.jigsaw.match.games"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        // minSdk = flutter.minSdkVersion
        minSdk = 23
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName

        // ✅ 建议加上这一行，确保 5 个广告 SDK 不会撑破方法数限制
        multiDexEnabled = true
    }

    signingConfigs {
        create("release") { // 使用 create 方法来定义签名配置
            storeFile = file(localProperties.getProperty("storeFile"))
            storePassword = localProperties.getProperty("storePassword")
            keyAlias = localProperties.getProperty("keyAlias")
            keyPassword = localProperties.getProperty("keyPassword")
        }
    }

    buildTypes {
        release {
            // ✅ 启用 R8 代码混淆和收缩（Kotlin DSL: 使用 isMinifyEnabled = true）
            isMinifyEnabled = true
            // ✅ 启用资源收缩（Kotlin DSL: 使用 isShrinkResources = true）
            isShrinkResources = true

            // ✅ 指定 ProGuard 规则文件（Kotlin DSL: 使用 proguardFiles(...) 函数）
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
            
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            // signingConfig = signingConfigs.getByName("debug")
            signingConfig = signingConfigs.getByName("release")
        }
    }
}

flutter {
    source = "../.."
}

dependencies {
    // ✅ MultiDex 支持库
    implementation("androidx.multidex:multidex:2.0.1")

    // AppLovin MAX 核心适配器 (建议使用最新版本或与你的 AppLovin SDK 匹配的版本)
    // 注意：Kotlin DSL 使用 implementation("...") 括号加双引号
    
    // 1. AdMob (Google) 适配器
    implementation("com.applovin.mediation:google-adapter:23.3.0.0")
    
    // 2. Meta (Facebook) 适配器
    implementation("com.applovin.mediation:facebook-adapter:6.17.0.0")

    // 3. UnityAds
    implementation("com.applovin.mediation:unityads-adapter:4.12.2.0")

    // 4. Moloco
    implementation("com.applovin.mediation:moloco-adapter:3.0.0.0")

    // 5. Mintegral
    implementation("com.applovin.mediation:mintegral-adapter:16.8.51.0")
}
