allprojects {
    repositories {
        flatDir {
            dirs(
                file("${project(":unityLibrary").projectDir}/libs")
            )
        }

        google()
        mavenCentral()
    }
}

val newBuildDir: Directory = rootProject.layout.buildDirectory.dir("../../build").get()
rootProject.layout.buildDirectory.value(newBuildDir)

subprojects {
    val newSubprojectBuildDir: Directory = newBuildDir.dir(project.name)
    project.layout.buildDirectory.value(newSubprojectBuildDir)
}

subprojects {
    afterEvaluate {
        if (project.extensions.findByName("android") != null) {
            extensions.configure<com.android.build.gradle.BaseExtension>("android") {
                if (namespace == null) {
                    namespace = project.group.toString()
                }
            }
        }

        if (plugins.hasPlugin("com.android.application") || plugins.hasPlugin("com.android.library")) {
            if (name == "flutter_unity_widget") {
                extensions.configure<com.android.build.gradle.BaseExtension>("android") {
                    compileOptions {
                        sourceCompatibility = JavaVersion.VERSION_17
                        targetCompatibility = JavaVersion.VERSION_17
                    }
                    (this as ExtensionAware).extensions.configure<org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions>("kotlinOptions") {
                        jvmTarget = "17"
                    }

                    if (namespace == null) {
                        namespace = project.group.toString()
                    }

                    compileSdkVersion(35)
                }
            }
        }
    }
}

subprojects {
    project.evaluationDependsOn(":app")
}

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