import org.apache.tools.ant.filters.*

plugins {
    id 'java-library'
    id 'idea'
    id 'eclipse'
    id 'maven-publish'
    id 'jacoco'
    id 'signing'
    id 'org.owasp.dependencycheck' version '11.1.0'
    id 'com.github.johnrengelman.shadow' version '8.1.1'
}

repositories {
    mavenCentral()

    if (project.hasProperty("isCI")) {
        maven {
            url uri('https://oss.sonatype.org/content/repositories/snapshots/')
            mavenContent {
                snapshotsOnly()
            }
        }
    }
}

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
    withJavadocJar()
    withSourcesJar()
}

ext {
    seleniumVersion = project.property('selenium.version')
    appiumClientVersion = project.property('appiumClient.version')
    slf4jVersion = '2.0.16'
}

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.34'
    annotationProcessor 'org.projectlombok:lombok:1.18.34'

    if (project.hasProperty("isCI")) {
        api "org.seleniumhq.selenium:selenium-api:${seleniumVersion}"
        api "org.seleniumhq.selenium:selenium-remote-driver:${seleniumVersion}"
        api "org.seleniumhq.selenium:selenium-support:${seleniumVersion}"
    } else {
        api('org.seleniumhq.selenium:selenium-api') {
            version {
                strictly "[${seleniumVersion}, 5.0)"
                prefer "${seleniumVersion}"
            }
        }
        api('org.seleniumhq.selenium:selenium-remote-driver') {
            version {
                strictly "[${seleniumVersion}, 5.0)"
                prefer "${seleniumVersion}"
            }
        }
        api('org.seleniumhq.selenium:selenium-support') {
            version {
                strictly "[${seleniumVersion}, 5.0)"
                prefer "${seleniumVersion}"
            }
        }
    }
    implementation 'com.google.code.gson:gson:2.11.0'
    implementation "org.slf4j:slf4j-api:${slf4jVersion}"
}

dependencyCheck {
    failBuildOnCVSS = 22
}

jacoco {
    toolVersion = '0.8.12'
}

tasks.withType(JacocoReport).configureEach {
    description = 'Generate Jacoco coverage reports after running tests'
    sourceSets sourceSets.main
    reports {
        html.required = true
        html.outputLocation = file("${buildDir}/Reports/jacoco")
    }
}
jacocoTestReport.dependsOn test

apply plugin: 'checkstyle'

checkstyle {
    toolVersion = '10.17.0'
    configFile = configDirectory.file('appium-style.xml').get().getAsFile()
    showViolations = true
    ignoreFailures = false
}

javadoc {
    options.addStringOption('encoding', 'UTF-8')
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = 'io.appium'
            artifactId = 'java-client'
            version = appiumClientVersion
            from components.java
            pom {
                name = 'java-client'
                description = 'Java client for Appium Mobile Webdriver'
                url = 'http://appium.io'
                developers {
                    developer {
                        name = 'Jonah Stiennon'
                        email = 'jonahss@gmail.com'
                        url = 'https://github.com/jonahss'
                        id = 'jonahss'
                    }
                    developer {
                        name = 'Sergey Tikhomirov'
                        email = 'tichomirovsergey@gmail.com'
                        url = 'https://github.com/TikhomirovSergey'
                        id = 'TikhomirovSergey'
                    }
                    developer {
                        name = 'Srinivasan Sekar'
                        email = 'srinivasan.sekar1990@gmail.com'
                        url = 'https://github.com/SrinivasanTarget'
                        id = 'SrinivasanTarget'
                    }
                    developer {
                        name = 'Mykola Mokhnach'
                        url = 'https://github.com/mykola-mokhnach'
                        id = 'mykola-mokhnach'
                    }
                    developer {
                        name = 'Valery Yatsynovich'
                        url = 'https://github.com/valfirst'
                        id = 'valfirst'
                    }
                }
                licenses {
                    license {
                        name = 'Apache License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution = 'repo'
                    }
                }
                scm {
                    url = 'https://github.com/appium/java-client'
                    connection = 'scm:git:ssh://git@github.com/appium/java-client.git'
                    developerConnection = 'scm:git:ssh://git@github.com/appium/java-client.git'
                    tag = 'HEAD'
                }
            }
        }
    }
    repositories {
        maven {
            credentials {
                username = System.getenv("MAVEN_USERNAME")
                password = System.getenv("MAVEN_PASSWORD")
            }
            def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
            def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/'"
            url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
        }
    }
}

signing {
    required { !'true'.equalsIgnoreCase(project.findProperty('signingDisabled')) }
    def signingKey = System.getenv("PGP_SECRET")
    def signingPassword = System.getenv("PGP_PASSPHRASE")
    useInMemoryPgpKeys(signingKey, signingPassword)
    sign publishing.publications.mavenJava
}

wrapper {
    gradleVersion = '8.9'
    distributionType = Wrapper.DistributionType.ALL
}

processResources {
    filter ReplaceTokens, tokens: [
            'selenium.version'    : seleniumVersion,
            'appiumClient.version': appiumClientVersion
    ]
}

testing {
    suites {
        configureEach {
            useJUnitJupiter()
            dependencies {
                implementation 'org.junit.jupiter:junit-jupiter:5.11.3'
                runtimeOnly 'org.junit.platform:junit-platform-launcher'
                implementation 'org.hamcrest:hamcrest:3.0'
                runtimeOnly "org.slf4j:slf4j-simple:${slf4jVersion}"
            }
            targets.configureEach {
                testTask.configure {
                    testLogging {
                        showStandardStreams = true
                        exceptionFormat = 'full'
                    }
                }
            }
        }

        test {
            dependencies {
                implementation "org.seleniumhq.selenium:selenium-chrome-driver:${seleniumVersion}"
                implementation('io.github.bonigarcia:webdrivermanager:5.9.2') {
                    exclude group: 'org.seleniumhq.selenium'
                }
            }
            targets.configureEach {
                testTask.configure {
                    finalizedBy jacocoTestReport
                }
            }
        }

        e2eIosTest(JvmTestSuite) {
            sources {
                java {
                    srcDirs = ['src/e2eIosTest/java']
                }
            }
            dependencies {
                implementation project()
                implementation(sourceSets.test.output)
                implementation('org.apache.commons:commons-lang3:3.17.0')
            }

            targets.configureEach {
                testTask.configure {
                    shouldRunAfter(test)
                    filter {
                        exclude '**/IOSScreenRecordTest.class'
                        exclude '**/ImagesComparisonTest.class'
                        exclude '**/IOSNativeWebTapSettingTest.class'
                    }
                }
            }
        }

        e2eAndroidTest(JvmTestSuite) {
            sources {
                java {
                    srcDirs = ['src/e2eAndroidTest/java']
                }
            }
            dependencies {
                implementation project()
                implementation(sourceSets.test.output)
                implementation('io.github.bonigarcia:webdrivermanager:5.9.2') {
                    exclude group: 'org.seleniumhq.selenium'
                }
            }

            targets.configureEach {
                testTask.configure {
                    shouldRunAfter(test)
                    filter {
                        // The following tests fail and should be reviewed/fixed
                        exclude '**/AndroidAbilityToUseSupplierTest.class'
                        exclude '**/AndroidConnectionTest.class'
                        exclude '**/AndroidContextTest.class'
                        exclude '**/AndroidDataMatcherTest.class'
                        exclude '**/AndroidDriverTest.class'
                        exclude '**/AndroidElementTest.class'
                        exclude '**/AndroidFunctionTest.class'
                        exclude '**/AndroidSearchingTest.class'
                        exclude '**/AndroidTouchTest.class'
                        exclude '**/AndroidViewMatcherTest.class'
                        exclude '**/ExecuteCDPCommandTest.class'
                        exclude '**/ExecuteDriverScriptTest.class'
                        exclude '**/FingerPrintTest.class'
                        exclude '**/ImagesComparisonTest.class'
                        exclude '**/KeyCodeTest.class'
                        exclude '**/LogEventTest.class'
                        exclude '**/UIAutomator2Test.class'
                        exclude '**/AndroidPageObjectTest.class'
                        exclude '**/MobileBrowserCompatibilityTest.class'
                    }
                }
            }
        }

        e2eFlutterTest(JvmTestSuite) {
            sources {
                java {
                    srcDirs = ['src/e2eFlutterTest/java']
                }
            }
            dependencies {
                implementation project()
                implementation(sourceSets.test.output)
            }

            targets.configureEach {
                testTask.configure {
                    shouldRunAfter(test)
                    systemProperties project.properties.subMap(["platform", "flutterApp"])
                }
            }
        }
    }
}
