buildscript {
    repositories {
        mavenCentral()
        google()
        maven { url = uri("https://developer.huawei.com/repo/") }
        maven {
            url = uri("https://api.mapbox.com/downloads/v2/releases/maven")
            credentials {
                username = "mapbox"
                password = "sk.eyJ1IjoicndhaWQiLCJhIjoiY2x6NWo0bTMzMWZodzJrcGZpemYzc3Z4dSJ9.uSSZuwNSGCcCdPAiORECmg"
                if (password.isNullOrEmpty()) {
                    throw GradleException("MAPBOX_DOWNLOADS_TOKEN isn't set. Set it to the project properties or to the environment variables.")
                }
            }
            authentication {
                create<BasicAuthentication>("basic")
            }
        }
    }
    dependencies {
        classpath("com.android.tools.build:gradle:8.1.0")
        classpath("com.huawei.agconnect:agcp:1.9.1.304")
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url = uri("https://developer.huawei.com/repo/") }
        maven { url = uri("https://artifactory.ess-dev.com/artifactory/gradle-dev-local") }
        maven {
            url = uri("https://api.mapbox.com/downloads/v2/releases/maven")
            credentials {
                username = "mapbox"
                password = "sk.eyJ1IjoicndhaWQiLCJhIjoiY2x6NWo0bTMzMWZodzJrcGZpemYzc3Z4dSJ9.uSSZuwNSGCcCdPAiORECmg"
                if (password.isNullOrEmpty()) {
                    throw GradleException("MAPBOX_DOWNLOADS_TOKEN isn't set. Set it to the project properties or to the environment variables.")
                }
            }
            authentication {
                create<BasicAuthentication>("basic")
            }
        }
    }

    configurations.all {
        exclude(group = "org.bouncycastle", module = "bcprov-jdk16")
    }

    tasks.withType<JavaCompile>().configureEach {
        sourceCompatibility = JavaVersion.VERSION_21.toString()
        targetCompatibility = JavaVersion.VERSION_21.toString()
    }

    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
        kotlinOptions {
            jvmTarget = "21"
        }
    }

    subprojects {
        afterEvaluate {
            if (project.hasProperty("android")) {
                project.extensions.findByName("android")?.let { androidExt ->
                    val android = androidExt as com.android.build.gradle.BaseExtension
                    if (android.namespace == null) {
                        android.namespace = project.group.toString()
                    }
                    android.buildFeatures.apply {
                        if (buildConfig == null) {
                            buildConfig = true
                        }
                    }
                    android.compileOptions.apply {
                        sourceCompatibility = JavaVersion.VERSION_21
                        targetCompatibility = JavaVersion.VERSION_21
                    }
                }
            }
            project.tasks.withType<JavaCompile>().configureEach {
                sourceCompatibility = JavaVersion.VERSION_21.toString()
                targetCompatibility = JavaVersion.VERSION_21.toString()
            }
            project.tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
                kotlinOptions {
                    jvmTarget = "21"
                }
            }
        }
    }
}

rootProject.buildDir = File("../build")
subprojects {
    project.buildDir = File("${rootProject.buildDir}/${project.name}")
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register<Delete>("clean") {
    delete(rootProject.layout.buildDirectory)
}