import org.gradle.api.tasks.Sync

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("org.jetbrains.kotlin.plugin.compose")
}

fun releaseCredential(name: String): String? =
    providers.gradleProperty(name).orElse(providers.environmentVariable(name)).orNull

val releaseStoreFile = releaseCredential("HT_ANDROID_KEYSTORE_FILE")
val releaseStorePassword = releaseCredential("HT_ANDROID_KEYSTORE_PASSWORD")
val releaseKeyAlias = releaseCredential("HT_ANDROID_KEY_ALIAS")
val releaseKeyPassword = releaseCredential("HT_ANDROID_KEY_PASSWORD")
val releaseSigningConfigured = listOf(
    releaseStoreFile,
    releaseStorePassword,
    releaseKeyAlias,
    releaseKeyPassword,
).all { !it.isNullOrBlank() }

val legalAssetsDir = layout.buildDirectory.dir("generated/legal-assets")
val prepareLegalAssets by tasks.registering(Sync::class) {
    from(rootProject.projectDir.resolve("LICENSES")) {
        include("GPL-3.0-only.txt", "LGPL-2.1-only.txt")
        into("legal")
    }
    from(rootProject.projectDir.resolve("THIRD_PARTY_NOTICES.md")) {
        into("legal")
    }
    into(legalAssetsDir)
}

android {
    namespace = "net.hivetechnology.o20010_06"
    compileSdk = 36
    ndkVersion = "27.3.13750724"

    defaultConfig {
        applicationId = "net.hivetechnology.o20010_06"
        minSdk = 29
        targetSdk = 36
        versionCode = 28
        versionName = "0.1.6"

        buildConfigField(
            "String",
            "GPL_SOURCE_URL",
            "\"https://portal.hivetechnology.net/open-source/ht-portal-vpn/0.1.6-android-source.tar.gz\"",
        )

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                arguments += listOf(
                    "-DHTP_BUILD_TESTS=OFF",
                    "-DHTP_BUILD_N3N=ON",
                    "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
                )
                cppFlags += "-std=c++17"
            }
        }
        ndk {
            abiFilters += listOf("arm64-v8a", "armeabi-v7a", "x86_64")
        }
    }

    signingConfigs {
        create("release") {
            if (releaseSigningConfigured) {
                storeFile = file(requireNotNull(releaseStoreFile))
                storePassword = releaseStorePassword
                keyAlias = releaseKeyAlias
                keyPassword = releaseKeyPassword
            }
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = true
            signingConfig = signingConfigs.getByName("release")
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
    externalNativeBuild {
        cmake {
            path = file("src/main/cpp/CMakeLists.txt")
            version = "3.22.1"
        }
    }
    buildFeatures {
        compose = true
        buildConfig = true
    }
    sourceSets.getByName("main").assets.srcDir(legalAssetsDir)
    packaging {
        jniLibs.useLegacyPackaging = false
        resources.excludes += setOf("META-INF/AL2.0", "META-INF/LGPL2.1")
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions.jvmTarget = "17"
}

tasks.configureEach {
    if (name == "bundleRelease" || name == "assembleRelease") {
        doFirst {
            check(releaseSigningConfigured) {
                "Android release signing is not configured. Set HT_ANDROID_KEYSTORE_FILE, " +
                    "HT_ANDROID_KEYSTORE_PASSWORD, HT_ANDROID_KEY_ALIAS and HT_ANDROID_KEY_PASSWORD."
            }
        }
    }
}

tasks.named("preBuild").configure {
    dependsOn(prepareLegalAssets)
}

dependencies {
    val composeBom = platform("androidx.compose:compose-bom:2024.12.01")
    implementation(composeBom)
    androidTestImplementation(composeBom)

    implementation("androidx.activity:activity-compose:1.10.0")
    implementation("androidx.browser:browser:1.10.0")
    implementation("androidx.core:core-ktx:1.15.0")
    implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.7")
    implementation("androidx.lifecycle:lifecycle-service:2.8.7")
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.7")
    implementation("androidx.compose.material3:material3")
    implementation("androidx.compose.material:material-icons-extended")
    implementation("androidx.compose.ui:ui")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation("com.squareup.okhttp3:okhttp:4.12.0")

    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.2.1")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
    androidTestImplementation("androidx.compose.ui:ui-test-junit4")
    debugImplementation("androidx.compose.ui:ui-tooling")
    debugImplementation("androidx.compose.ui:ui-test-manifest")
}
