apply plugin: 'com.android.application'
apply plugin: 'jacoco'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-allopen'
apply plugin: 'androidx.navigation.safeargs.kotlin'

allOpen {
    // allows mocking for classes w/o directly opening them for release builds
    annotation 'com.elifox.legocatalog.testing.OpenClass'
}

android {
    compileSdkVersion rootProject.compileSdkVersion
    dataBinding {
        enabled = true
    }
    defaultConfig {
        applicationId "com.elifox.legocatalog"
        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion
        testInstrumentationRunner "com.elifox.legocatalog.util.AppTestRunner"
        versionCode 1
        versionName "1"
        vectorDrawables.useSupportLibrary true

        buildConfigField 'String', 'API_DEVELOPER_TOKEN', "\"${api_developer_token}\""
    }
    buildTypes {
        debug {
            testCoverageEnabled !project.hasProperty('android.injected.invoked.from.ide')
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    // work-runtime-ktx 2.1.0 and above now requires Java 8
    kotlinOptions {
        jvmTarget = "1.8"
    }

}

jacoco {
    toolVersion = "$rootProject.jacocoVersion"
}

dependencies {
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    kapt "com.github.bumptech.glide:compiler:$rootProject.glideVersion"
    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
    implementation "androidx.core:core-ktx:$rootProject.ktxVersion"
    implementation "androidx.fragment:fragment-ktx:$rootProject.fragmentVersion"
    implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.navigation:navigation-fragment-ktx:$rootProject.navigationVersion"
    implementation "androidx.navigation:navigation-ui-ktx:$rootProject.navigationVersion"
    implementation "androidx.recyclerview:recyclerview:$rootProject.recyclerViewVersion"
    implementation "androidx.room:room-runtime:$rootProject.roomVersion"
    implementation "androidx.paging:paging-runtime:$rootProject.pagingVersion"
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    implementation "androidx.work:work-runtime-ktx:$rootProject.workVersion"
    implementation "com.github.bumptech.glide:glide:$rootProject.glideVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$rootProject.kotlinVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutinesVersion"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutinesVersion"

    // Tools
    implementation "com.jakewharton.timber:timber:$rootProject.timberVersion"
    implementation "com.facebook.stetho:stetho:$rootProject.stetho"
    implementation "com.facebook.stetho:stetho-okhttp3:$rootProject.stetho"

    // Dagger
    kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"

    // Networking
    implementation "com.google.code.gson:gson:$rootProject.gsonVersion"
    implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofit"
    implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofit"
    implementation "com.squareup.okhttp3:okhttp:$rootProject.okhttp"
    implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.okhttp"

    // Testing dependencies
    testImplementation "junit:junit:$rootProject.junitVersion"
    testImplementation "com.squareup.okhttp3:mockwebserver:$rootProject.mockwebserverVersion"
    testImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
    testImplementation "org.mockito:mockito-core:$rootProject.mockitoVersion"

    androidTestImplementation "androidx.test:core:$rootProject.testCoreVersion"
    androidTestImplementation "androidx.test:runner:$rootProject.testRunnerVersion"
    androidTestImplementation "androidx.test:rules:$rootProject.testRulesVersion"

    androidTestImplementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    androidTestImplementation "androidx.recyclerview:recyclerview:$rootProject.recyclerViewVersion"
    androidTestImplementation "com.google.android.material:material:$rootProject.materialVersion"

    androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$rootProject.espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$rootProject.espressoVersion"
    androidTestImplementation "androidx.test.ext:junit:$rootProject.testExtJunit"
    androidTestImplementation "androidx.test.uiautomator:uiautomator:$rootProject.uiAutomatorVersion"
    androidTestImplementation "androidx.work:work-testing:$rootProject.workVersion"

    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
    androidTestImplementation "org.mockito:mockito-core:$rootProject.mockitoVersion"
    androidTestImplementation "org.mockito:mockito-android:$rootProject.mockitoAndroidVersion"
}

task fullCoverageReport(type: JacocoReport) {
    dependsOn 'createDebugCoverageReport'
    dependsOn 'testDebugUnitTest'
    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*',
                      '**/*Test*.*', 'android/**/*.*',
                      '**/*_MembersInjector.class',
                      '**/Dagger*Component.class',
                      '**/Dagger*Component$Builder.class',
                      '**/*_*Factory.class',
                      '**/*ComponentImpl.class',
                      '**/*SubComponentBuilder.class']
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec",
            "outputs/code-coverage/connected/*coverage.ec"
    ])
}

// we need all open to run tests which  a we enable only for debug builds.
project.tasks.whenTaskAdded {
    if (it.name == "testReleaseUnitTest") {
        it.enabled = false
    }
}
