import java.nio.charset.Charset

/*
 * Copyright 2020 American Express Travel Related Services Company, Inc.
 *
 * 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.
 */
buildscript {
    ext.kotlin_version = '1.6.0'
    repositories {
        mavenCentral()
        exclusiveContent {
            forRepository { google() }
            filter {
                includeGroupByRegex("androidx\\..*")
                includeGroupByRegex("com\\.android\\..*")
                includeGroup("com.android")
                includeGroup("com.google.testing.platform")
                includeGroup("com.google.android.material")
            }
        }
    }
    dependencies {
        classpath 'org.jetbrains.dokka:dokka-gradle-plugin:1.6.0'
        classpath 'com.android.tools.build:gradle:7.0.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        mavenCentral()
        exclusiveContent {
            forRepository { google() }
            filter {
                includeGroupByRegex("androidx\\..*")
                includeGroupByRegex("com\\.android\\..*")
                includeGroup("com.android")
                includeGroup("com.google.testing.platform")
                includeGroup("com.google.android.material")
            }
        }
    }
}

def snapshot = System.getProperty("snapshot", "true").toBoolean()
group = System.getProperty('group')
version = System.getProperty('version') + (snapshot ? '-SNAPSHOT' : '')

subprojects {
    ext.deps = [targetSdk : 29,
                compileSdk: 29,
                minSdk    : 19,
                junit     : '4.13.1',
                assertj   : '2.6.0',
                mockito2  : '3.0.0',
                androidx  : [
                        material  : '1.0.0',
                        annotation: '1.1.0',
                        test      : [
                                rules   : '1.2.0',
                                espresso: [
                                        core           : '3.2.0',
                                        idling_resource: '3.2.0'
                                ]
                        ]
                ]
    ]

    ext.compilerArgs = ["-Werror"]

    apply plugin: 'org.jetbrains.dokka'
    apply plugin: 'maven-publish'
    apply plugin: 'signing'

    group = rootProject.group
    version = rootProject.version

    afterEvaluate {
        if (plugins.hasPlugin('java-library')) {
            sourceCompatibility = 1.8
            targetCompatibility = 1.8
        }

        if (plugins.hasPlugin('org.jetbrains.kotlin.android')) {
            android.kotlinOptions {
                jvmTarget = "1.8"
            }
        }

        if (plugins.hasPlugin('org.jetbrains.kotlin.jvm')) {
            compileKotlin {
                kotlinOptions {
                    jvmTarget = "1.8"
                }
            }

            compileTestKotlin {
                kotlinOptions {
                    jvmTarget = "1.8"
                }
            }
            java {
                withSourcesJar()
            }
        }

        if (plugins.hasPlugin('com.android.library')
                || plugins.hasPlugin('com.android.application')) {
            android {

                lintOptions {
                    disable "GradleDependency"
                }

                compileOptions {
                    sourceCompatibility JavaVersion.VERSION_1_8
                    targetCompatibility JavaVersion.VERSION_1_8
                }
            }
        }

        // Use getenv so they can be passed in without having them in a file
        // or in the command args
        def maven_username = System.getenv('MAVEN_CENTRAL_USERNAME') ?: ''
        def maven_password = System.getenv('MAVEN_CENTRAL_PASSWORD') ?: ''

        // from https://docs.gradle.org/current/userguide/publishing_maven.html
        if (plugins.hasPlugin('java-library')) {
            publishing {
                task dokkaJavadocJar(type: Jar) {
                    from dokkaJavadoc.outputDirectory
                }
                repositories {
                    maven {
                        // Use getenv so they can be passed in without having them in a file
                        // or in the command args
                        credentials {
                            username maven_username
                            password maven_password
                        }

                        url System.getenv('MAVEN_REPO_URL')
                    }
                }
                publications {
                    // groupId and version come from gradle.properties
                    // artifactId comes from the name of the gradle module
                    busybee(MavenPublication) {
                        from components.java
                        artifact dokkaJavadocJar {
                            getArchiveClassifier().set('javadoc')
                        }
                        pom {
                            name = 'busybee'
                            description = 'BusyBee is an alternative API for IdlingResources in Espresso tests.'
                            url = 'https://github.com/americanexpress/busybee'
                            licenses {
                                license {
                                    name = 'The Apache License, Version 2.0'
                                    url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                                }
                            }
                            developers {
                                developer {
                                    name = 'American Express Travel Related Services Company, Inc.'
                                }
                            }
                            scm {
                                connection = 'scm:git:https://github.com/americanexpress/busybee.git'
                                developerConnection = 'scm:git:git@github.com:americanexpress/busybee.git'
                                url = 'https://github.com/americanexpress/busybee'
                            }
                        }
                    }
                }
            }
            def signingKeyBase64 = findProperty("signingKey_base64")
            if (signingKeyBase64 != null) {
                signing {
                    def signingKey = new String(Base64.decoder.decode(signingKeyBase64), Charset.forName("UTF-8"))
                    def signingPassword = findProperty("signingPassword")
                    useInMemoryPgpKeys(signingKey, signingPassword)
                    sign publishing.publications.busybee
                }
            }
        }

        if (plugins.hasPlugin('com.android.library')) {
            // need afterEvaluate else the components.release will be missing below
            afterEvaluate {
                // need this until it is built into AGP, see https://issuetracker.google.com/issues/145670440
                task androidSourcesJar(type: Jar) {
                    from android.sourceSets.main.java.srcDirs
                }
                task dokkaJavadocJar(type: Jar) {
                    from dokkaJavadoc.outputDirectory
                }
                publishing {
                    repositories {
                        maven {
                            credentials {
                                username maven_username
                                password maven_password
                            }

                            url System.getenv('MAVEN_REPO_URL')
                        }
                    }
                    publications {
                        // groupId and version come from gradle.properties
                        // artifactId comes from the name of the gradle module
                        busybee(MavenPublication) {
                            from components.release
                            artifact androidSourcesJar {
                                getArchiveClassifier().set('sources')
                            }
                            artifact dokkaJavadocJar {
                                getArchiveClassifier().set('javadoc')
                            }
                            pom {
                                name = 'busybee'
                                description = 'BusyBee is an alternative API for IdlingResources in Espresso tests.'
                                url = 'https://github.com/americanexpress/busybee'
                                licenses {
                                    license {
                                        name = 'The Apache License, Version 2.0'
                                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                                    }
                                }
                                developers {
                                    developer {
                                        name = 'American Express Travel Related Services Company, Inc.'
                                    }
                                }
                                scm {
                                    connection = 'scm:git:https://github.com/americanexpress/busybee.git'
                                    developerConnection = 'scm:git:git@github.com:americanexpress/busybee.git'
                                    url = 'https://github.com/americanexpress/busybee'
                                }
                            }
                        }
                    }
                    def signingKeyBase64 = findProperty("signingKey_base64")
                    if (signingKeyBase64 != null) {
                        signing {
                            def signingKey = new String(Base64.decoder.decode(signingKeyBase64), Charset.forName("UTF-8"))
                            def signingPassword = findProperty("signingPassword")
                            useInMemoryPgpKeys(signingKey, signingPassword)
                            sign publishing.publications.busybee
                        }
                    }
                }
            }
        }
    }
}
