/*
 * Copyright (C) 2013-2020 Federico Iosue (federico@iosue.it)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

apply plugin: 'com.android.application'
apply plugin: 'jacoco'

boolean signConfigExists = project.hasProperty("SIGN_PROPS") && new File(project.property("SIGN_PROPS")).exists()
if(signConfigExists) {
    Properties props = new Properties()
    props.load(new FileInputStream(file(project.property("SIGN_PROPS"))))

    android {
        signingConfigs {
            release {
                storeFile file(props['keystore'])
                storePassword props['keystore.password']
                keyAlias props['keystore.alias']
                keyPassword props['keystore.alias.password']
            }
        }
    }
}

android {

    if (project.hasProperty("SIGN_PROPS")
            && new File(project.property("SIGN_PROPS").toString()).exists()) {
        Properties props = new Properties()
        props.load(new FileInputStream(file(project.property("SIGN_PROPS"))))
        signingConfigs {
            signConfig {
                storeFile file(props['keystore'])
                storePassword props['keystore.password']
                keyAlias props['keystore.alias']
                keyPassword props['keystore.alias.password']
            }
        }
    }

    compileSdkVersion Integer.parseInt(project.TARGET_SDK)
    buildToolsVersion project.BUILD_TOOLS

    defaultConfig {
        applicationId project.PACKAGE
        minSdkVersion project.MIN_SDK
        targetSdkVersion project.TARGET_SDK
        versionName project.VERSION_NAME
        versionCode Integer.parseInt(project.VERSION_CODE)
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
        multiDexEnabled true
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    buildTypes {
        debug {
            testCoverageEnabled true
        }
        release {
            minifyEnabled true
            shrinkResources false
            multiDexEnabled false
            testCoverageEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            android.applicationVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = "OmniNotes-${variant.name}-${variant.versionName}.apk"
                }
            }
            if(signConfigExists) {
                signingConfig signingConfigs.release
            }
        }
    }

    flavorDimensions "standard"
    productFlavors {
        play {
            dimension "standard"
        }
        foss {
            dimension "standard"
            applicationId project.PACKAGE + ".foss"
        }
        beta {
            dimension "standard"
        }
        alpha {
            dimension "standard"
            applicationId project.PACKAGE + ".alpha"
        }
    }

    lintOptions {
        abortOnError false
        disable 'MissingTranslation'
    }

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

dependencies {
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-all:1.9.5'
    testImplementation 'org.powermock:powermock:1.6.5'
    testImplementation 'org.powermock:powermock-module-junit4:1.6.5'
    testImplementation 'org.powermock:powermock-api-mockito:1.6.5'

    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test:rules:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation ('androidx.test.espresso:espresso-intents:3.1.0') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }

    compileOnly "org.projectlombok:lombok:1.18.10"
    annotationProcessor 'org.projectlombok:lombok:1.18.10'

    implementation 'com.larswerkman:HoloColorPicker:1.4'
    implementation 'de.keyboardsurfer.android.widget:crouton:1.8.4@aar'
    implementation 'com.google.android.apps.dashclock:dashclock-api:2.0.0'
    implementation "ch.acra:acra-http:5.4.0"
    implementation "ch.acra:acra-toast:5.4.0"
    implementation 'com.github.gabrielemariotti.changeloglib:changelog:2.0.0'
    implementation 'com.appeaser.sublimepickerlibrary:sublimepickerlibrary:2.1.2'
    implementation 'androidx.preference:preference:1.1.0'
    implementation 'com.android.support:multidex:2.0.1'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'de.greenrobot:eventbus:2.4.0'
    implementation 'com.pushbullet:android-extensions:1.0.4@aar'
    implementation 'com.getbase:floatingactionbutton:1.10.1'
    implementation 'com.nhaarman.listviewanimations:lib-core:3.1.0@aar'
    implementation 'com.nhaarman.listviewanimations:lib-manipulation:3.1.0@aar'
    implementation 'com.github.bumptech.glide:glide:4.10.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
    implementation('com.github.afollestad.material-dialogs:core:0.9.6.0') {
        transitive = true
        exclude group: "com.android.support"
    }
    implementation('com.github.afollestad.material-dialogs:commons:0.9.6.0') {
        transitive = true
        exclude group: "com.android.support"
    }
    implementation 'com.jakewharton:butterknife:10.2.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
    implementation('org.mnode.ical4j:ical4j:3.0.11') {
        exclude group: 'commons.io'
    }
    debugImplementation ('com.squareup.leakcanary:leakcanary-android:1.6.2') {
        exclude group: "com.android.support"
    }
    implementation 'org.apache.commons:commons-lang3:3.9'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
    testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
    implementation 'com.pnikosis:materialish-progress:1.5'
    implementation 'com.github.paolorotolo:appintro:1.3.0'
    implementation 'io.reactivex:rxandroid:1.0.1'
    implementation 'io.reactivex:rxjava:1.0.14'
    implementation 'com.artemzin.rxjava:proguard-rules:1.0.14.2'
    implementation 'com.tbruyelle.rxpermissions:rxpermissions:0.4.2@aar'
    implementation 'org.ocpsoft.prettytime:prettytime:3.2.7.Final'
    implementation 'org.bitbucket.cowwoc:diff-match-patch:1.1'
    implementation 'com.github.bosphere.android-filelogger:filelogger:1.0.7'

    implementation('com.github.federicoiosue:simplegallery:3.0.1') {
        exclude group: 'com.android.support'
    }
    implementation 'com.github.federicoiosue:Springpad-Importer:1.0.1'
    implementation ('com.github.federicoiosue:Omni-Notes-Commons:1.2.0') {
        exclude group: 'org.projectlombok'
    }
    implementation ('com.github.federicoiosue:checklistview:3.2.1') {
        transitive=false
    }
    implementation 'com.github.federicoiosue:pixlui:3.0.0'

    // Flavors specific dependencies
    playImplementation 'io.nlopez.smartlocation:library:3.2.4'
    betaImplementation 'io.nlopez.smartlocation:library:3.2.4'
    alphaImplementation 'io.nlopez.smartlocation:library:3.2.4'
    fossImplementation 'com.github.federicoiosue:smart-location-lib:3.2.6'

    playImplementation ('com.github.federicoiosue:analitica:0.0.3:googleAnalyticsRelease@aar'){
        transitive=true
    }
    betaImplementation ('com.github.federicoiosue:analitica:0.0.3:googleAnalyticsRelease@aar'){
        transitive=true
    }
    alphaImplementation ('com.github.federicoiosue:analitica:0.0.3:googleAnalyticsRelease@aar'){
        transitive=true
    }
    fossImplementation ('com.github.federicoiosue:analitica:0.0.3:piwikRelease@aar'){
        transitive=true
    }
    playImplementation 'com.google.android.gms:play-services-analytics:9.0.2'
    betaImplementation 'com.google.android.gms:play-services-analytics:9.0.2'
    alphaImplementation 'com.google.android.gms:play-services-analytics:9.0.2'
    fossImplementation ('org.piwik.sdk:piwik-sdk:1.0.2'){
        exclude group: 'com.jakewharton.timber', module: 'timber'
    }
    fossImplementation 'com.jakewharton.timber:timber:4.5.1'

    testImplementation 'junit:junit:4.12'
}

configurations.all {
    exclude group: 'commons-logging', module: 'commons-logging'
    resolutionStrategy {
        force 'com.android.support:support-annotations:28.0.0'
        force 'com.android.support:support-v4:28.0.0'
    }
}

android.productFlavors.each { flavor ->
    String analyticsUrl = flavor.name == 'foss' && ANALYTICS_PARAMS_FOSS?.trim() ? ANALYTICS_PARAMS_FOSS : ANALYTICS_PARAMS
    flavor.buildConfigField 'String', 'ANALYTICS_PARAMS', '"' + analyticsUrl + '"'
    flavor.buildConfigField 'String', 'CRASH_REPORTING_URL', '"' + CRASH_REPORTING_URL + '"'
    flavor.buildConfigField 'String', 'VERSION_CHECK_URL', '"' + VERSION_CHECK_URL + '"'
    flavor.buildConfigField 'String', 'MAPS_API_KEY', '"' + MAPS_API_KEY + '"'
}

apply plugin: 'jacoco-android'
jacocoAndroidUnitTestReport {
    csv.enabled false
    html.enabled false
    xml.enabled true
}
jacoco {
    toolVersion = "0.7.5.201505241946"
}

tasks.withType(Test) {
    testLogging {
        events "standardOut", "started", "passed", "skipped", "failed"
    }
}
