buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.3'
        classpath 'com.github.dcendents:android-maven-plugin:1.2'
        classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.5.3"
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
    }
}

apply plugin: 'com.android.library'
apply plugin: 'maven'
apply plugin: 'signing'
apply plugin: 'io.codearte.nexus-staging'
apply plugin: 'me.tatarka.retrolambda'

group = 'im.actor'
version = '0.0.1'

def baseVersion = "3.0"

android {

    compileSdkVersion 24
    buildToolsVersion '24.0.0'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 24
    }

    sourceSets {
        main {

            res {
                srcDirs = ['src/main/res',
                           'src/main/res-material']
            }

            resources {
                srcDirs = ['../../sdk-core/core/core-shared/src/main/resources']
            }

            assets {
                srcDirs = ['../../sdk-core/core/core-shared/src/main/resources',
                           'src/main/assets']
            }

            java {
                srcDirs = ['src/main/java',
                           '../../sdk-core/core/core-shared/src/main/java',
                           '../../sdk-core/core/core-android/src/main/java',
                           '../../sdk-core/runtime/runtime-shared/src/main/java',
                           '../../sdk-core/runtime/runtime-generic/src/main/java',
                           '../../sdk-core/runtime/runtime-android/src/main/java']
            }
        }
    }

    lintOptions {
        abortOnError false
    }

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

dependencies {

    // Calls
    compile 'io.pristine:libjingle:11139@aar'

    // UI
    compile 'com.facebook.fresco:fresco:0.10.0'
    compile 'com.facebook.fresco:animated-gif:0.10.0'
    compile 'com.facebook.fresco:webpsupport:0.10.0' // Not Enabling animated WebP yet
    compile 'com.github.castorflex.smoothprogressbar:library-circular:1.2.0'
    compile 'com.facebook.rebound:rebound:0.3.8'
    compile 'com.rengwuxian.materialedittext:library:2.1.4'

    compile 'com.soundcloud.android:android-crop:1.0.0@aar'
    compile('com.github.chrisbanes.photoview:library:1.2.4') {
        exclude module: 'support-v13'
    }

    compile 'com.droidkit:progress:0.5'
    compile 'com.getbase:floatingactionbutton:1.9.1'

    // Phone parser
    compile files('libs/libphonenumber-5.9.jar')

    // Core
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'

    // Android Support
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.android.support:support-v13:24.0.0'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:palette-v7:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:customtabs:24.0.0'
}

//
// Publishing of SDK to Maven Central
//

//
// Prepare Android JavaDocs and Sources jar
//

android.libraryVariants.all { variant ->

    task("javadoc${variant.name.capitalize()}", type: Javadoc, dependsOn: "assemble${variant.name.capitalize()}") {

        title = "Actor SDK for Android"

        // Sources
        source = variant.javaCompile.source

        // First add all of your dependencies to the classpath, then add the android jars
        classpath += files(variant.javaCompile.classpath.files)
        classpath += files(android.getBootClasspath())

        // Configuration
        options.links("http://d.android.com/reference")
        failOnError = false // TODO: Fix all errors

        // We're excluding these generated files
        // TODO: Fix
        // exclude '**/BuildConfig.java'
        // exclude '**/R.java'
    }

    task("javadocJar${variant.name.capitalize()}", type: Jar, dependsOn: "javadoc${variant.name.capitalize()}") {
        classifier 'javadoc'
        destinationDir = new File("$project.buildDir/libs/")
        from "$project.buildDir/javadoc/$variant.name"
    }

    task("sourcesJar${variant.name.capitalize()}", type: Jar, dependsOn: "assemble${variant.name.capitalize()}") {
        classifier 'sources'
        destinationDir = new File("$project.buildDir/libs/")
        from variant.javaCompile.source
    }
}

//
// Configuration of publishing
//

String ossrhUsername = ""
String ossrhPassword = ""
boolean isDeployConfigured = false
if (project.rootProject.file('gradle.properties').exists()) {
    Properties properties = new Properties()
    properties.load(project.rootProject.file('gradle.properties').newDataInputStream())
    ossrhUsername = properties.getProperty("ossrhUsername", "")
    ossrhPassword = properties.getProperty("ossrhPassword", "")
    version = baseVersion + "." + properties.getProperty("build_index", "")

    print("##teamcity[buildNumber '$version']")

    nexusStaging {
        username ossrhUsername
        password ossrhPassword
    }

    isDeployConfigured = true
}

project.afterEvaluate {

    artifacts {
        archives javadocJarRelease
        archives sourcesJarRelease
    }

    if (isDeployConfigured) {

        signing {
            sign configurations.archives
        }

        uploadArchives {
            repositories {
                mavenDeployer {
                    beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

                    repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                        authentication(userName: ossrhUsername, password: ossrhPassword)
                    }

                    pom.project {
                        name 'Actor SDK for Android'
                        packaging 'aar'
                        description 'Actor SDK: Embedding mobile messaging to Android App'
                        url 'https://github.com/actorapp/actor-platform'

                        scm {
                            url 'scm:git@github.com:actorapp/actor-platform.git'
                            connection 'scm:git@github.com:actorapp/actor-platform.git'
                            developerConnection 'scm:git@github.com:actorapp/actor-platform.git'
                        }

                        licenses {
                            license {
                                name 'The MIT License (MIT)'
                                url 'http://opensource.org/licenses/MIT'
                                distribution 'repo'
                            }
                        }

                        developers {
                            developer {
                                id 'ex3ndr'
                                name 'Stepan Korshakov'
                            }
                        }
                    }
                }
            }
        }
    }
}