import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    alias(libVersions.plugins.android.application)
    alias(libVersions.plugins.kotlin.android)
    id 'kotlin-kapt'
    id 'kotlin-parcelize'
    id("org.jetbrains.kotlin.plugin.serialization") version libVersions.versions.kotlin
}

android {
    compileSdk 34

    defaultConfig {
        applicationId "nl.eduvpn.app"
        minSdkVersion 21
        targetSdkVersion 34
        versionCode 31
        versionName "3.3.4"
        ndkVersion "26.1.10909125"

        vectorDrawables.useSupportLibrary = true

        buildConfigField "String", "ORGANIZATION_LIST_BASE_URL", "\"https://disco.eduvpn.org/v2/\""
        buildConfigField "String", "SIGNATURE_URL_POSTFIX", "\".minisig\""
        buildConfigField "String[]", "MINISIGN_SIGNATURE_VALIDATION_PUBLIC_KEY", // If you want to add more, follow this guide (list version): https://stackoverflow.com/a/42130853/1395437
                "{" +
                        "\"RWRtBSX1alxyGX+Xn3LuZnWUT0w//B6EmTJvgaAxBMYzlQeI+jdrO6KF\"," + // fkooman@deic.dk, kolla@uninett.no
                        "\"RWQKqtqvd0R7rUDp0rWzbtYPA3towPWcLDCl7eY9pBMMI/ohCmrS0WiM\"" + // RoSp
                        "}"
        buildConfigField "boolean", "API_DISCOVERY_ENABLED", "true" // If false, you can only enter server URLs manually.
        buildConfigField "String", "HELP_URI", "\"https://www.eduvpn.org/faq.html\""

        // API
        buildConfigField "String", "OAUTH_CLIENT_ID", "\"org.eduvpn.app.android\""
        buildConfigField "String", "OAUTH_REDIRECT_URI", "\"org.eduvpn.app:/api/callback\""
        buildConfigField "String", "OAUTH_SCOPE", "\"config\""

        manifestPlaceholders = [
                'redirectScheme': 'org.eduvpn.app'
        ]
        missingDimensionStrategy 'implementation', 'ui'  // Skeleton is no option for us because we need the log activity
        missingDimensionStrategy 'ovpnimpl', 'ovpn2' // Only allow OpenVPN 2.x

        // This excludes some pretty old ABIs such armeabi or mips. LibSodium-JNI still includes binaries for these, which
        // leads to some devices selecting these as the app ABI, but the OpenVPN library did not include a binary for these,
        // thus resulting in a crash.
        ndk {
            abiFilters 'arm64-v8a', 'x86', 'x86_64', 'armeabi-v7a'
            debugSymbolLevel 'full'
        }
        bundle {
            abi { enableSplit = false }
        }
        buildFeatures {
            dataBinding = true
        }
        packagingOptions {
            jniLibs {
                useLegacyPackaging = true
            }
        }


        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        // The following argument makes the Android Test Orchestrator run its
        // "pm clear" command after each test invocation. This command ensures
        // that the app's state is completely cleared between tests.
        testInstrumentationRunnerArguments clearPackageData: 'true'

        testOptions {
            execution 'ANDROIDX_TEST_ORCHESTRATOR'
        }

    }

    buildTypes {
        debug {
            minifyEnabled false
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    flavorDimensions = ["product"]

    productFlavors {
        basic {
            dimension "product"
            isDefault = true
        }
        dev {
            dimension "product"
            applicationId "nl.eduvpn.app.dev"
        }
        gov {
            dimension "product"
            applicationId "org.govvpn.app"
            // API discovery is disabled, and only custom URLs can be entered.
            buildConfigField "boolean", "API_DISCOVERY_ENABLED", "false"
            buildConfigField "String", "OAUTH_CLIENT_ID", "\"org.govvpn.app.android\""
            buildConfigField "String", "OAUTH_REDIRECT_URI", "\"org.govvpn.app:/api/callback\""
            buildConfigField "String", "HELP_URI", "\"https://govroam.nl/govvpn\""

            manifestPlaceholders = [
                    'redirectScheme': 'org.govvpn.app.android'
            ]
        }
        home {
            dimension "product"
            applicationId "org.letsconnect_vpn.app"
            // API discovery is disabled, and only custom URLs can be entered.
            buildConfigField "boolean", "API_DISCOVERY_ENABLED", "false"
            buildConfigField "String", "OAUTH_CLIENT_ID", "\"org.letsconnect-vpn.app.android\""
            buildConfigField "String", "OAUTH_REDIRECT_URI", "\"org.letsconnect-vpn.app:/api/callback\""

            manifestPlaceholders = [
                    'redirectScheme': 'org.letsconnect-vpn.app'
            ]
        }
    }


    compileOptions {
        // Support Java 8+ on sdk < 24, also necessary for WireGuard
        coreLibraryDesugaringEnabled true

        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17
    }

    kapt {
        javacOptions {
            // Increase the max count of errors from annotation processors.
            // Default is 100.
            // See: https://github.com/google/dagger/issues/306#issuecomment-405525757
            option("-Xmaxerrs", 2000)
            // This will fail the build on warnings which are run in a build phase executed by kapt.
            option("-Werror")
        }
    }
    packagingOptions {
        resources {
            excludes += ['DebugProbesKt.bin']
        }
    }

    namespace 'nl.eduvpn.app'
    lint {
        disable 'GradleDependency', 'UnsafeNativeCodeLocation', 'RtlSymmetry', 'RtlHardcoded', 'MissingTranslation'
    }
}

dependencies {
    // OpenVPN library
    implementation project(path: ':ics-openvpn-main')
    // eduVPN common library written in Go, stores all data and does the communication with the servers
    implementation project(path: ':common')
    // WireGuard VPN library
    implementation project(path: ':wireguard')

    // Please try to stay in sync with the versions used in the ics-openvpn module
    implementation(eduvpnVersions.android.appcompat)
    implementation(eduvpnVersions.android.activity.ktx)
    implementation(eduvpnVersions.android.fragment.ktx)
    implementation(eduvpnVersions.android.recyclerview)
    implementation(eduvpnVersions.android.material)
    implementation(eduvpnVersions.android.browser)
    implementation(eduvpnVersions.constraint.layout)
    implementation(eduvpnVersions.security.crypto)
    implementation(eduvpnVersions.lifecycle.extensions)
    implementation(eduvpnVersions.lifecycle.viewmodel.ktx)
    implementation(eduvpnVersions.lifecycle.livedata.ktx)
    // Dependency injection
    implementation(eduvpnVersions.dagger)
    kapt(eduvpnVersions.dagger.compiler)
    // Image loader
    implementation(eduvpnVersions.picasso)
    // Kotlin coroutines
    implementation(eduvpnVersions.coroutines)
    implementation(eduvpnVersions.serialization)

    // All testing related libraries
    testImplementation(eduvpnVersions.junit)
    androidTestImplementation(eduvpnVersions.androidx.test.runner)
    androidTestImplementation(eduvpnVersions.androidx.test.rules)
    androidTestImplementation(eduvpnVersions.androidx.test.ext.junit)
    androidTestImplementation(eduvpnVersions.espresso)
    androidTestImplementation(eduvpnVersions.uiautomator)
    coreLibraryDesugaring(eduvpnVersions.desugar.jdk.libs)
    androidTestUtil(eduvpnVersions.androidx.test.orchestrator)
}

// This will fail the build if there is a Kotlin compiler warning.
tasks.withType(KotlinCompile).forEach {
    kotlinOptions {
        freeCompilerArgs = ["-Werror", "-opt-in=kotlin.RequiresOptIn"]
    }
}
