apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'org.jlleitschuh.gradle.ktlint'
apply plugin: 'io.gitlab.arturbosch.detekt'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    Properties properties = new Properties()
    properties.load(project.rootProject.file("local.properties").newDataInputStream())

    defaultConfig {
        applicationId "com.adammcneilly.androidstudyguide"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        // If you want to talk to a real Segment instance, provide the `SegmentKey` in your local.properties
        // file.
//        buildConfigField("String", "SEGMENT_API_KEY", properties.getProperty("SegmentKey"))
        buildConfigField("String", "SEGMENT_API_KEY", "TEST_API_KEY")
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    buildFeatures {
        viewBinding = true
        dataBinding = true
        compose = true
    }

    lintOptions {
        abortOnError false
    }

    packagingOptions {
        exclude 'META-INF/AL2.0'
        exclude 'META-INF/LGPL2.1'
    }

    composeOptions {
        kotlinCompilerExtensionVersion compose_version
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.4.0-alpha03'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
    implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1'
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-simplexml:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
    implementation "com.google.dagger:hilt-android:$hilt_plugin_version"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation "androidx.compose.foundation:foundation:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
    implementation "androidx.compose.material:material-icons-core:$compose_version"
    implementation "androidx.compose.material:material-icons-extended:$compose_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07"
    implementation "androidx.navigation:navigation-compose:2.4.0-alpha05"
    implementation "androidx.activity:activity-compose:1.3.0"
    implementation platform('com.google.firebase:firebase-bom:28.0.1')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation "com.google.accompanist:accompanist-placeholder-material:0.14.0"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_plugin_version"
    implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03'
    implementation 'com.segment.analytics.android:analytics:4.10.0'
    // When using Kotlin.
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    testImplementation "com.google.truth:truth:1.1.3"
    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1'
    testImplementation "androidx.arch.core:core-testing:2.1.0"
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "com.google.truth:truth:1.1.3"
    androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

    def room_version = "2.3.0"

    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    // optional - Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"

    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    lintChecks project(":lint-checks")
}

ktlint {
    reporters {
        reporter "plain"
        reporter "checkstyle"
    }
    outputColorName = "RED"
}

detekt {
    config = files("${rootProject.projectDir}/config/detekt/detekt.yml") // point to your custom config defining rules to run, overwriting default behavior

    reports {
        html.enabled = true // observe findings in your browser with structure and code snippets
        xml.enabled = true // checkstyle like format mainly for integrations like Jenkins
        txt.enabled = true // similar to the console output, contains issue signature to manually edit baseline files
    }
}

def isNonStable = { String version ->
    def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase().contains(it) }
    def regex = /^[0-9,.v-]+(-r)?$/
    return !stableKeyword && !(version ==~ regex)
}

tasks.named("dependencyUpdates").configure {
    rejectVersionIf {
        isNonStable(it.candidate.version) && !isNonStable(it.currentVersion)
    }
}