import java.util.Properties

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

val sharedSigningPropertiesFile = rootProject.file("key.properties")
val sharedSigningProperties =
    Properties().also { properties ->
        check(sharedSigningPropertiesFile.isFile) {
            "Shared signing config missing: ${sharedSigningPropertiesFile.absolutePath}"
        }
        sharedSigningPropertiesFile.inputStream().use(properties::load)
    }

fun requiredSharedSigningProperty(name: String): String =
    sharedSigningProperties.getProperty(name)?.takeIf { it.isNotBlank() }
        ?: error("Shared signing property '$name' missing in ${sharedSigningPropertiesFile.absolutePath}")

val sharedSigningStoreFile =
    sharedSigningPropertiesFile.parentFile.resolve(requiredSharedSigningProperty("storeFile"))

check(sharedSigningStoreFile.isFile) {
    "Shared signing keystore missing: ${sharedSigningStoreFile.absolutePath}"
}

android {
    namespace = "com.gametime.app"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    defaultConfig {
        applicationId = "com.gametime.app"
        minSdk = flutter.minSdkVersion
        targetSdk = flutter.targetSdkVersion
        versionCode = flutter.versionCode
        versionName = flutter.versionName
    }

    signingConfigs {
        create("sharedLocal") {
            storeFile = sharedSigningStoreFile
            storePassword = requiredSharedSigningProperty("storePassword")
            keyAlias = requiredSharedSigningProperty("keyAlias")
            keyPassword = requiredSharedSigningProperty("keyPassword")
        }
    }

    buildTypes {
        release {
            signingConfig = signingConfigs.getByName("sharedLocal")
        }
    }
}

kotlin {
    compilerOptions {
        jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17
    }
}

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

dependencies {
    implementation("com.google.android.gms:play-services-wearable:19.0.0")
}
