import java.io.FileInputStream
import java.util.Properties

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

android {
    namespace = "ir.hakemsho.hakemsho"
    // flutter_secure_storage نیازمند compileSdk 36 است.
    compileSdk = maxOf(36, flutter.compileSdkVersion)
    // NDK لازم نیست: path_provider_android به نسخه‌ی پیش‌از‑jni پین شده.
    // 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 = "com.approagency.hakemirani"
        // You can update the following values to match your application needs.
        // For more information, see: https://flutter.dev/to/review-gradle-config.
        // flutter_secure_storage حداقل SDK 23 را لازم دارد.
        minSdk = maxOf(23, flutter.minSdkVersion)
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
        // مقادیرِ پیش‌فرض تا buildِ بدونِ flavor (مثلِ `flutter run`) هم merge شود؛
        // flavorها این‌ها را override می‌کنند.
        manifestPlaceholders += mapOf(
            "marketApplicationId" to "com.farsitel.bazaar",
            "marketBindAddress" to "com.farsitel.bazaar.InAppBillingService.BIND",
            "marketPermission" to "com.farsitel.bazaar.permission.PAY_THROUGH_BAZAAR"
        )
    }

    // یک flavor برای هر فروشگاه؛ هر کدام placeholderهای manifestِ افزونه‌ی
    // پرداخت (myket_iap / flutter_poolakey) را پر می‌کند. STORE به‌صورتِ
    // BuildConfig هم تزریق می‌شود تا سمتِ Dart بدونِ dart-define بداند کدام
    // درگاه فعال است.
    flavorDimensions += "store"
    productFlavors {
        create("myket") {
            dimension = "store"
            manifestPlaceholders += mapOf(
                "marketApplicationId" to "ir.mservices.market",
                "marketBindAddress" to "ir.mservices.market.InAppBillingService.BIND",
                "marketPermission" to "ir.mservices.market.BILLING"
            )
            resValue("string", "iap_store", "myket")
        }
        create("bazaar") {
            dimension = "store"
            manifestPlaceholders += mapOf(
                "marketApplicationId" to "com.farsitel.bazaar",
                "marketBindAddress" to "com.farsitel.bazaar.InAppBillingService.BIND",
                "marketPermission" to "com.farsitel.bazaar.permission.PAY_THROUGH_BAZAAR"
            )
            resValue("string", "iap_store", "bazaar")
        }
    }

   val keystoreProperties = Properties()
    val keystorePropertiesFile = rootProject.file("key.properties")
    if (keystorePropertiesFile.exists()) {
        keystoreProperties.load(FileInputStream(keystorePropertiesFile))
    }

    signingConfigs {
        create("release") {
            keyAlias = keystoreProperties["keyAlias"] as String?
            keyPassword = keystoreProperties["keyPassword"] as String?
            storeFile = keystoreProperties["storeFile"]?.let { 
                file(it) 
            }
            storePassword = keystoreProperties["storePassword"] as String?
        }
    }
    buildTypes {
        release {
            // 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")
            getByName("release") {
            signingConfig = signingConfigs.getByName("release")
            isMinifyEnabled = true
            isShrinkResources = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
        }
    }
}

// هر دو افزونه‌ی پرداخت کلاسِ AIDLِ یکسانِ
// `com.android.vending.billing.IInAppBillingService` را در AARِ خود دارند؛ چون
// هر دو پکیجِ Dart همیشه در classpath هستند، هر flavor هر دو AAR را می‌کشد و
// «Duplicate class» می‌دهد. راه‌حل: از هر flavor فقط کتابخانه‌ی فروشگاهِ خودش را
// نگه می‌داریم و کتابخانه‌ی فروشگاهِ دیگر را حذف می‌کنیم (در آن flavor صدا زده
// نمی‌شود).
configurations.configureEach {
    if (name.startsWith("myket")) {
        exclude(group = "com.github.cafebazaar.Poolakey")
    }
    if (name.startsWith("bazaar")) {
        exclude(group = "com.github.myketstore")
    }
}

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