plugins {
    id "com.diffplug.gradle.spotless" version "4.5.1"
    id "com.github.b3er.local.properties" version "1.1"
}

def rootConfiguration = {}
apply from: project(':app').file('config.gradle'), to: rootConfiguration

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

def YOUTUBE_API_KEY = System.getenv('YOUTUBE_API_KEY') ?: "YOUR_API_KEY"
//noinspection SpellCheckingInspection
def MAPBOX_API_KEY = '"'+System.getenv('MAPBOX_API_KEY')+'"' ?: '"DEFAULT"'

def LOCAL_KEY_PRESENT = project.hasProperty('SIGNING_KEY_FILE') && rootProject.file(SIGNING_KEY_FILE).exists()

android {
    compileSdkVersion rootConfiguration.compileSdkVersion

    defaultConfig {
        applicationId "ai.susi"
        minSdkVersion rootConfiguration.minSdkVersion
        targetSdkVersion rootConfiguration.targetSdkVersion
        versionCode 18
        versionName "1.3.0"
        vectorDrawables.useSupportLibrary = true
        // for unit test
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        manifestPlaceholders = [
                YOUTUBE_API_KEY: YOUTUBE_API_KEY
        ]
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        if (CIRCLE_BUILD) {
            release {
                storeFile KEYSTORE_FILE
                storePassword System.getenv("STORE_PASS")
                keyAlias System.getenv("ALIAS")
                keyPassword System.getenv("KEY_PASS")
            }
        } else if (LOCAL_KEY_PRESENT) {
            release {
                storeFile rootProject.file(SIGNING_KEY_FILE)
                storePassword STORE_PASS
                keyAlias ALIAS
                keyPassword KEY_PASS
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "String", "SPEAKER_BASE_URL", '"http://10.0.0.1:5000"'
            buildConfigField "String", "MAPBOX_API_KEY", MAPBOX_API_KEY

            if (LOCAL_KEY_PRESENT || CIRCLE_BUILD)
                signingConfig signingConfigs.release
        }

        debug {
            buildConfigField "String", "SPEAKER_BASE_URL", '"http://10.0.0.1:5000"'
            buildConfigField "String", "MAPBOX_API_KEY", MAPBOX_API_KEY
        }
    }

    flavorDimensions "default"

    productFlavors {
        fdroid {
            dimension "default"
        }

        playStore {
            dimension "default"
        }
    }

    lintOptions {
        abortOnError false
        checkReleaseBuilds false
        disable 'InvalidPackage'
    }

    testOptions {
        unitTests {
            returnDefaultValues = true
            includeAndroidResources = true
        }
    }
}

spotless {
    kotlin {
        ktlint()
        target '**/*.kt'
    }
}

repositories {
    maven { url "https://jitpack.io" }
    jcenter()
}

androidExtensions {
    experimental = true
}

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

    implementation "com.android.support:appcompat-v7:${rootConfiguration.supportLibraryVersion}"
    implementation "com.android.support:exifinterface:${rootConfiguration.supportLibraryVersion}"
    implementation "com.android.support:recyclerview-v7:${rootConfiguration.supportLibraryVersion}"
    implementation "com.android.support:design:${rootConfiguration.supportLibraryVersion}"
    implementation "com.android.support:cardview-v7:${rootConfiguration.supportLibraryVersion}"
    implementation "com.android.support:support-v4:${rootConfiguration.supportLibraryVersion}"
    implementation "com.android.support:customtabs:${rootConfiguration.supportLibraryVersion}"
    implementation 'com.android.support.constraint:constraint-layout:2.0.4'

    //piechart
    implementation "com.github.PhilJay:MPAndroidChart:${rootConfiguration.mpAndroidChartVersion}"

    //Leak Canary
    debugImplementation "com.squareup.leakcanary:leakcanary-android:${rootConfiguration.leakCanaryVersion}"
    releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:${rootConfiguration.leakCanaryVersion}"
    testImplementation "com.squareup.leakcanary:leakcanary-android-no-op:${rootConfiguration.leakCanaryVersion}"

    //butterknife
    implementation "com.jakewharton:butterknife:${rootConfiguration.butterKnifeVersion}"
    kapt "com.jakewharton:butterknife-compiler:${rootConfiguration.butterKnifeVersion}"

    //koin
    implementation "org.koin:koin-android-scope:${rootConfiguration.koinVersion}"
    implementation "org.koin:koin-android:${rootConfiguration.koinVersion}"
    testImplementation "org.koin:koin-test:${rootConfiguration.koinVersion}"

    //For Picasso
    implementation "com.squareup.picasso:picasso:${rootConfiguration.picassoVersion}"

    //Retrofit
    implementation "com.squareup.retrofit2:retrofit:${rootConfiguration.retrofitVersion}"
    implementation "com.squareup.retrofit2:converter-gson:${rootConfiguration.retrofitVersion}"
    implementation "com.squareup.okhttp3:logging-interceptor:${rootConfiguration.okHttpVersion}"

    //For Link Previews
    implementation "org.jsoup:jsoup:${rootConfiguration.jsoupVersion}" // required
    implementation "io.github.ponnamkarthik:richlinkpreview:${rootConfiguration.linkPreviewVersion}"

    //preference UI
    implementation "com.takisoft.fix:preference-v7:${rootConfiguration.preferenceVersion}"

    //Unit Test
    testImplementation "junit:junit:${rootConfiguration.jUnitVersion}"
    testImplementation "org.mockito:mockito-core:${rootConfiguration.mockitoCoreVersion}"
    androidTestImplementation "com.android.support:support-annotations:${rootConfiguration.supportLibraryVersion}"
    androidTestImplementation "com.android.support.test:runner:${rootConfiguration.testVersion}"
    androidTestImplementation "com.android.support.test:rules:${rootConfiguration.testVersion}"
    androidTestImplementation "com.android.support.test.espresso:espresso-core:${rootConfiguration.espressoVersion}"

    //realm adapter
    implementation "io.realm:android-adapters:${rootConfiguration.realmVersion}"

    //waiting dots
    implementation "com.github.tajchert:WaitingDots:${rootConfiguration.waitingDotsVersion}"

    //Youtube API
    playStoreImplementation files('libs/YouTubeAndroidPlayerApi.jar')

    //Timber
    implementation "com.jakewharton.timber:timber:${rootConfiguration.timberVersion}"

    //Shimmer
    implementation "com.facebook.shimmer:shimmer:${rootConfiguration.shimmerVersion}"

    //Smart LOck authentication
    implementation "com.google.android.gms:play-services-auth:${rootConfiguration.playServiceAuthVersion}"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${rootConfiguration.kotlinVersion}"

    //Stetho
    debugImplementation "com.facebook.stetho:stetho:${rootConfiguration.stethoVersion}"

    //Stetho network helper
    debugImplementation "com.facebook.stetho:stetho-okhttp3:${rootConfiguration.stethoVersion}"

    releaseImplementation "com.github.iamareebjamal:stetho-noop:1.2.1"
}
