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"
        }
    } else {
        dependencies {
            classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.8.9"
        }
    }
}

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
    id "app.cash.licensee" version "1.13.0" apply false
}

def testDebugAll = tasks.create("testDebugAll")

//https://www.droidcon.com/2022/03/11/reducing-gradle-boilerplate-in-multi-module-android-projects/
subprojects { sub ->
    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)
                }
            }
        }

    }
    
    if (rootProject.ext.onCi) {
        plugins.withId("com.android.application") {
            // Apply the plugin after Android plugin is ready
            sub.pluginManager.apply("app.cash.licensee")
            sub.afterEvaluate {
                // Configure licensee
                sub.licensee {
                    // Allow common SPDX identifiers
                    allow("Apache-2.0")
                    allow("MIT")
                    allow("BSD-3-Clause")

                    // Allow specific license URLs
                    allowUrl("https://developer.android.com/studio/terms.html") {
                        because("Google/Android SDK Terms - standard for Google Play Services and Android libraries")
                    }
                    allowUrl("https://github.com/facebook/shimmer-android/blob/master/LICENSE") {
                        because("Facebook Shimmer is BSD-licensed")
                    }
                    allowUrl("https://www.bouncycastle.org/licence.html") {
                        because("BouncyCastle is MIT-licensed")
                    }
                    
                    // Allow specific dependencies that declare no licenses
                    allowDependency("com.github.anyproto", "any-crypto-kotlin", "1.0.2") {
                        because("any-crypto-kotlin is MIT-licensed (verified in repository)")
                    }
                    allowDependency("com.github.komputing", "KBase58", "0.4") {
                        because("KBase58 is MIT-licensed (verified in repository)")
                    }
                    allowDependency("io.anyproto", "anytype-heart-android", "v0.43.0-rc02") {
                        because("Internal Anytype library")
                    }
                }
            }
        }
    }

    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"))