ext.configureAndroidLibrary = { project ->
    project.apply plugin: 'com.android.library'
    project.apply plugin: 'kotlin-android'
    project.apply plugin: 'kotlin-parcelize'
    configureAndroidModule(project)
}

ext.configureAndroidApp = { project ->
    project.apply plugin: 'com.android.application'
    project.apply plugin: 'kotlin-android'
    project.apply plugin: 'kotlin-parcelize'
    configureAndroidModule(project)
}

ext.configureDetektForAllSubprojects = { Project project ->
    project.subprojects {
        afterEvaluate { subProject ->
            if (subProject.plugins.hasPlugin("kotlin") || subProject.plugins.hasPlugin("kotlin-android")) {
                subProject.apply plugin: "io.gitlab.arturbosch.detekt"

                subProject.detekt {
                    parallel = true
                    input = files("${subProject.projectDir}/src/")
                    config = files("${project.rootDir}/detekt.yml")

                    def baselineFile = file("${subProject.projectDir}/detekt-baseline.xml")
                    baseline = baselineFile
                }
            }
        }
    }
}

ext.junitTestImplementation = { it ->
    it.dependencies {
        testImplementation deps.test.junitApi
        testImplementation deps.test.junitParams
        testRuntimeOnly deps.test.junitEngine
        testImplementation deps.test.junitVintage
        testImplementation project(':internal:junitextensions')
    }
}

ext.junitAndroidTestImplementation = { project ->
    project.dependencies {
        androidTestImplementation deps.test.junit4
    }
}

ext.junitImplementation = { project ->
    project.dependencies {
        implementation deps.test.junitApi
        implementation deps.test.junitParams
        implementation deps.test.junitEngine
        implementation deps.test.junitVintage
    }
}

private def configureAndroidModule(Project project) {
    project.android {
        compileSdk deps.build.compileSdk

        defaultConfig {
            minSdk deps.build.minSdk
            targetSdk deps.build.targetSdk
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }

        testOptions {
            animationsDisabled true
            unitTests {
                includeAndroidResources = true
                all {
                    useJUnitPlatform()
                    systemProperty 'junit.jupiter.extensions.autodetection.enabled', 'true'
                }
            }
        }

        compileOptions {
            sourceCompatibility = JavaVersion.VERSION_11
            targetCompatibility = JavaVersion.VERSION_11
        }

        project.afterEvaluate {
            project.android.kotlinOptions.freeCompilerArgs += "-Xopt-in=com.badoo.ribs.annotation.ExperimentalApi"
        }
    }
}
