// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
    id 'firebase-library'
    id("kotlin-android")
    id 'com.google.protobuf'
    id("copy-google-services")
}

firebaseLibrary {
    libraryGroup = "firestore"
    testLab {
        enabled = true
        timeout = '45m'
    }
    releaseNotes {
        name.set("{{firestore}}")
        versionName.set("firestore")
    }
}

protobuf {
    // Configure the protoc executable
    protoc {
        // Download from repositories
        artifact = libs.protoc.get().toString()
    }
    plugins {
        grpc {
            artifact = libs.grpc.protoc.gen.java.get().toString()
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java { option 'lite' }
            }
            task.plugins {
                grpc {
                    option 'lite'
                }
            }
        }
    }
}

android {
    adbOptions {
        timeOutInMs 60 * 1000
    }

    namespace "com.google.firebase.firestore"
    compileSdkVersion project.compileSdkVersion
    defaultConfig {
        targetSdkVersion project.targetSdkVersion
        minSdkVersion project.minSdkVersion
        versionName version
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'proguard.txt'

        // Acceptable values are: 'emulator', 'qa', 'nightly', and 'prod'.
        def targetBackend = findProperty("targetBackend") ?: "emulator"
        buildConfigField("String", "TARGET_BACKEND", "\"$targetBackend\"")

        def targetDatabaseId = findProperty('targetDatabaseId') ?: "(default)"
        buildConfigField("String", "TARGET_DATABASE_ID", "\"$targetDatabaseId\"")

        def localProps = new Properties()

        try {
            file("local.properties").withInputStream { localProps.load(it) }
        } catch (FileNotFoundException e) {
        }
    }

    sourceSets {
        main {
            proto {
                srcDir 'src/proto'
            }
        }
        test {
            java {
                srcDir 'src/testUtil/java'
                srcDir 'src/roboUtil/java'
            }
        }
        androidTest {
            java {
                srcDir 'src/testUtil/java'
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    testOptions.unitTests.includeAndroidResources = true

}

kotlin { compilerOptions { jvmTarget = JvmTarget.JVM_1_8 } }

tasks.withType(Test) {
    maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1
}

dependencies {
    javadocClasspath libs.autovalue.annotations

    api project(':protolite-well-known-types')
    api libs.playservices.tasks
    api libs.firebase.annotations
    api 'com.google.firebase:firebase-appcheck-interop:17.0.0'
    api('com.google.firebase:firebase-auth-interop:19.0.2') {
        exclude group: "com.google.firebase", module: "firebase-common"
    }

    api(libs.firebase.common)
    api(libs.firebase.components)
    api('com.google.firebase:firebase-database-collection:18.0.1')

    implementation libs.androidx.annotation
    implementation libs.playservices.base
    implementation libs.playservices.basement
    implementation libs.grpc.android
    implementation libs.grpc.okhttp
    implementation libs.grpc.protobuf.lite
    implementation libs.grpc.stub
    implementation libs.kotlin.stdlib
    implementation libs.kotlinx.coroutines.core

    compileOnly libs.autovalue.annotations
    compileOnly libs.javax.annotation.jsr250

    annotationProcessor libs.autovalue

    androidTestAnnotationProcessor libs.autovalue

    testImplementation project(':firebase-database-collection')
    testImplementation project(':firebase-firestore')
    testProtobuf(libs.proto.google.common.protos)
    testImplementation libs.androidx.test.core
    testImplementation libs.jackson.databind
    testImplementation libs.playservices.tasks
    testImplementation 'com.google.guava:guava-testlib:12.0-rc2'
    testImplementation libs.truth
    testImplementation libs.junit
    testImplementation libs.hamcrest
    testImplementation libs.mockito.core
    testImplementation libs.robolectric

    testCompileOnly libs.protobuf.java

    androidTestImplementation libs.androidx.annotation
    androidTestImplementation libs.androidx.test.rules
    androidTestImplementation libs.androidx.test.runner
    androidTestImplementation libs.androidx.test.junit
    androidTestImplementation libs.jackson.databind
    androidTestImplementation(libs.truth) {
        exclude group: "org.codehaus.mojo", module: "animal-sniffer-annotations"
    }
    androidTestImplementation libs.junit
    androidTestImplementation libs.mockito.android
    androidTestImplementation libs.mockito.core
}

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        // TODO(wuandy): Also add "-Xlint:unchecked". But currently that
        // enables 100+ warnings due to our generated source code.
        // TODO(wuandy): Re-enable error on warnings once errorprone issues are fixed.
        options.compilerArgs << "-Xlint:deprecation" // << "-Werror"
    }
}
