import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.File

class ComposeConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            extensions.findByType<ApplicationExtension>()?.run {
                buildFeatures {
                    compose = true
                }

                composeOptions {
                    kotlinCompilerExtensionVersion = "1.4.7"
                }

                dependencies {
                    val platformBom = "androidx.compose:compose-bom:2023.05.01"
                    add("implementation", platform(platformBom))
                    add("implementation", "androidx.compose.ui:ui")
                    add("implementation", "androidx.compose.runtime:runtime")
                    add("implementation", "androidx.compose.foundation:foundation")
                    add("implementation", "androidx.compose.material3:material3")
                    add("implementation", "androidx.compose.ui:ui-tooling-preview")
                }
            }

            extensions.findByType<LibraryExtension>()?.run {
                buildFeatures {
                    compose = true
                }

                composeOptions {
                    kotlinCompilerExtensionVersion = "1.4.7"
                }

                dependencies {
                    val platformBom = "androidx.compose:compose-bom:2023.05.01"
                    add("implementation", platform(platformBom))
                    add("implementation", "androidx.compose.ui:ui")
                    add("implementation", "androidx.compose.runtime:runtime")
                    add("implementation", "androidx.compose.foundation:foundation")
                    add("implementation", "androidx.compose.material3:material3")
                    add("implementation", "androidx.compose.ui:ui-tooling-preview")
                }
            }

            with(tasks) {
                withType<KotlinCompile>().configureEach {
                    kotlinOptions {
                        freeCompilerArgs = freeCompilerArgs + buildComposeMetricsParameters()
                    }
                }
            }
        }
    }
}

private fun Project.buildComposeMetricsParameters(): List<String> {
    val metricParameters = mutableListOf<String>()
    val enableMetricsProvider = project.providers.gradleProperty("enableComposeCompilerMetrics")
    val enableMetrics = (enableMetricsProvider.orNull == "true")
    if (enableMetrics) {
        val metricsFolder = File(project.buildDir, "compose-metrics")
        metricParameters.add("-P")
        metricParameters.add(
            "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" + metricsFolder.absolutePath
        )
    }

    val enableReportsProvider = project.providers.gradleProperty("enableComposeCompilerReports")
    val enableReports = (enableReportsProvider.orNull == "true")
    if (enableReports) {
        val reportsFolder = File(project.buildDir, "compose-reports")
        metricParameters.add("-P")
        metricParameters.add(
            "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" + reportsFolder.absolutePath
        )
    }
    return metricParameters.toList()
}
