apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'
apply plugin: 'realm-android'
apply plugin: 'maven-publish'

if (project.hasProperty("coverage")) {
    apply plugin: 'jacoco'
}

buildscript {
    repositories {
        // Do not use `mavenCentral()`, it prevents Dependabot from working properly
        maven {
            url 'https://repo1.maven.org/maven2'
        }
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:10.16.0"
    }
}

android {
    namespace "org.matrix.android.sdk"

    testOptions.unitTests.includeAndroidResources = true

    compileSdk versions.compileSdk

    defaultConfig {
        minSdk versions.minSdk
        targetSdk versions.targetSdk

        // Multidex is useful for tests
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        // The following argument makes the Android Test Orchestrator run its
        // "pm clear" command after each test invocation. This command ensures
        // that the app's state is completely cleared between tests.
        testInstrumentationRunnerArguments clearPackageData: 'true'

        // WARNING: always restore this line after importing code from Element Android (2/2)
        buildConfigField "String", "SDK_VERSION", "\"${project.getProperties().getOrDefault("VERSION_NAME", "0.0.0")}\""

        buildConfigField "String", "GIT_SDK_REVISION", "\"${gitRevision()}\""
        buildConfigField "String", "GIT_SDK_REVISION_UNIX_DATE", "\"${gitRevisionUnixDate()}\""
        buildConfigField "String", "GIT_SDK_REVISION_DATE", "\"${gitRevisionDate()}\""

        defaultConfig {
            consumerProguardFiles 'proguard-rules.pro'
        }
    }

    testOptions {
        // Comment to run on Android 12
//        execution 'ANDROIDX_TEST_ORCHESTRATOR'
    }

    buildTypes {
        debug {
            if (project.hasProperty("coverage")) {
                testCoverageEnabled = coverage == "true"
            }
            // Set to true to log privacy or sensible data, such as token
            buildConfigField "boolean", "LOG_PRIVATE_DATA", project.property("vector.debugPrivateData")
            // Set to BODY instead of NONE to enable logging
            buildConfigField "okhttp3.logging.HttpLoggingInterceptor.Level", "OKHTTP_LOGGING_LEVEL", "okhttp3.logging.HttpLoggingInterceptor.Level." + project.property("vector.httpLogLevel")
        }

        release {
            buildConfigField "boolean", "LOG_PRIVATE_DATA", "false"
            buildConfigField "okhttp3.logging.HttpLoggingInterceptor.Level", "OKHTTP_LOGGING_LEVEL", "okhttp3.logging.HttpLoggingInterceptor.Level.BASIC"
        }
    }

    adbOptions {
        installOptions "-g"
//        timeOutInMs 350 * 1000
    }

    compileOptions {
        sourceCompatibility versions.sourceCompat
        targetCompatibility versions.targetCompat
    }

    kotlinOptions {
        jvmTarget = "11"
        freeCompilerArgs += [
                // Disabled for now, there are too many errors. Could be handled in another dedicated PR
                // '-Xexplicit-api=strict', // or warning
                "-opt-in=kotlin.RequiresOptIn",
                // Opt in for kotlinx.coroutines.FlowPreview
                "-opt-in=kotlinx.coroutines.FlowPreview",
        ]
    }

    sourceSets {
        androidTest {
            java.srcDirs += "src/sharedTest/java"
        }
        test {
            java.srcDirs += "src/sharedTest/java"
        }
    }

    publishing {
        singleVariant("release") {
            withSourcesJar()
            withJavadocJar()
        }
    }
}

static def gitRevision() {
    def cmd = "git rev-parse --short=8 HEAD"
    return cmd.execute().text.trim()
}

static def gitRevisionUnixDate() {
    def cmd = "git show -s --format=%ct HEAD^{commit}"
    return cmd.execute().text.trim()
}

static def gitRevisionDate() {
    def cmd = "git show -s --format=%ci HEAD^{commit}"
    return cmd.execute().text.trim()
}

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

dependencies {
    api libs.jetbrains.coroutinesCore
    api libs.jetbrains.coroutinesAndroid


//    implementation(name: 'crypto-android-release', ext: 'aar')
    //implementation 'net.java.dev.jna:jna:5.13.0@aar'

    //   implementation libs.androidx.appCompat
    api libs.androidx.core

    api libs.androidx.lifecycleLivedata

    // Lifecycle
    api libs.androidx.lifecycleCommon
    api libs.androidx.lifecycleProcess

    // Network
    api libs.squareup.retrofit
    api libs.squareup.retrofitMoshi

    // When version of okhttp is updated (current is 4.9.3), consider removing the workaround
    // to force usage of Protocol.HTTP_1_1. Check the status of:
    // - https://github.com/square/okhttp/issues/3278
    // - https://github.com/square/okhttp/issues/4455
    // - https://github.com/square/okhttp/issues/3146
    //Do not use bom for publishing
    def okhttp_version = "4.12.0"
    api "com.squareup.okhttp3:okhttp:$okhttp_version"
    api "com.squareup.okhttp3:logging-interceptor:$okhttp_version"

    api libs.squareup.moshi
    api libs.squareup.moshiAdapters
    kapt libs.squareup.moshiKotlin

    api "com.atlassian.commonmark:commonmark:0.13.0"

    // Image
    api libs.androidx.exifinterface

    // Database
    api 'com.github.Zhuinden:realm-monarchy:0.7.1'

    kapt 'dk.ilios:realmfieldnameshelper:2.0.0'

    // Shared Preferences
    api libs.androidx.preferenceKtx

    // Work
    api libs.androidx.work

    // olm lib is now hosted in MavenCentral
    api 'org.matrix.android:olm-sdk:3.2.12'

    // DI
    api libs.dagger.dagger
    kapt libs.dagger.daggerCompiler

    // Logging
    api libs.jakewharton.timber

    // Video compression
    api 'com.otaliastudios:transcoder:0.10.5'

    // Exif data handling
    api libs.apache.commonsImaging

    api libs.google.phonenumber

    //Bcrypt
    api 'at.favre.lib:bcrypt:0.9.0'

    api("org.futo.gitlab.circles:circles-rust-components-kotlin:v0.3.15.6@aar") {
        transitive = true
    }
    //rustCryptoApi project(":library:rustCrypto")

    testImplementation libs.tests.junit
    // Note: version sticks to 1.9.2 due to https://github.com/mockk/mockk/issues/281
    testImplementation libs.mockk.mockk
    testImplementation libs.tests.kluent
    testImplementation libs.jetbrains.coroutinesTest
    // Plant Timber tree for test
    testImplementation 'net.lachlanmckee:timber-junit-rule:1.0.1'
    // Transitively required for mocking realm as monarchy doesn't expose Rx
    testImplementation libs.rx.rxKotlin

    kaptAndroidTest libs.dagger.daggerCompiler
    androidTestImplementation libs.androidx.testCore
    androidTestImplementation libs.androidx.testRunner
    androidTestImplementation libs.androidx.testRules
    androidTestImplementation libs.androidx.junit
    androidTestImplementation libs.androidx.espressoCore
    androidTestImplementation libs.tests.kluent
    androidTestImplementation libs.mockk.mockkAndroid
    androidTestImplementation libs.androidx.coreTesting
    androidTestImplementation libs.jetbrains.coroutinesAndroid
    androidTestImplementation libs.jetbrains.coroutinesTest

    // Plant Timber tree for test
    androidTestImplementation libs.tests.timberJunitRule

    androidTestUtil libs.androidx.orchestrator
}

publishing {
    publications {
        release(MavenPublication) {
            groupId = "org.futo.gitlab.circles"
            artifactId = "matrix-android-sdk"
            version = "1.6.10.25"

            afterEvaluate {
                from components.release
            }
        }
    }
}