plugins {
    id 'com.android.library'
    id 'kotlin-android'
    id 'me.sigptr.rust-android'
    id 'org.equalitie.ouisync.convention'
    id 'dokka-convention'
}

version = rootProject.version
group   = rootProject.group

android {
    namespace = 'org.equalitie.ouisync.service'

    compileSdk = 35

    defaultConfig {
        minSdk = 23
        targetSdk = 35
        versionCode = 1
        versionName = version

        consumerProguardFiles 'proguard-rules.pro'
    }

    kotlin {
        jvmToolchain(17)
    }

    publishing {
        singleVariant 'release'
        singleVariant 'debug'
    }
}

dependencies {
    implementation project(':ouisync-session')
    implementation 'net.java.dev.jna:jna:5.17.0@aar'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2'
}

def rustTargets = project.findProperty('target-platform')?.split(',').collect {
    // https://github.com/flutter/flutter/blob/37dbe030feb47cdf15f3d4c1921857dbbac8aedf/packages/flutter_tools/gradle/flutter.gradle#L78-L82
    switch (it) {
        case 'android-arm':   return 'arm'
        case 'android-arm64': return 'arm64'
        case 'android-x86':   return 'x86'
        case 'android-x64':   return 'x86_64'
        default: throw new IllegalArgumentException("unknown target platform: $it")
    }
}

cargo {
    module = "$rootDir/../.."
    libname = 'ouisync_service'

    // https://stackoverflow.com/questions/66054763/build-rust-on-android
    pythonCommand = "python3"

    // Read targets from the 'target-platform' project property that can be set in
    // `gradle.properties` or using the `-P` command line option when invoking gradle.
    //
    //Example: `gradle assemble -Ptarget-platform=android-x64`
    targets = rustTargets ?: ['arm', 'arm64', 'x86_64']

    extraCargoBuildArguments = ['--package', 'ouisync-service']
}

afterEvaluate {
    android.libraryVariants.each {
        def variant = it.name.capitalize()

        // Build the native library for the target android platforms.
        tasks.named("merge${variant}JniLibFolders") {
            dependsOn 'cargoBuild'
        }

        // Set cargo profile according to build type.
        //
        // FIXME: This doesn't work when multiple build types are built in the same gradle
        // invocation. Only one profile is build and then reused for all build types.
        // See also: https://github.com/mozilla/rust-android-gradle/pull/68
        def cargoProfile = it.name
        tasks.named("pre${variant}Build") {
            doLast {
                cargo.profile = cargoProfile
            }
        }

        tasks.named('cargoBuild') {
            mustRunAfter "pre${variant}Build"
        }
    }

    publishing {
        publications {
            android.libraryVariants.each {
                def variant = it.name
                def packageNameSuffix = variant == 'release' ? '' : "-${variant}"

                create("${variant}Service", MavenPublication) {
                    def packageName = "${project.name}${packageNameSuffix}"

                    from components.getByName(variant)

                    artifactId = packageName

                    pom {
                        name = packageName
                        description = "Secure peer-to-peer file syncing library (service component)"
                        url = 'https://github.com/equalitie/ouisync'
                        licenses {
                            license {
                                name = 'MPL-2.0'
                                url = 'https://github.com/equalitie/ouisync/blob/master/LICENSE'
                            }
                        }
                        developers {
                            developer {
                                name = 'Ouisync Developers'
                                email = 'ouisync@equalit.ie'
                            }
                        }

                        scm {
                            connection = 'scm:git:github.com/equalitie/ouisync.git'
                            developerConnection = 'scm:git:ssh://github.com/equalitie/ouisync.git'
                            url = 'https://github.com/equalitie/ouisync/tree/master/bindings/kotlin'
                        }
                    }
                }
            }
        }
    }
}

signing {
    // Only sign when publishing to sonatype, not local
    required = android.libraryVariants.any { variant ->
        gradle.taskGraph.hasTask("publish${variant}ServicePublicationToSonatypeRepository")
    }

    if (signingKey) {
        useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
    }

    sign publishing.publications
}

dokka {
    dokkaSourceSets.configureEach {
        includes.from("Module.md")
    }
}