import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.time.ZoneId

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.huawei.agconnect")
}


android {
    namespace = "com.atmob.keyboard"
    compileSdk = rootProject.extra["compileSdkVersion"] as Int
    ndkVersion = rootProject.extra["ndkVersion"] as String

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

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

    defaultConfig {

        applicationId = rootProject.extra["applicationId"] as String

        minSdk = rootProject.extra["minSdkVersion"] as Int
        targetSdk = rootProject.extra["targetSdkVersion"] as Int
        versionCode = flutter.versionCode
        versionName = flutter.versionName

        ndk {
            abiFilters.add("arm64-v8a")
        }

        manifestPlaceholders.putAll(
            mapOf(
                "JPUSH_PKGNAME" to applicationId!!,
                "JPUSH_APPKEY" to "e4c8b538fa1046eccce756de",
                "JPUSH_CHANNEL" to "developer-default",
                "XIAOMI_APPKEY" to "5652041165266",
                "XIAOMI_APPID" to "2882303761520411266",
                "VIVO_APPKEY" to "37b75015089824508ddc711e385a3df3",
                "VIVO_APPID" to "105899223",
                "OPPO_APPKEY" to "OP-2cc9d48944b440ac9480292cf80bbac5",
                "OPPO_APPID" to "OP-33224191",
                "OPPO_APPSECRET" to "OP-989965607b044943b8b1b610385cf404",
                "HONOR_APPID" to "104498449",
            )
        )
    }
    signingConfigs {
        create("keyboard") {
            storeFile = file("keystore/zhuiai.jks")
            storePassword = "zhuiai888"
            keyAlias = "zhuiai"
            keyPassword = "zhuiai888"
        }
    }
    buildTypes {
        getByName("debug") {
            // 允许debug
            isDebuggable = true
            // 是否开启混淆
//            isMinifyEnabled = true
//            proguardFiles(
//                getDefaultProguardFile("proguard-android-optimize.txt"),
//                "proguard-rules.pro"
//            )
            // Note: The debug build type must have the same signing config as the main build type.
            signingConfig = signingConfigs.getByName("keyboard")
        }
        getByName("release") {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            isMinifyEnabled = true
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
            signingConfig = signingConfigs.getByName("keyboard")
        }
    }

    applicationVariants.all {
        val variant = this
        variant.outputs.map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl }
            .forEach { output ->
                val formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm")
                val date = LocalDateTime.now(ZoneId.of("Asia/Shanghai")).format(formatter)

                val outputFileName =
                    "${variant.applicationId}-v${variant.versionName}-${variant.baseName}-${date}.apk"
                output.outputFileName = outputFileName
            }
    }

    dependencies {

        implementation("cn.jiguang.sdk.plugin:xiaomi:5.7.0")

        implementation("cn.jiguang.sdk.plugin:vivo:5.7.0")

        implementation("cn.jiguang.sdk.plugin:oppo:5.7.0")

        implementation("cn.jiguang.sdk.plugin:honor:5.7.0")

        implementation("com.google.code.gson:gson:2.6.2")
        implementation("commons-codec:commons-codec:1.6")
        implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
        implementation("androidx.annotation:annotation:1.1.0")

        implementation ("com.huawei.hms:push:6.12.0.300")
        implementation ("cn.jiguang.sdk.plugin:huawei:5.7.0")
    }


}

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