import com.android.build.gradle.LibraryPlugin
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper

buildscript {
    ext.compile_sdk = 35
    ext.target_sdk = 35
    ext.min_sdk = 26

    ext.application_id = 'io.anytype.app'
    ext.version_name = '1.0'
    ext.version_code = 1
    ext.test_runner = 'androidx.test.runner.AndroidJUnitRunner'
    ext.onCi = project.properties.getOrDefault("com.anytype.ci", "false").toBoolean()

    def githubProperties = new Properties()
    def githubPropertiesFile = file("$rootDir${File.separator}github.properties")
    githubProperties.load(new FileInputStream(githubPropertiesFile))

    def apiKeysProperties = new Properties()
    def apiKeysPropertiesFile = file("$rootDir${File.separator}apikeys.properties")
    apiKeysProperties.load(new FileInputStream(apiKeysPropertiesFile))

    ext.sentryApiKey = apiKeysProperties["sentry_dsn"]

    repositories {
        mavenLocal()
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }

    if (ext.onCi) {
        dependencies {
            classpath "com.github.openkin:license-gradle-plugin:0d9582e233"
        }
    }
}

plugins {
    alias(libs.plugins.application) apply false
    alias(libs.plugins.library) apply false
    alias(libs.plugins.kotlinAndroid) apply false
    alias(libs.plugins.compose.compiler) apply false
    alias(libs.plugins.kserialization) apply false
    alias(libs.plugins.wire) apply false
    alias(libs.plugins.dokka) apply false
    alias(libs.plugins.firebaseDistribution) apply false
    alias(libs.plugins.gms) apply false
    alias(libs.plugins.ksp) apply false
}

def testDebugAll = tasks.create("testDebugAll")

//https://www.droidcon.com/2022/03/11/reducing-gradle-boilerplate-in-multi-module-android-projects/
subprojects {
    project.plugins.whenPluginAdded { plugin ->
        if (plugin instanceof LibraryPlugin) {

            android {
                def config = rootProject.extensions.getByName("ext")
                compileSdkVersion config["compile_sdk"]

                defaultConfig {
                    minSdkVersion config["min_sdk"]
                    targetSdkVersion config["target_sdk"]
                    testInstrumentationRunner config["test_runner"]
                }

                testOptions {
                    unitTests {
                        includeAndroidResources = true
                    }
                }
            }
        }
        if (plugin instanceof KotlinAndroidPluginWrapper) {
            android {
                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_17
                    targetCompatibility JavaVersion.VERSION_17
                }

                kotlin {
                    jvmToolchain(17)
                }
            }
        }
    }

    plugins.withId("kotlin") {
        testDebugAll.dependsOn(tasks.named("test"))
    }
    plugins.withId("com.android.library") {
        project.apply from: "$rootDir/lint.gradle"
        afterEvaluate {
            testDebugAll.dependsOn(tasks.named("testDebugUnitTest"))
        }
    }
    plugins.withId("com.android.application") {
        project.apply from: "$rootDir/lint.gradle"
        afterEvaluate {
            testDebugAll.dependsOn(tasks.named("testDebugUnitTest"))
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

tasks.register("installGitHooks") {
    setGroup("Build Setup")
    setDescription("Install local repository git hooks")
    exec {
        commandLine 'sh', '-c', 'git config core.hooksPath .githooks'
    }
}

var initialTaskNames = getProject().getGradle().getStartParameter().getTaskNames()
getProject().getGradle().getStartParameter().setTaskNames(
        initialTaskNames + Collections.singletonList("installGitHooks"))