apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'

def appVersion = (new File(project.rootDir, 'version.txt')).text.trim()

ext.generateVersionCode = { versionName ->
    def (versionMajor, versionMinor, versionPatch) = versionName.tokenize(".")
    Integer versionCode = versionMajor.toInteger() * 100000000 + versionMinor.toInteger() * 10000 + versionPatch.toInteger()
    logger.info('Version code based on version {}: {}', versionName, versionCode)
    return versionCode
}

android {
    compileSdk rootProject.ext.compileSdkVersion
    defaultConfig {
        applicationId "com.sample.android.tmdb"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode generateVersionCode(appVersion)
        versionName appVersion
        buildConfigField "String", "TMDB_API_KEY", "\"${getProperty("local.properties", "tmdb_api_key") ?: System.getenv("TMDB_API_KEY")}\""
        buildConfigField "String", "TMDB_BASE_URL", "\"http://api.themoviedb.org/\""
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }

    signingConfigs {
        release {
            storeFile file(RELEASE_STORE_FILE)
            storePassword RELEASE_STORE_PASSWORD
            keyAlias RELEASE_KEY_ALIAS
            keyPassword RELEASE_KEY_PASSWORD
        }
    }

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

    dataBinding {
        enabled = true
    }

    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion compose
        kotlinCompilerVersion kotlin_version
    }
    kotlinOptions {
        jvmTarget = '1.8'
        freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn", "-Xjvm-default=all"]
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // Support libraries
    implementation "androidx.appcompat:appcompat:$appCompat"
    implementation "com.google.android.material:material:$materialVersion"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:$swiperefreshVersion"
    implementation "androidx.palette:palette-ktx:$paletteVersion"
    implementation "androidx.constraintlayout:constraintlayout:$constraintLayoutVersion"
    implementation "com.google.guava:guava:$guavaVersion"

    // Architecture components
    implementation "androidx.paging:paging-runtime:$pagingVersion"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"

    // Compose
    implementation "androidx.compose.ui:ui:$compose"
    implementation "androidx.compose.material:material:$compose"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose"
    implementation "androidx.activity:activity-compose:$activity_compose"
    implementation 'io.coil-kt:coil-compose:1.4.0'

    // Gson
    implementation "com.google.code.gson:gson:$gsonVersion"

    // Retrofit
    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.retrofit2:retrofit-mock:$retrofitVersion"

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

    //Android RX
    implementation "io.reactivex.rxjava2:rxjava:$rxjavaVersion"
    implementation "io.reactivex.rxjava2:rxandroid:$rxandroidVersion"

    // Other
    implementation "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"
    implementation "de.hdodenhof:circleimageview:$circleimageviewVersion"
    implementation "com.github.bumptech.glide:glide:$glideVersion"
    kapt "com.github.bumptech.glide:compiler:$glideVersion"

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

    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"

    // Firebase
    implementation 'com.google.android.play:core:1.10.2'
    implementation platform('com.google.firebase:firebase-bom:28.3.0')
    implementation 'com.google.firebase:firebase-config-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'

    // Dependencies for local unit tests
    testImplementation "junit:junit:$junitVersion"
    testImplementation "org.mockito:mockito-core:$mockitoVersion"

    testImplementation "androidx.arch.core:core-testing:$archLifecycleVersion"
    // mock extension function
    testImplementation "io.mockk:mockk:$mockkVersion"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"

    // Android Testing Support Library's runner and rules
    androidTestImplementation "com.android.support.test:runner:$runnerVersion"
    androidTestImplementation "com.android.support.test:rules:$rulesVersion"

    // Espresso UI Testing
    androidTestImplementation "com.android.support.test.espresso:espresso-core:$espressoVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-contrib:$espressoVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-intents:$espressoVersion"
    androidTestImplementation "com.jakewharton.espresso:okhttp3-idling-resource:$okhttpIdlingResourceVersion"
    implementation "com.android.support.test.espresso:espresso-idling-resource:$espressoVersion"
}

def getProperty(String filename, String propName) {
    def propsFile = rootProject.file(filename)
    if (propsFile.exists()) {
        def props = new Properties()
        props.load(new FileInputStream(propsFile))
        if (props[propName] != null) {
            return props[propName]
        } else {
            print("No such property " + propName + " in file " + filename)
        }
    } else {
        print(filename + " does not exist!")
    }
}
