/*
 * (C) Copyright IBM Deutschland GmbH 2021
 * (C) Copyright IBM Corp. 2021
 */

class Config {
    Integer minVersion = null
    Integer targetVersion = null
    Integer compileSdkVersion = null
    List<String> disableAndroidLints = []
    Boolean kotlinExplicitApiMode = true
    List<String> kotlinCompilerArgs = []
    String javaVersion = JavaVersion.VERSION_11
}

ext.androidCommon = { args = [:] ->
    apply from: "$rootDir/gradle/common/kotlin-common.gradle"

    Config config = new Config(args)

    int minVersion = config.minVersion ?: 23
    int targetVersion = config.targetVersion ?: 34
    int compileVersion = config.compileSdkVersion ?: 34

    kotlinCommon()

    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-parcelize'

    android {
        compileSdkVersion compileVersion

        compileOptions {
            coreLibraryDesugaringEnabled true

            sourceCompatibility = config.javaVersion
            targetCompatibility = config.javaVersion
        }

        kotlinOptions {
            jvmTarget = config.javaVersion.toString()
            freeCompilerArgs = getCompilerArgs(
                kotlinExplicitApiMode: config.kotlinExplicitApiMode,
                kotlinCompilerArgs: config.kotlinCompilerArgs,
            )
        }

        defaultConfig {
            minSdkVersion minVersion
            targetSdkVersion targetVersion
            versionCode 1
            versionName version
            buildConfigField("String", "MODULE_VERSION", "\"$version\"")

            vectorDrawables.useSupportLibrary = true
            multiDexEnabled true

            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }

        lintOptions {
            disable(*config.disableAndroidLints)
            checkGeneratedSources false
            abortOnError true
            warningsAsErrors true
            textReport true
            textOutput 'stdout'
            lintConfig file("$rootDir/gradle/common/lint.xml")
        }

        testOptions {
            // Needed for Robolectric
            unitTests {
                includeAndroidResources = true
            }
        }

        packagingOptions {
            pickFirst 'META-INF/*.kotlin_module'
            pickFirst 'META-INF/mailcap'
        }
    }

    dependencies {
        coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
    }
}
