buildscript {
    ext.kotlin_version = "1.9.22"
    ext.compose_version = "1.5.10"
    ext.accompanist_version = "0.34.0"

    repositories {
        google()
        mavenCentral()
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:8.1.4"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
        classpath("com.ncorti.ktfmt.gradle:plugin:0.17.0")
    }
}

repositories {
    google()
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}

apply plugin: 'kotlin-android'
apply plugin: 'com.android.application'
apply plugin: 'org.jetbrains.kotlin.plugin.serialization'
apply plugin: 'com.ncorti.ktfmt.gradle'

android {
    ndkVersion "23.1.7779620"
    compileSdkVersion 34
    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 34
        versionCode 204
        versionName "1.65.0-t220764331-g50bb25aca2d"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = "17"
    }

    kotlinOptions {
        jvmTarget = "17"
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion = "$compose_version"
    }
    flavorDimensions "version"
    namespace 'com.tailscale.ipn'

    buildTypes {
        applicationTest {
            initWith debug
            buildConfigField "String", "GITHUB_USERNAME", "\"" + getLocalProperty("githubUsername")+"\""
            buildConfigField "String", "GITHUB_PASSWORD", "\"" + getLocalProperty("githubPassword")+"\""
            buildConfigField "String", "GITHUB_2FA_SECRET", "\"" + getLocalProperty("github2FASecret")+"\""
        }
    }

    testBuildType "applicationTest"
}

dependencies {
    // Android dependencies.
    implementation "androidx.core:core:1.12.0"
    implementation 'androidx.core:core-ktx:1.12.0'
    implementation "androidx.browser:browser:1.8.0"
    implementation "androidx.security:security-crypto:1.1.0-alpha06"
    implementation "androidx.work:work-runtime:2.9.0"

    // Kotlin dependencies.
    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0"
    runtimeOnly "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.0"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

    // Compose dependencies.
    def composeBom = platform('androidx.compose:compose-bom:2023.06.01')
    implementation composeBom
    androidTestImplementation composeBom
    implementation 'androidx.compose.material3:material3:1.2.1'
    implementation 'androidx.compose.material:material-icons-core:1.6.3'
    implementation "androidx.compose.ui:ui:1.6.3"
    implementation "androidx.compose.ui:ui-tooling:1.6.3"
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0'
    implementation 'androidx.activity:activity-compose:1.8.2'
    implementation "com.google.accompanist:accompanist-permissions:$accompanist_version"
    implementation "com.google.accompanist:accompanist-systemuicontroller:$accompanist_version"
    implementation "androidx.core:core-splashscreen:1.1.0-alpha02"

    // Navigation dependencies.
    def nav_version = "2.7.7"
    implementation "androidx.navigation:navigation-compose:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    // Supporting libraries.
    implementation("io.coil-kt:coil-compose:2.6.0")
    implementation("com.google.zxing:core:3.5.1")

    // Tailscale dependencies.
    implementation ':libtailscale@aar'

    // Tests
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test:runner:1.5.2'
    androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.5'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    implementation 'androidx.test.uiautomator:uiautomator:2.3.0'

    // Authentication only for tests
    androidTestImplementation 'dev.turingcomplete:kotlin-onetimepassword:2.4.0'
    androidTestImplementation 'commons-codec:commons-codec:1.16.1'
    
    debugImplementation("androidx.compose.ui:ui-tooling")
    implementation("androidx.compose.ui:ui-tooling-preview")
}

def getLocalProperty(key) {
    try {
        Properties properties = new Properties()
        properties.load(project.file('local.properties').newDataInputStream())
        return properties.getProperty(key);
    } catch(Throwable ignored) {
        return ""
    }
}