/*
 * Copyright 2019 Esri
 *
 * 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.
 */

plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'org.jetbrains.dokka-android'
    // https://github.com/wupdigital/android-maven-publish
    id 'digital.wup.android-maven-publish' version '3.6.2'
}

apply from: "$rootProject.projectDir/gradle/script/artifact.gradle"

android {
    compileSdkVersion droid.targetSdk

    defaultConfig {
        minSdkVersion droid.minSdk
        targetSdkVersion droid.targetSdk
        versionCode _versionCode
        versionName _versionName
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            project.ext.set("archivesBaseName", _artifactId + '-' + _versionName)
            libraryVariants.all { variant ->
                variant.outputs.all {
                    outputFileName = outputFileName.replace("-release", "")
                }
            }
        }
        debug {}
    }
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        dataBinding = true
    }
}

dokka {
    outputFormat = 'html'
    outputDirectory = "$buildDir/javadoc"
    jdkVersion = 8 // Used for linking to JDK

    // This just mutes a warning that spams the console: `Can't find node by signature` but still cannot link
    // external classes in KDocs
    // https://github.com/Kotlin/dokka/issues/269#issuecomment-468733943
    externalDocumentationLink {
        packageListUrl = new URL("https://developers.arcgis.com/android/latest/api-reference/")
        url = new URL("https://developers.arcgis.com/android/latest/api-reference/")
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_reflect"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines"
    implementation "androidx.appcompat:appcompat:$droid.appcompat"
    implementation "androidx.constraintlayout:constraintlayout:$droid.constraintLayout"
    implementation "androidx.multidex:multidex:$droid.multiDex"
    implementation "androidx.navigation:navigation-fragment-ktx:$droid.nav_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$droid.lifecycle"
    implementation "androidx.recyclerview:recyclerview:$droid.recyclerview"
    implementation "com.google.android.material:material:$droid.material"
    implementation "com.google.ar:core:$droid.arCore"
    api "androidx.lifecycle:lifecycle-common-java8:$droid.lifecycle"
    api "com.google.ar.sceneform.ux:sceneform-ux:$droid.arCoreSceneForm"
    api "com.google.ar.sceneform:core:$droid.arCoreSceneForm"
    // fixing failing D8 https://github.com/google-ar/sceneform-android-sdk/issues/575
    implementation "com.google.ar.sceneform:animation:$droid.arCoreSceneForm"
    api "com.esri.arcgisruntime:arcgis-android:$arcgis"
    androidTestImplementation "androidx.test:rules:$test.rules"
    androidTestImplementation "androidx.test.ext:junit:$test.jUnit"
    androidTestImplementation("androidx.test.espresso:espresso-core:$test.espresso", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
}

afterEvaluate {
    // define publication and maven repo for publishing the aar and pom file
    // https://docs.gradle.org/current/userguide/publishing_maven.html
    publishing {
        publications {
            aar(MavenPublication) {
                from components.release
                groupId _groupId
                artifactId _artifactId
                version _fullVersion
            }
        }

        repositories {
            maven {
                // find the maven repository URL, e.g. http://my.org/repo, in properties passed through command line or read
                // from GRADLE_HOME/gradle.properties or local gradle.properties
                url project.findProperty("ARTIFACTORY_URL") ?: ""
                credentials {
                    username project.findProperty("ARTIFACTORY_USER") ?: ""
                    password project.findProperty("ARTIFACTORY_PASSWORD") ?: ""
                }
            }
        }
    }

    // publishAarPublicationToMavenRepository is defined by aar publication above
    // make sure assemble task runs before publishing
    tasks.findByName("publishAarPublicationToMavenRepository").dependsOn(assembleRelease)
}
