/*
 * Copyright (C) 2018 Florian Dreier
 *
 * This file is part of MyTargets.
 *
 * MyTargets is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * MyTargets is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
plugins {
    id 'com.android.application'
    id 'com.github.triplet.play' version '2.2.0'
    id 'com.getkeepsafe.dexcount'
}

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
apply plugin: 'androidx.navigation.safeargs'

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    compileSdkVersion compileSdkVersionCode

    defaultConfig {
        applicationId 'de.dreier.mytargets'
        minSdkVersion 16
        targetSdkVersion 28
        versionCode gitVersionCode
        versionName gitVersionName
//        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        vectorDrawables.useSupportLibrary true
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        // The following argument makes the Android Test Orchestrator run its
        // "pm clear" command after each test invocation. This command ensures
        // that the app's state is completely cleared between tests.
        testInstrumentationRunnerArguments clearPackageData: 'true'
    }

    sourceSets {
        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
    }

    dataBinding {
        enabled true
    }

    androidExtensions {
        experimental true
    }

    signingConfigs {
        debug {
            storeFile file('../debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }

        release {
            storeFile file(KEYSTORE_NAME)
            storePassword KEYSTORE_PASSWORD
            keyAlias KEY_ALIAS
            keyPassword KEY_PASSWORD
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            signingConfig signingConfigs.debug
            testCoverageEnabled true
        }
        release {
            signingConfig signingConfigs.release
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), '../tools/rules-proguard.pro'
        }
    }

    flavorDimensions "mode"

    productFlavors {
        dev {
            dimension "mode"
            multiDexEnabled true
        }
        regular {
            dimension "mode"
        }
        screengrab {
            dimension "mode"
            multiDexEnabled true
        }
    }

    bundle {
        language {
            // Splitting by language is disabled to preserve the app's internal language selection
            // See LanguagePreference
            enableSplit = false
        }
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/ASL2.0'
    }

    useLibrary 'android.test.runner'
    useLibrary 'android.test.base'
    useLibrary 'android.test.mock'

    testOptions {
        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }
}

apply from: '../tools/script-play-store.gradle'

dependencies {
    Map<String, String> dependencies = rootProject.ext.dependencies

    implementation dependencies.kotlinStdlibJdk7

    implementation dependencies.androidxCore
    implementation dependencies.androidxCoreUtils
    implementation dependencies.androidxCoreUi
    implementation dependencies.androidxFragment
    implementation dependencies.androidxV13
    implementation dependencies.appCompat
    implementation dependencies.androidxDesign
    implementation dependencies.androidxAnnotations
    implementation dependencies.androidxPreference
    implementation dependencies.androidxExifInterface
    implementation dependencies.recyclerView
    implementation dependencies.cardView

    implementation dependencies.androidxCoreKtx
    implementation dependencies.androidFragmentKtx

    implementation dependencies.timber

    implementation dependencies.firebaseCore
    implementation dependencies.firebaseCrashlytics

    implementation dependencies.playServicesAuth
    implementation(dependencies.googleHttpClientAuth) {
//        exclude: group: 'com.google.http-client:google-http-client-apache'
    }
    implementation(dependencies.googleApiClientAndroid) {
        exclude group: 'org.apache.httpcomponents'
        exclude group: 'com.google.http-client', module: 'google-http-client-apache'
    }
    implementation(dependencies.googleApiServicesDrive) {
        exclude group: 'org.apache.httpcomponents'
        exclude group: 'com.google.http-client', module: 'google-http-client-apache'
    }

    implementation dependencies.gson

    implementation dependencies.okHttp
    implementation dependencies.okHttpUrlConnection

    implementation dependencies.retrofit
    implementation dependencies.retrofitConverterGson

    implementation dependencies.picasso

    implementation dependencies.androidState
    kapt dependencies.androidStateProcessor

    implementation dependencies.permissionsDispatcher
    kapt dependencies.permissionsDispatcherProcessor

    implementation dependencies.iab

    implementation dependencies.mpAndroidChart

    implementation dependencies.discreteSeekbar

    implementation dependencies.easyImage

    implementation dependencies.materialDialogs

    implementation dependencies.androidAboutPage

    implementation dependencies.homageCore
    implementation dependencies.homageRecyclerView

    implementation dependencies.roundedImageView

    implementation dependencies.materialIntroScreen

    implementation dependencies.photoView

    implementation dependencies.androidLanguages

    implementation dependencies.versionInfoMaterialDialog

    implementation dependencies.signaturePad

    implementation dependencies.archLifecycleExtensions
    kapt dependencies.archLifecycleCompiler
    implementation dependencies.archRoomRuntime
    kapt dependencies.archRoomCompiler
    implementation dependencies.archNavigationFragment
    implementation dependencies.archNavigationUi

    implementation project(':shared')
    wearApp project(':wearable')

    testImplementation dependencies.jUnit
    testImplementation dependencies.kotlinTest
    testImplementation dependencies.kotlinTestJunit

    androidTestImplementation dependencies.truth
    androidTestImplementation dependencies.androidxTestCore
    androidTestImplementation dependencies.runner
    androidTestImplementation dependencies.rules
    androidTestUtil dependencies.androidTestOrchestrator
    androidTestImplementation dependencies.uiAutomator
    androidTestImplementation dependencies.espressoCore
    androidTestImplementation dependencies.espressoContrib
    androidTestImplementation dependencies.espressoIntents

    implementation dependencies.espressoIdlingResource
    androidTestScreengrabImplementation dependencies.screengrab
    androidTestImplementation dependencies.archRoomTesting
    androidTestImplementation dependencies.androidxTestExt

    implementation dependencies.multiDex

}

configurations.all {
    exclude module: 'httpclient'
    exclude module: 'commons-logging'
    resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'com.google.guava' && details.requested.name == 'guava') {
            details.useVersion '27.0.1-android'
        } else if (details.requested.group == 'org.checkerframework' && details.requested.name == 'checker-compat-qual') {
            details.useVersion '2.5.5'
        } else if (details.requested.group == 'com.google.errorprone' && details.requested.name == 'error_prone_annotations') {
            details.useVersion '2.3.1'
        }
    }
}

// Must be at the bottom
if (HAS_GMS_SECRET) {
    apply plugin: 'com.google.gms.google-services'
} else {
    println 'GMS secret is missing!'
}