plugins {
    id 'base'
}

final String BUILD_DIRECTORY = "${Project.DEFAULT_BUILD_DIR_NAME}"

version = htmlSanityCheckVersion

final String INTEGRATION_TEST_DIRECTORY_COMMON = "common"
final String INTEGRATION_TEST_DIRECTORY_COMMON_BUILD = "common/${BUILD_DIRECTORY}"

tasks.register("cleanIntegrationTestCommon", Delete) {
    group("Build")
    description("Deletes the common data for integration tests")

    def filesToDelete
            = files(INTEGRATION_TEST_DIRECTORY_COMMON_BUILD)

    delete(filesToDelete)

    outputs.upToDateWhen {
        !filesToDelete.files.any { it.exists() }
    }
}
tasks.register("integrationTestPrepare") {
    group("Verification")
    description("Prepare integration tests data")

    mustRunAfter(cleanIntegrationTestCommon)

    doLast {
        def result = exec {
            workingDir INTEGRATION_TEST_DIRECTORY_COMMON
            if (System.getProperty("os.name") ==~ /Windows.*/) {
                commandLine('cmd', '/c', '..\\..\\gradlew.bat', "clean", "buildReadmeAll")
            } else {
                commandLine("../../gradlew", "clean", "buildReadmeAll")
            }
        }
        logger.debug "Script output: $result"
    }
}

final String INTEGRATION_TEST_DIRECTORY_GRADLE_PLUGIN = "gradle-plugin"
final String INTEGRATION_TEST_DIRECTORY_GRADLE_PLUGIN_BUILD = "gradle-plugin/${BUILD_DIRECTORY}"

tasks.register("cleanIntegrationTestGradlePlugin", Delete) {
    group("Build")
    description("Deletes the result directory from gradle-plugin integration tests")

    def filesToDelete
            = files(INTEGRATION_TEST_DIRECTORY_GRADLE_PLUGIN_BUILD)

    delete(filesToDelete)

    outputs.upToDateWhen {
        !filesToDelete.files.any { it.exists() }
    }
}
tasks.register("integrationTestGradlePlugin") {
    group("Verification")
    description("Run gradle-plugin integration tests (only)")

    final String buildReportsDirectory = "${INTEGRATION_TEST_DIRECTORY_GRADLE_PLUGIN_BUILD}/reports"
    final File testIndex = new File(file(buildReportsDirectory), "index.html")
    outputs.file testIndex

    dependsOn(integrationTestPrepare)
    mustRunAfter(cleanIntegrationTestGradlePlugin)

    doLast {
        def result = exec {
            workingDir INTEGRATION_TEST_DIRECTORY_GRADLE_PLUGIN
            if (System.getProperty("os.name") ==~ /Windows.*/) {
                commandLine('cmd', '/c', '..\\..\\gradlew.bat', "clean", "htmlSanityCheck", "-PhtmlSanityCheckVersion=${project.version}")
            } else {
                commandLine("../../gradlew", "clean", "htmlSanityCheck", "-PhtmlSanityCheckVersion=${project.version}")
            }
        }
        logger.debug "Script output: $result"
        assert testIndex.exists()
    }
}

final String INTEGRATION_TEST_DIRECTORY_CLI = "cli"
final String INTEGRATION_TEST_DIRECTORY_CLI_BUILD = "${INTEGRATION_TEST_DIRECTORY_CLI}/${BUILD_DIRECTORY}"

tasks.register("cleanIntegrationTestCli", Delete) {
    group("Build")
    description("Deletes the result directory from CLI integration tests")

    def filesToDelete
            = files(INTEGRATION_TEST_DIRECTORY_CLI_BUILD)

    delete(filesToDelete)

    outputs.upToDateWhen {
        !filesToDelete.files.any { it.exists() }
    }
}
tasks.register("integrationTestCli") {
    group("Verification")
    description("Run CLI integration tests (only)")

    final File buildReportsDirectory = file("${INTEGRATION_TEST_DIRECTORY_CLI_BUILD}/reports")
    final File testIndex = new File(buildReportsDirectory, "index.html")

    outputs.file testIndex

    dependsOn(integrationTestPrepare)
    mustRunAfter(cleanIntegrationTestCli)

    doLast {
        def result = exec {
            buildReportsDirectory.mkdirs()
            final String hscScriptFileName = "../htmlSanityCheck-cli/${BUILD_DIRECTORY}/install/hsc/bin/hsc"
            String params = "-r ${buildReportsDirectory} " +
                    "--exclude https://www.baeldung.com/.*" +
                    "${INTEGRATION_TEST_DIRECTORY_COMMON_BUILD}/docs"
            if (System.getProperty("os.name") ==~ /Windows.*/) {
                commandLine 'cmd', '/c', "echo off && ${hscScriptFileName.replace('/', '\\')}.bat ${params}"
            } else {
                commandLine 'sh', '-c', "${hscScriptFileName} ${params}"
            }
        }
        logger.debug "Script output: ${result}"
        assert testIndex.exists()
    }
}

final String INTEGRATION_TEST_DIRECTORY_MAVEN = "maven-plugin"

tasks.register("cleanIntegrationTestMavenPlugin") {
    group("Verification")
    description("Deletes the result directory of maven-plugin integration tests")

    File integrationTestDirectory = file(INTEGRATION_TEST_DIRECTORY_MAVEN)
    final File testIndex = new File(integrationTestDirectory, "target/reports/index.html")

    outputs.file testIndex

    doLast {
        def result = exec {
            workingDir INTEGRATION_TEST_DIRECTORY_MAVEN
            if (System.getProperty("os.name") ==~ /Windows.*/) {
                commandLine "cmd", "/c", "mvnw.cmd", "clean", "-Dhsc.version=${project.version}", "-ssettings.xml", "-B"
            } else {
                commandLine "./mvnw", "clean", "-Dhsc.version=${project.version}", "-ssettings.xml", "-B"
            }
        }

    }

    outputs.upToDateWhen {
        !testIndex.exists()
    }
}

tasks.register("integrationTestMavenPlugin") {
    group("Verification")
    description("Run Maven integration tests (only)")

    File integrationTestDirectory = file(INTEGRATION_TEST_DIRECTORY_MAVEN)
    final File testIndex = new File(integrationTestDirectory, "target/reports/index.html")

    outputs.file testIndex

    doLast {
        def result = exec {
            workingDir INTEGRATION_TEST_DIRECTORY_MAVEN
            if (System.getProperty("os.name") ==~ /Windows.*/) {
                commandLine "cmd", "/c", "mvnw.cmd", "verify", "-Dhsc.version=${project.version}", "-ssettings.xml"
            } else {
                commandLine "./mvnw", "verify", "-Dhsc.version=${project.version}", "-ssettings.xml"
            }
        }
        logger.debug "Script output: $result"
        assert testIndex.exists()
    }
}

tasks.register("integrationTest") {
    group("Verification")
    description("Run all integration tests (without any installations etc.)")

    dependsOn(integrationTestGradlePlugin, integrationTestCli, integrationTestMavenPlugin)
}

tasks.register("cleanIntegrationTest", Delete) {
    group("Build")
    description("Deletes all integration test builds")

    dependsOn(cleanIntegrationTestCommon, cleanIntegrationTestGradlePlugin,
            cleanIntegrationTestCli, cleanIntegrationTestMavenPlugin)
}
clean.dependsOn(cleanIntegrationTest)
