/*
 * Copyright 2016 Futurice GmbH
 *
 * 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.
 */

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'checkstyle'

apply from: 'app-config.gradle'

def STRING = "String"

android {
    compileSdkVersion 27
    buildToolsVersion '28.0.3'

    defaultConfig {
        applicationId "com.futurice.freesound"
        minSdkVersion 22
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        signingConfig signingConfigs.debug
        multiDexEnabled true
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'

        buildConfigField(STRING, "FREESOUND_API_URL", freesound_api_url)
        buildConfigField(STRING, "FREESOUND_API_CLIENT_ID", freesound_api_client_id)
        buildConfigField(STRING, "FREESOUND_API_CLIENT_SECRET", freesound_api_client_secret)

        // AutoValue and AutoFactory require this workaround because they haven't separated their
        // annotation processor from their compile time dependencies.
        //
        // Without this, compilation would fail as it requires that annotation processors are only
        // used by kapt/annotationProcessor.
        //
        // Note: This option has also been targeted for deprecation by the Android Gradle plugin.
        //
        // Refer: https://github.com/google/auto/issues/268
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath false
            }
        }
    }

    signingConfigs {
        debug {
            storeFile file("keystore/debug.keystore")
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '-DEBUG'
            testCoverageEnabled = true
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    './proguard-rules/android-support-rules.pro',
                    './proguard-rules/autovalue-rules.pro',
                    './proguard-rules/okhttp3-rules.pro',
                    './proguard-rules/picasso-rules.pro',
                    './proguard-rules/retrofit2-rules.pro',
                    './proguard-rules/rxjava2-rules.pro',
                    './proguard-rules/okio-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    // Required so Android Studio can find source files for auto-completion for Kotlin generated
    // code, such as Jonty. It still compiles via Gradle without this, but Android Studio would
    // show the call site as missing.
    buildTypes.all { buildType ->
        sourceSets.getByName(buildType.name).java.srcDirs +=
                "build/generated/source/kaptKotlin/${buildType.name}"
    }

    lintOptions {
        lintConfig file('./quality/lint.xml')
        abortOnError false
    }

}

androidExtensions {
    experimental = true
}

task checkstyle(type: Checkstyle) {
    group 'verification'
    description 'Checks if the code adheres to the coding standards'
    configFile file('quality/checkstyle.xml')
    source 'src/main/java'
    include '**/*.java'
    exclude '**/gen/**'
    classpath = files() // empty because unnecessary for checkstyle
}

ext {
    dependencyVersions = [
            kotlin                  : kotlin_version,
            androidSupport          : '27.1.1',
            androidSupportConstraint: '1.1.0',
            androidArchLifecycle    : '1.1.1',
            androidSupportTest      : '1.0.0',
            dagger2                 : '2.4',
            rxJava2                 : '2.1.0',
            rxAndroid               : '2.0.2',
            rxBinding               : '2.0.0',
            okHttp                  : '3.6.0',
            picasso                 : '2.5.2',
            retrofit2               : '2.2.0',
            retrofit2RxJava2Adapter : '1.0.0',
            retrofit2GsonConverter  : '2.0.0',
            moshi                   : '1.6.0',
            moshiConverter          : '2.4.0',
            autoFactory             : '1.0-beta5',
            autoValue               : '1.5.2',
            jsr250                  : '1.0',
            jonty                   : '0.7',
            options                 : '1.2.4',
            exoplayer               : 'r2.1.1',
            butterknife             : '8.7.0',
            firebase                : '9.4.0',
            stetho                  : '1.5.0',
            timber                  : '4.7.0',
            junit                   : '4.12',
            mockito                 : '2.18.3',
            assertJ                 : '1.7.1',
            leakCanary              : '1.5.1',
            rxLint                  : '1.6.1']
}

dependencies {
    // Kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$dependencyVersions.kotlin"

    // Android Support
    implementation "com.android.support:support-v4:$dependencyVersions.androidSupport"
    implementation "com.android.support:support-annotations:$dependencyVersions.androidSupport"
    implementation "com.android.support:appcompat-v7:$dependencyVersions.androidSupport"
    implementation "com.android.support:palette-v7:$dependencyVersions.androidSupport"
    implementation "com.android.support:recyclerview-v7:$dependencyVersions.androidSupport"
    implementation "com.android.support:cardview-v7:$dependencyVersions.androidSupport"
    implementation "com.android.support:design:$dependencyVersions.androidSupport"
    implementation "com.android.support.constraint:constraint-layout:$dependencyVersions.androidSupportConstraint"

    implementation "android.arch.lifecycle:runtime:$dependencyVersions.androidArchLifecycle"
    implementation "android.arch.lifecycle:extensions:$dependencyVersions.androidArchLifecycle"
    implementation "android.arch.lifecycle:reactivestreams:$dependencyVersions.androidArchLifecycle"
    kapt "android.arch.lifecycle:compiler:$dependencyVersions.androidArchLifecycle"
    testImplementation "android.arch.core:core-testing:$dependencyVersions.androidArchLifecycle"

    // Dagger 2
    kapt "com.google.dagger:dagger-compiler:$dependencyVersions.dagger2"
    implementation "com.google.dagger:dagger:$dependencyVersions.dagger2"

    // RxJava
    implementation "io.reactivex.rxjava2:rxandroid:$dependencyVersions.rxAndroid"
    implementation "io.reactivex.rxjava2:rxjava:$dependencyVersions.rxJava2"

    // RxBinding
    implementation "com.jakewharton.rxbinding2:rxbinding:$dependencyVersions.rxBinding"
    implementation "com.jakewharton.rxbinding2:rxbinding-design-kotlin:$dependencyVersions.rxBinding"
    implementation "com.jakewharton.rxbinding2:rxbinding-support-v4-kotlin:$dependencyVersions.rxBinding"

    implementation 'com.jakewharton.rx2:replaying-share-kotlin:2.0.0'

    // Networking
    implementation "com.squareup.okhttp3:okhttp:$dependencyVersions.okHttp"
    implementation "com.squareup.okhttp3:logging-interceptor:$dependencyVersions.okHttp"
    implementation "com.squareup.picasso:picasso:$dependencyVersions.picasso"

    // Networking Retrofit
    implementation "com.squareup.retrofit2:retrofit:$dependencyVersions.retrofit2"
    implementation "com.jakewharton.retrofit:retrofit2-rxjava2-adapter:$dependencyVersions.retrofit2RxJava2Adapter"

    // Moshi
    implementation "com.squareup.moshi:moshi:$dependencyVersions.moshi"
    implementation "com.squareup.moshi:moshi-kotlin:$dependencyVersions.moshi"
    implementation "com.squareup.moshi:moshi-adapters:$dependencyVersions.moshi"
    implementation "com.squareup.retrofit2:converter-moshi:$dependencyVersions.moshiConverter"

    // AutoFactory
    compileOnly "com.google.auto.factory:auto-factory:$dependencyVersions.autoFactory"
    kapt "com.google.auto.factory:auto-factory:$dependencyVersions.autoFactory"

    // Auto-Value
    compileOnly "com.google.auto.value:auto-value:$dependencyVersions.autoValue"
    kapt "com.google.auto.value:auto-value:$dependencyVersions.autoValue"

    // Required because "javax.annotation.Generated" is referenced by AutoValue/Factory
    // Refer to https://github.com/google/auto/issues/240
    compileOnly "javax.annotation:jsr250-api:$dependencyVersions.jsr250"

    // Jonty
    compileOnly "com.github.peter-tackage.jonty:jonty:$dependencyVersions.jonty"
    kapt "com.github.peter-tackage.jonty:jonty-processor:$dependencyVersions.jonty"

    // Optionals for Java 6/7
    implementation "com.github.tomaszpolanski:options:$dependencyVersions.options"

    // Audio player
    implementation "com.google.android.exoplayer:exoplayer:$dependencyVersions.exoplayer"

    // Butter Knife
    implementation "com.jakewharton:butterknife:$dependencyVersions.butterknife"
    kapt "com.jakewharton:butterknife-compiler:$dependencyVersions.butterknife"

    // Timber
    implementation "com.jakewharton.timber:timber:$dependencyVersions.timber"

    // Instrumentation
    implementation "com.facebook.stetho:stetho:$dependencyVersions.stetho"
    implementation "com.facebook.stetho:stetho-okhttp3:$dependencyVersions.stetho"

    // Firebase
    implementation "com.google.firebase:firebase-core:$dependencyVersions.firebase"
    implementation "com.google.firebase:firebase-crash:$dependencyVersions.firebase"

    // Unit Testing
    testImplementation "junit:junit:$dependencyVersions.junit"
    testImplementation "org.mockito:mockito-core:$dependencyVersions.mockito"
    testImplementation "org.assertj:assertj-core:$dependencyVersions.assertJ"
    testImplementation "com.squareup.okhttp3:mockwebserver:$dependencyVersions.okHttp"

    // Instrumentation Testing
    androidTestImplementation "com.android.support.test:runner:$dependencyVersions.androidSupportTest"
    androidTestImplementation "com.android.support.test:rules:$dependencyVersions.androidSupportTest"
    androidTestImplementation "org.assertj:assertj-core:$dependencyVersions.assertJ"

    // Leak Canary
    debugImplementation "com.squareup.leakcanary:leakcanary-android:$dependencyVersions.leakCanary"
    releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$dependencyVersions.leakCanary"
    testImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$dependencyVersions.leakCanary"

    // RxLint
    implementation "nl.littlerobots.rxlint:rxlint:$dependencyVersions.rxLint"

}

apply plugin: 'com.google.gms.google-services'
