package io.nexilis

import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.android.build.gradle.internal.dsl.DynamicFeatureExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.ProjectEvaluationListener
import org.gradle.api.ProjectState

const val libNameSpace = "com.example.dm"

class DynamicModulePlugin : Plugin<Project> {
    var mainProjectName: String = ""
    override fun apply(target: Project) {
        target.gradle.addProjectEvaluationListener(object : ProjectEvaluationListener {
            override fun beforeEvaluate(project: Project) {
                println(">>>>>>>>>>> beforeEvaluate:${project.name}")
            }

            override fun afterEvaluate(project: Project, state: ProjectState) {
                println(">>>>>>>>>>> afterEvaluate:${project.name}")
                project.plugins.withId("com.android.application") {
                    val android = project.extensions.getByType(BaseAppModuleExtension::class.java)
                    android.sourceSets.maybeCreate("main")
                    android.sourceSets.getByName("main") { sourceSet ->
                        sourceSet.java.setSrcDirs(listOf("src/custom/java"))
                        println(">>>>>>>>>>> java:setSrcDirs:${project.projectDir}")
                    }
                    android.dynamicFeatures.add(":dynamic_core")
                    println(">>>>>>>>>>> dynamicFeature:add:dynamic_core")
                }
                project.plugins.withId("com.android.application") {
                    mainProjectName = project.name
                    project.dependencies.add("implementation", "io.nexilis:ui-api-dm:5.0.12")
                    println(">>>>>>>>>>> implementation:io.nexilis:ui-api-dm")
                    project.dependencies.add("implementation", "io.nexilis:ss:5.0.12")
                    println(">>>>>>>>>>> implementation:io.nexilis:ss")
                }
                if (project.name.equals("dynamic_core")) {
                    val android = project.extensions.getByType(DynamicFeatureExtension::class.java)
                    android.namespace = libNameSpace
                    android.defaultConfig.applicationId = null
                    android.buildTypes.forEach {
                        it.isMinifyEnabled = false
                    }
                    println(">>>>>>>>>>> set:minifyEnabled:false")
                    project.dependencies.add(
                        "implementation",
                        project.project(":$mainProjectName")
                    )
                    println(">>>>>>>>>>> implementation:$mainProjectName")
                    project.dependencies.add("implementation", "io.nexilis:ui-api:5.0.12")
                    println(">>>>>>>>>>> implementation:io.nexilis:ui-api:5.0.12")
                    project.dependencies.add("implementation", "io.nexilis:uc:5.0.12")
                    println(">>>>>>>>>>> implementation:io.nexilis:uc:5.0.12")
                }
            }
        })
    }

}
