/*
 * Copyright 2020 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'
}

firebaseLibrary {
    libraryGroup = "perf"
    publishJavadoc = true
    // Disabling Test Lab as there are no instrumentation tests for the SDK currently.
    // When we add instrumentation tests in the future, this needs to be enabled.
    testLab.enabled = false // Runs SDK Instrumentation Tests on Firebase Test Lab.
    releaseNotes {
        name.set("{{perfmon}}")
        versionName.set("performance")
        artifactName.set("firebase-performance")
    }
}

protobuf {
    protoc {
        artifact = libs.protoc.get().toString()
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java { option 'lite' }
            }
        }
    }
}

android {
    namespace "com.google.firebase.perf"
    compileSdk project.compileSdkVersion

    lintOptions {
        abortOnError true
        baseline file("lint-baseline.xml")
    }

    defaultConfig {
        // Note: To control the values of the auto-generated BuildConfig.java file (or any static
        // final field) in the Unit Test (with ReflectionHelpers.setStaticField()) we need to avoid
        // Java compiler inlining the value of the static final field with a code that accesses the
        // field. This can be done by making the value a result of a method call, even a dummy one.
        // This is why we are using String.valueOf() or Boolean.valueOf() in "buildConfigField()".
        //
        // Refer:
        //  - http://stackoverflow.com/q/14102000
        //  - https://stackoverflow.com/q/42130730
        // TODO(b/166645747): Remove all non-public references from the codebase before open sourcing.
        buildConfigField("String", "TRANSPORT_LOG_SRC", "String.valueOf(\"FIREPERF\")")
        buildConfigField("Boolean", "ENFORCE_DEFAULT_LOG_SRC", "Boolean.valueOf(false)")
        buildConfigField("String", "FIREPERF_VERSION_NAME", "String.valueOf(\"" + property("version") + "\")")

        if (project.hasProperty("fireperfBuildForAutopush")) {
            // This allows the SDK to be built for "Autopush" env when the mentioned flag
            // (-PfireperfBuildForAutopush) is passed in the gradle build command (of either the
            // SDK or the Test App).
            buildConfigField("String", "TRANSPORT_LOG_SRC", "String.valueOf(\"FIREPERF_AUTOPUSH\")")
            buildConfigField("Boolean", "ENFORCE_DEFAULT_LOG_SRC", "Boolean.valueOf(true)")
        }

        minSdkVersion project.minSdkVersion
        multiDexEnabled true
        targetSdkVersion project.targetSdkVersion
        versionName version
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

kotlin { compilerOptions { jvmTarget = JvmTarget.JVM_1_8 } }

dependencies {
    annotationProcessor libs.dagger.compiler
    compileOnly group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.14'
    implementation libs.androidx.annotation
    implementation libs.androidx.lifecycle.process
    implementation libs.playservices.tasks
    implementation libs.protobuf.java.lite
    implementation libs.kotlin.stdlib
    implementation libs.androidx.appcompat
    implementation 'com.google.android.datatransport:transport-api:3.0.0'
    implementation libs.dagger.dagger
    api libs.firebase.annotations
    api 'com.google.firebase:firebase-installations-interop:17.1.0'
    api project(":protolite-well-known-types")
    implementation libs.okhttp
    api(libs.firebase.common)
    api(libs.firebase.components)
    api("com.google.firebase:firebase-config:21.5.0") {
        exclude group: 'com.google.firebase', module: 'firebase-common-ktx'
    }
    api("com.google.firebase:firebase-installations:18.0.0") {
        exclude group: 'com.google.firebase', module: 'firebase-common-ktx'
    }
    api("com.google.firebase:firebase-sessions:2.0.7") {
        exclude group: 'com.google.firebase', module: 'firebase-common'
        exclude group: 'com.google.firebase', module: 'firebase-common-ktx'
        exclude group: 'com.google.firebase', module: 'firebase-components'
    }
    javadocClasspath libs.autovalue.annotations
    javadocClasspath libs.findbugs.jsr305
    runtimeOnly("com.google.firebase:firebase-datatransport:18.1.8") {
        exclude group: 'com.google.firebase', module: 'firebase-common'
        exclude group: 'com.google.firebase', module: 'firebase-components'
    }
    testCompileOnly libs.protobuf.java
    testImplementation libs.androidx.test.core
    testImplementation libs.truth
    testImplementation libs.robolectric
    testImplementation libs.androidx.test.rules
    testImplementation libs.junit
    testImplementation libs.mockito.core
    testImplementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'
}
