/*
 * 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
 *
 *     https://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.
 */

plugins {
    id "com.google.protobuf" version "0.8.8"
}

apply plugin: "com.android.application"
apply plugin: "kotlin-android"
apply plugin: "kotlin-android-extensions"

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.google.api.examples.kotlin.client"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
    packagingOptions {
        // TODO: find a better way than excluding this here
        exclude "META-INF/main.kotlin_module"
    }
    sourceSets {
        // add the proto file from the server project
        main {
            proto {
                srcDir "../../example-apis"
            }
        }

        // add generated unit tests to the project
        test.java.srcDirs += "${project.buildDir}/generated/source/protoTest/debugUnitTest/client"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

configurations {
    ktlint
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation "com.android.support:appcompat-v7:28.0.0"
    implementation "com.android.support:support-v4:28.0.0"
    implementation "javax.annotation:javax.annotation-api:1.3.2"

    // normally get the KGax library via jitpack:
    //    (this library is in preview and not yet published to maven)
    //
    // implementation("com.github.googleapis.gax-kotlin:kgax-grpc-android:v0.6.0")
    //
    // but we use a local copy for development
    implementation "com.google.api:kgax-grpc-android:0.7.0-SNAPSHOT"

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0"

    // Dependency for Google Sign-In
    implementation "com.google.android.gms:play-services-auth:16.0.1"

    testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
    testImplementation "com.nhaarman:mockito-kotlin:1.6.0"
    androidTestImplementation "com.android.support.test:rules:1.0.2"
    androidTestImplementation "com.android.support.test:runner:1.0.2"
    androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.2"
    androidTestImplementation "org.awaitility:awaitility:3.1.1"
    androidTestImplementation "org.awaitility:awaitility-kotlin:3.1.1"

    // needed to unit test with suspend functions (can remove when the dependency above is updated most likely)
    testImplementation("org.mockito:mockito-core:2.23.4")

    ktlint "com.github.shyiko:ktlint:0.31.0"
}

// compile proto and generate gRPC Kotlin clients!
protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.7.1"
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        client {
            // get the KGen code generator
            // this is normally done through the package manager, i.e.:
            //   artifact = 'com.github.googleapis:gapic-generator-kotlin:master-SNAPSHOT:core@jar'
            // but these examples are used for testing so we'll use a local copy instead
            path = "${projectDir}/../../runLocalGenerator.sh"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite {}
                // generate clients
                client {
                    // this option will add generated unit tests for the client to the project
                    // be sure to add the directory to your test source set(s) as shown above
                    option "test-output=${project.buildDir}/generated/source/protoTest/debugUnitTest/client"
                    
                    // this option is not typical. It is used here because the cloud API
                    // definitions used in this example use a legacy configuration format.
                    // This will be removed once they are updated.
                    option "source=${projectDir}/../../example-apis"

                    // this options should be added for Google APIs
                    option "auth-google-cloud"

                    // this option should be added for Android
                    option "lite"
                }
            }
            // add something like this to have the generated clients formatted with your project settings
            ktlintFormatGenerated.shouldRunAfter task
        }
    }
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

task ktlint(type: JavaExec, group: "verification") {
    description = "Check Kotlin code style."
    classpath = configurations.ktlint
    main = "com.github.shyiko.ktlint.Main"
    args "src/main/**/*.kt"
}
check.dependsOn ktlint

task ktlintFormat(type: JavaExec, group: "formatting") {
    description = "Fix Kotlin code style deviations."
    main = "com.github.shyiko.ktlint.Main"
    classpath = configurations.ktlint
    args "-F", "src/**/*.kt", "test/**/*.kt"
}

task ktlintFormatGenerated(type: JavaExec, group: "formatting") {
    description = "Fix Kotlin code style deviations."
    main = "com.github.shyiko.ktlint.Main"
    classpath = configurations.ktlint
    args "-F", "${project.buildDir}/generated/source/proto/**/*.kt"
}
