apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'signing'

repositories {
    maven {
        url "https://oss.sonatype.org/content/repositories/staging/"
    }
}

android {
    compileSdk rootProject.ext.coreCompileSdkVersion
    publishing {
        multipleVariants {
            allVariants()
            withJavadocJar()
        }
    }
    defaultConfig {
        minSdkVersion rootProject.ext.coreMinSdkVersion
        targetSdkVersion rootProject.ext.coreTargetSdkVersion
    }
    namespace 'com.adjust.sdk.google.lvl'
    sourceSets {
        main {
            aidl {
                srcDirs 'src/main/aidl'
            }
        }
    }
    buildFeatures {
        aidl true
    }
    testOptions {
        unitTests.includeAndroidResources = true
    }
}

dependencies {

    // Add SDK via module.
    compileOnly project(':sdk-core')
    // Unit testing
    // For unit tests
    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:5.10.0'

// For integration tests
    testImplementation 'org.robolectric:robolectric:4.11.1'

    // Add SDK via Maven.
    // implementation 'com.adjust.sdk:adjust-android:5.3.0'
}

// read local properties
File localPropsFile = project.rootProject.file('local.properties')
if (localPropsFile.exists()) {
    Properties p = new Properties()
    new FileInputStream(localPropsFile).withCloseable { is ->
        p.load(is)
    }
    p.each { name, value ->
        ext[name] = value
    }
}

task adjustLvlPluginAar(type: Copy) {
    dependsOn 'assembleRelease'

    from('build/outputs/aar/')
    include 'sdk-plugin-google-lvl-release.aar'
    destinationDir file('build/libs/')
    rename 'sdk-plugin-google-lvl-release.aar', "${project.name}.aar"

}

task adjustLvlPluginJar(type: Jar) {
    dependsOn 'packageReleaseAssets'
    dependsOn 'compileReleaseJavaWithJavac'

    from('build/intermediates/library_assets/release/packageReleaseAssets/out/') {
        into('assets')
    }
    from('build/intermediates/javac/release/classes/')

    archiveFileName = project.provider { "${project.name}.jar" }
}


task adjustLvlPluginSourcesJar(type: Jar) {
    setArchiveClassifier('sources')
    from android.sourceSets.main.java.srcDirs
}

artifacts {
    archives adjustLvlPluginJar
    archives adjustLvlPluginSourcesJar
}

afterEvaluate {
    publishing {
        publications {
/*
            mavenAndroidLvlJar(MavenPublication) {
                customizePomForJar(pom)
                groupId rootProject.ext.adjustGroupId
                artifactId 'adjust-android-license-verification'
                version rootProject.ext.coreVersionName

                artifact adjustLvlPluginJar
                artifact adjustLvlPluginSourcesJar
            }
*/
            mavenAndroidLvlAar(MavenPublication) {
                customizePomForAar(pom)
                groupId rootProject.ext.adjustGroupId
                artifactId 'adjust-android-google-lvl'
                version rootProject.ext.coreVersionName

                artifact("$buildDir/outputs/aar/sdk-plugin-google-lvl-release.aar") {
                    builtBy tasks.getByPath("adjustLvlPluginAar")
                }
                artifact adjustLvlPluginSourcesJar
            }
        }

        repositories {
            maven {
                if (rootProject.ext.coreVersionName.toString().contains('SNAPSHOT')) {
                    println "Using Maven SNAPSHOT repository for publishing"
                    url "https://oss.sonatype.org/content/repositories/snapshots/"
                } else {
                    url "https://oss.sonatype.org/service/local/staging/deploy/maven2"
                }
                if (project.hasProperty("sonatypeUsername")) {
                    credentials {
                        username sonatypeUsername
                        password sonatypePassword
                    }
                }
            }
        }
    }
    signing {
//        sign publishing.publications.mavenAndroidLvlJar
        sign publishing.publications.mavenAndroidLvlAar
    }
}

def customizePomForJar(pom) {
    pom.withXml {
        def root = asNode()

        // Add all items necessary for maven central publication.
        root.children().last() + {
            resolveStrategy = Closure.DELEGATE_FIRST
            description 'The License Verification plugin for Adjust SDK for Android'
            name 'Adjust Android SDK License Verification plugin'
            url 'https://github.com/adjust/android_sdk'

            organization {
                name 'Adjust GmbH'
                url 'http://www.adjust.com'
            }
            licenses {
                license {
                    name 'MIT License'
                    url 'http://www.opensource.org/licenses/mit-license.php'
                }
            }
            scm {
                url 'git@github.com:adjust/android_sdk.git'
                connection 'scm:git:git@github.com:adjust/android_sdk.git'
                developerConnection 'scm:git:git@github.com:adjust/android_sdk.git'
            }
            developers {
                developer {
                    name 'Pedro Silva'
                    email 'pedro@adjust.com'
                }
                developer {
                    name 'Ugljesa Erceg'
                    email 'ugi@adjust.com'
                }
            }
        }
    }
}

def customizePomForAar(pom) {
    pom.withXml {
        def root = asNode()

        // Add all items necessary for maven central publication.
        root.children().last() + {
            resolveStrategy = Closure.DELEGATE_FIRST
            description 'The License Verification plugin for Adjust SDK for Android'
            name 'Adjust Android SDK License Verification plugin'
            url 'https://github.com/adjust/android_sdk'

            organization {
                name 'Adjust GmbH'
                url 'http://www.adjust.com'
            }
            licenses {
                license {
                    name 'MIT License'
                    url 'http://www.opensource.org/licenses/mit-license.php'
                }
            }
            scm {
                url 'git@github.com:adjust/android_sdk.git'
                connection 'scm:git:git@github.com:adjust/android_sdk.git'
                developerConnection 'scm:git:git@github.com:adjust/android_sdk.git'
            }
            developers {
                developer {
                    name 'Pedro Silva'
                    email 'pedro@adjust.com'
                }
                developer {
                    name 'Ugljesa Erceg'
                    email 'ugi@adjust.com'
                }
            }
        }
    }
}

model {
//    tasks.generatePomFileForMavenAndroidLvlJarPublication {
//        destination = file("${project.buildDir}/generated-pom.xml")
//    }
    /* TODO check if/how to replace this tasks
    tasks.publishMavenAndroidLvlPublicationToMavenLocal {
        dependsOn project.tasks.signArchives
    }
    tasks.publishMavenAndroidLvlPublicationToMavenRepository {
        dependsOn project.tasks.signArchives
    }
    */
    tasks.generatePomFileForMavenAndroidLvlAarPublication {
        destination = file("${project.buildDir}/generated-pom.xml")
    }
}


