/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.kotlin.dsl.integration

import org.gradle.integtests.fixtures.RepoScriptBlockUtil.mavenCentralRepository
import org.gradle.kotlin.dsl.fixtures.AbstractKotlinIntegrationTest
import org.gradle.test.fixtures.dsl.GradleDsl.KOTLIN
import org.gradle.util.GradleVersion
import org.junit.Test
import spock.lang.Issue


class GradleKotlinDslRegressionsTest : AbstractKotlinIntegrationTest() {

    @Test
    @Issue("https://github.com/gradle/gradle/issues/9919")
    fun `gradleKotlinDsl dependency declaration does not throw`() {

        withBuildScript(
            """
            plugins { java }
            dependencies {
                compileOnly(gradleKotlinDsl())
            }
            """
        )

        build("help")
    }

    @Test
    @Issue("https://youtrack.jetbrains.com/issue/KT-44303")
    fun `can configure ext extension`() {
        withBuildScript(
            """
            ext {
                set("foo", "bar")
            }
            """
        )

        build("help")
    }

    @Test
    @Issue("https://youtrack.jetbrains.com/issue/KT-55068")
    fun `kotlin ir backend issue kt-55068`() {

        withKotlinBuildSrc()
        withFile("buildSrc/src/main/kotlin/my-plugin.gradle.kts", """
            data class Container(val property: Property<String> = objects.property())
        """)
        withBuildScript("""plugins { id("my-plugin") }""")

        build("help")
    }

    @Test
    @Issue("https://youtrack.jetbrains.com/issue/KT-55065")
    fun `kotlin ir backend issue kt-55065`() {

        withKotlinBuildSrc()
        withFile("buildSrc/src/main/kotlin/my-plugin.gradle.kts", """
            @file:Suppress("DEPRECATION")

            tasks.withType<DefaultTask>().configureEach {
                val p: String by project
            }
        """)
        withBuildScript("""plugins { id("my-plugin") }""")

        executer.expectDocumentedDeprecationWarning(
            "The 'val name: Type by project' property delegate syntax has been deprecated. " +
                "This is scheduled to be removed in Gradle 10. " +
                "Use 'val property = project.property(name)' instead. " +
                "Consult the upgrading guide for further information: " +
                "https://docs.gradle.org/${GradleVersion.current().version}/userguide/upgrading_version_9.html#kotlin_dsl_delegated_properties"
        )

        build("help")
    }

    @Test
    @Issue("https://youtrack.jetbrains.com/issue/KT-55542")
    fun `nullable type parameters on non-nullable member works without disabling Koltlin type enhancement improvements in strict mode`() {
        withBuildScript("""
            import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

            plugins { `embedded-kotlin` }
            $repositoriesBlock
            dependencies {
                implementation(gradleKotlinDsl())
            }
            tasks.withType<KotlinCompile>().configureEach {
                // Work around JVM validation issue: https://youtrack.jetbrains.com/issue/KT-66919
                jvmTargetValidationMode = org.jetbrains.kotlin.gradle.dsl.jvm.JvmTargetValidationMode.WARNING
                compilerOptions.freeCompilerArgs.addAll("-Xjsr305=strict", "-Xjspecify-annotations=strict")
            }
        """)

        withFile("src/main/kotlin/code.kt", """
            import org.gradle.api.*

            class MyPlugin : Plugin<Project> {
                override fun apply(project: Project): Unit = project.run {
                    provider { "thing" }.map { null }
                }
            }
        """)

        build("classes")
    }

    @Test
    @Issue("https://github.com/gradle/gradle/issues/8423")
    fun `non-static inner class for component metadata rule fails with a reasonable error message`() {
        withBuildScript("""
            plugins {
                id("java")
            }
            ${mavenCentralRepository(KOTLIN)}
            dependencies {
               components.all(FixOksocialOutput::class.java)
               implementation("com.baulsupp:oksocial-output:4.19.0")
            }
            open class FixOksocialOutput: ComponentMetadataRule {
               override fun execute(context: ComponentMetadataContext) = context.details.run {
                  if (id.group == "com.baulsupp" && id.name == "oksocial-output") {
                      allVariants {
                         withDependencies {
                            removeAll { name == "jackson-bom" }
                         }
                      }
                  }
               }
            }
        """)
        withFile("src/main/java/Main.java", "public class Main {}")

        buildAndFail("compileJava").apply {
            assertHasCause("Could not create an instance of type Build_gradle${'$'}FixOksocialOutput.")
            assertHasCause("Class Build_gradle.FixOksocialOutput is a non-static inner class.")
        }
    }

    @Test
    @Issue("https://youtrack.jetbrains.com/issue/KT-55880")
    @Issue("https://github.com/gradle/gradle/issues/23491")
    fun `compiling standalone scripts does not emit a warning at info level`() {
        withBuildScript("""println("test")""")
        build("help", "--info").apply {
            assertNotOutput("is not supposed to be used along with regular Kotlin sources, and will be ignored in the future versions by default. (Use -Xallow-any-scripts-in-source-roots command line option to opt-in for the old behavior.)")
        }
    }

    @Test
    @Issue("https://github.com/gradle/gradle/issues/24481")
    fun `applied project scripts don't have project accessors`() {
        withFile("applied.gradle.kts", """
            println(java.sourceCompatibility)
        """)
        withBuildScript("""
            plugins { java }
            apply(from = "applied.gradle.kts")
        """)
        buildAndFail("help").apply {
            assertHasErrorOutput("Unresolved reference 'sourceCompatibility'.")
        }

        withFile("applied.gradle.kts", """
            buildscript {
                dependencies {}
            }
            println(java.sourceCompatibility)
        """)
        buildAndFail("help").apply {
            assertHasErrorOutput("Unresolved reference 'sourceCompatibility'.")
        }
    }
}
