import org.apache.tools.ant.taskdefs.condition.Os

/*
 * Copyright (C) 2017 Emmanuel Kehinde
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id "org.jlleitschuh.gradle.ktlint" version "9.4.1"
    id "com.diffplug.gradle.spotless"
}

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.emmanuelkehinde.twittasave"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 2
        versionName "2.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_7
        sourceCompatibility JavaVersion.VERSION_1_7
    }

    flavorDimensions "mock"

    productFlavors {
        mock {
            dimension "mock"
            applicationIdSuffix = ".mock"
            buildConfigField "String", "CONSUMER_KEY", "\"mock_key\""
            buildConfigField "String", "CONSUMER_SECRET", "\"mock_secret\""
        }
        prod {
            Properties properties = new Properties()
            properties.load(project.rootProject.file("local.properties").newDataInputStream())

            buildConfigField "String", "CONSUMER_KEY", properties.getProperty("consumer.key", "")
            buildConfigField "String", "CONSUMER_SECRET", properties.getProperty("consumer.secret", "")
        }
    }
}

dependencies {
    implementation project(path: ":shared")
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.google.firebase:firebase-messaging:20.1.3'
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'com.github.esafirm:RxDownloader:1.0.1'
    testImplementation 'junit:junit:4.13.2'
    implementation 'com.android.support:multidex:1.0.3'
}

apply plugin: 'com.google.gms.google-services'

task installGitHook(type: Copy) {
    def suffix = "macos"
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        suffix = "windows"
    }
    from new File(rootProject.rootDir, "automation/scripts/pre-commit-$suffix")
    into { new File(rootProject.rootDir, '.git/hooks') }
    rename("pre-commit-$suffix", 'pre-commit')
    fileMode 0775
}

tasks.getByPath(':androidApp:preBuild').dependsOn installGitHook

def build_param = "${build}"
if (build_param == "devCI") {
    // Exclude all except mockDebug flavor
    android.variantFilter { variant ->
        if (variant.getFlavors().get(0).name != 'mock' || variant.buildType.name != 'debug') {
            variant.setIgnore(true)
        }
    }
} else if (build_param == "releaseCI") {
    // Exclude all except prodRelease flavor
    android.variantFilter { variant ->
        if (variant.getFlavors().get(0).name != 'prod' || variant.buildType.name != 'release') {
            variant.setIgnore(true)
        }
    }
} else {
    // Normal local machine dev
    // Exclude mockRelease as it's not needed.
    android.variantFilter { variant ->
        if(variant.buildType.name == 'release' && variant.getFlavors().get(0).name == 'mock') {
            variant.setIgnore(true)
        }
    }
}
