/*
 * Nextcloud Talk - Android Client (F7cloud embed patches in build.f7cloud.gradle)
 */
plugins {
    id "org.jetbrains.kotlin.plugin.compose" version "2.3.0"
    id "org.jetbrains.kotlin.kapt"
    id 'com.google.devtools.ksp' version '2.3.4'
}

def f7cloudAsLibrary = findProperty('f7cloudAsLibrary') == 'true'

if (f7cloudAsLibrary) {
    apply plugin: 'com.android.library'
} else {
    apply plugin: 'com.android.application'
}
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'
if (!f7cloudAsLibrary) {
    apply plugin: 'com.github.spotbugs'
    apply plugin: 'io.gitlab.arturbosch.detekt'
    apply plugin: "org.jlleitschuh.gradle.ktlint"
}
apply plugin: 'kotlinx-serialization'

android {
    compileSdkVersion 36

    namespace = 'ru.f7cloud.talk'

    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 36
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

        // F7cloud: keep-правила зависимостей vendor (WebRTC/LoganSquare/Dagger/RxJava/
        // Parceler/EventBus) для R8 приложения-потребителя (:app). Vendor — library без
        // минификации, поэтому только через consumerProguardFiles правила доедут до :app.
        consumerProguardFiles 'consumer-rules.pro'

        // mayor.minor.hotfix.increment (for increment: 01-50=Alpha / 51-89=RC / 90-99=stable)
        // xx   .xxx  .xx    .xx
        versionCode 230000090
        versionName "23.0.0"

        flavorDimensions "default"
        renderscriptTargetApi = 19
        renderscriptSupportModeEnabled true

        productFlavors {
            if (f7cloudAsLibrary) {
                f7 {
                    dimension "default"
                }
            } else {
                generic {
                    applicationId 'com.nextcloud.talk2'
                    dimension "default"
                }
                gplay {
                    applicationId 'com.nextcloud.talk2'
                    dimension "default"
                }
                qa {
                    applicationId "com.nextcloud.talk2.qa"
                    dimension "default"
                    versionCode 1
                    versionName "1"
                }
            }
        }

        // Enabling multidex support.
        multiDexEnabled = true

        vectorDrawables.useSupportLibrary = true

        lintOptions {
            disable 'InvalidPackage'
            disable 'MissingTranslation'
            disable 'VectorPath'
            disable 'UnusedQuantity'
        }

        javaCompileOptions {
            annotationProcessorOptions {
                arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

        sourceSets {
            // Adds exported schema location as test app assets.
            getByName("androidTest").assets.srcDir("$projectDir/schemas")
        }

        testInstrumentationRunnerArgument "TEST_SERVER_URL", "${NC_TEST_SERVER_BASEURL}"
        testInstrumentationRunnerArgument "TEST_SERVER_USERNAME", "${NC_TEST_SERVER_USERNAME}"
        testInstrumentationRunnerArgument "TEST_SERVER_PASSWORD", "${NC_TEST_SERVER_PASSWORD}"

        def localBroadcastPermission = "PRIVATE_BROADCAST"
        manifestPlaceholders.broadcastPermission = localBroadcastPermission
        buildConfigField "String", "PERMISSION_LOCAL_BROADCAST", "\"${localBroadcastPermission}\""
    }

    testOptions {
        unitTests.all {
            useJUnitPlatform()
        }
        unitTests.returnDefaultValues = true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        resources {
            excludes += [
                'META-INF/LICENSE.txt',
                'META-INF/LICENSE',
                'META-INF/NOTICE.txt',
                'META-INF/NOTICE',
                'META-INF/DEPENDENCIES',
                'META-INF/rxjava.properties'
            ]
        }
    }

    if (!f7cloudAsLibrary) {
        check.dependsOn 'spotbugsGplayDebug', 'lint', 'ktlintCheck', 'detekt'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    buildFeatures {
        viewBinding = true
        buildConfig = true
        compose = true
    }

    composeOptions {
        kotlinCompilerExtensionVersion = "1.5.15"
    }

    if (f7cloudAsLibrary) {
        // Merge as library — F7cloud app supplies the Application class.
        packaging {
            jniLibs {
                pickFirsts += ['libjingle_peerconnection_so.so']
            }
        }
    }

    lint {
        abortOnError = false
        disable 'MissingTranslation','PrivateResource'
        htmlOutput = layout.buildDirectory.file("reports/lint/lint.html").get().asFile
        htmlReport = true
    }
}
kapt {
    correctErrorTypes = true
}

ext {
    androidxCameraVersion = "1.5.2"
    coilKtVersion = "2.7.0"
    daggerVersion = "2.59"
    emojiVersion = "1.6.0"
    fidoVersion = "4.1.0-patch2"
    lifecycleVersion = '2.10.0'
    okhttpVersion = "4.12.0"
    markwonVersion = "4.6.2"
    materialDialogsVersion = "3.3.0"
    parcelerVersion = "1.1.13"
    prismVersion = "2.0.0"
    retrofit2Version = "3.0.0"
    roomVersion = "2.8.4"
    workVersion = "2.11.0"
    espressoVersion = "3.7.0"
    androidxTestVersion = "1.5.0"
    media3_version = "1.9.0"
    coroutines_version = "1.10.2"
    mockitoKotlinVersion = "6.2.2"
}

configurations.configureEach {
    exclude group: 'com.google.firebase', module: 'firebase-core'
    exclude group: 'com.google.firebase', module: 'firebase-analytics'
    exclude group: 'com.google.firebase', module: 'firebase-measurement-connector'
    exclude group: 'org.jetbrains', module: 'annotations-java5' // via prism4j, already using annotations explicitly
}

dependencies {

    kapt "org.jetbrains.kotlin:kotlin-metadata-jvm:${rootProject.ext.kotlinVersion}"
    implementation "androidx.room:room-testing-android:${roomVersion}"
    implementation 'androidx.compose.foundation:foundation-layout:1.10.1'
    if (!f7cloudAsLibrary) {
        spotbugsPlugins 'com.h3xstream.findsecbugs:findsecbugs-plugin:1.14.0'
        spotbugsPlugins 'com.mebigfatguy.fb-contrib:fb-contrib:7.7.4'
        detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.23.8")
    }

    implementation("androidx.compose.runtime:runtime:1.10.1")
    implementation 'androidx.preference:preference-ktx:1.2.1'
    implementation 'androidx.datastore:datastore-core:1.2.0'
    implementation 'androidx.datastore:datastore-preferences:1.2.0'
    implementation 'androidx.test.ext:junit-ktx:1.3.0'
	
    implementation fileTree(include: ['*'], dir: 'libs')

    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.10.0"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:$coroutines_version"

    implementation 'androidx.appcompat:appcompat:1.7.1'
    implementation 'com.google.android.material:material:1.13.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
    implementation "com.vanniktech:emoji-google:0.23.0"
    implementation "androidx.emoji2:emoji2:${emojiVersion}"
    implementation "androidx.emoji2:emoji2-bundled:${emojiVersion}"
    implementation "androidx.emoji2:emoji2-views:${emojiVersion}"
    implementation "androidx.emoji2:emoji2-views-helper:${emojiVersion}"
    implementation 'org.michaelevans.colorart:library:0.0.3'
    implementation "androidx.work:work-runtime:${workVersion}"
    implementation "androidx.work:work-rxjava2:${workVersion}"
    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.2.0'
    implementation 'com.google.android.flexbox:flexbox:3.0.0'
    implementation ('com.github.bitfireAT:dav4jvm:2.1.3', {
        exclude group: 'org.ogce', module: 'xpp3'	// Android comes with its own XmlPullParser
    })
    implementation 'org.conscrypt:conscrypt-android:2.5.3'
    implementation "com.github.nextcloud-deps:qrcodescanner:0.1.2.4" // "com.github.blikoon:QRCodeScanner:0.1.2"

    implementation "androidx.camera:camera-core:${androidxCameraVersion}"
    implementation "androidx.camera:camera-camera2:${androidxCameraVersion}"
    implementation "androidx.camera:camera-lifecycle:${androidxCameraVersion}"
    implementation "androidx.camera:camera-view:${androidxCameraVersion}"
    implementation "androidx.exifinterface:exifinterface:1.4.2"

    implementation "androidx.lifecycle:lifecycle-runtime-ktx:${lifecycleVersion}"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:${lifecycleVersion}"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${lifecycleVersion}"
    implementation "androidx.lifecycle:lifecycle-process:${lifecycleVersion}"
    implementation "androidx.lifecycle:lifecycle-common:${lifecycleVersion}"
    implementation("androidx.lifecycle:lifecycle-viewmodel-compose:${lifecycleVersion}")

    implementation 'androidx.biometric:biometric:1.1.0'

    implementation 'androidx.multidex:multidex:2.0.1'

    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation "io.reactivex.rxjava2:rxjava:2.2.21"

    implementation "com.squareup.okhttp3:okhttp:${okhttpVersion}"
    implementation "com.squareup.okhttp3:okhttp-urlconnection:${okhttpVersion}"
    implementation "com.squareup.okhttp3:logging-interceptor:${okhttpVersion}"

    implementation 'com.bluelinelabs:logansquare:1.3.7'
    implementation 'com.fasterxml.jackson.core:jackson-core:2.20.1'
    kapt 'com.bluelinelabs:logansquare-compiler:1.3.7'

    implementation "com.squareup.retrofit2:retrofit:${retrofit2Version}"
    implementation "com.squareup.retrofit2:adapter-rxjava2:${retrofit2Version}"
    implementation "com.squareup.retrofit2:converter-gson:${retrofit2Version}"
    implementation 'de.mannodermaus.retrofit2:converter-logansquare:1.4.1'

    implementation "com.google.dagger:dagger:${daggerVersion}"
    kapt "com.google.dagger:dagger-compiler:${daggerVersion}"
    implementation 'com.github.lukaspili.autodagger2:autodagger2:1.1'
    kapt 'com.github.lukaspili.autodagger2:autodagger2-compiler:1.1'
    compileOnly 'javax.annotation:javax.annotation-api:1.3.2'

    implementation 'org.greenrobot:eventbus:3.3.1'
    implementation 'net.zetetic:sqlcipher-android:4.13.0'

    implementation "androidx.room:room-runtime:${roomVersion}"
    implementation "androidx.room:room-rxjava2:${roomVersion}"
    ksp "androidx.room:room-compiler:${roomVersion}"
    implementation "androidx.room:room-ktx:${roomVersion}"

    implementation "org.parceler:parceler-api:$parcelerVersion"
    implementation 'com.github.ddB0515.FlexibleAdapter:flexible-adapter:5.1.1'
    implementation 'com.github.ddB0515.FlexibleAdapter:flexible-adapter-ui:5.1.1'
    implementation 'org.apache.commons:commons-lang3:3.20.0'
    implementation 'com.google.code.findbugs:jsr305:3.0.2'
    implementation 'com.github.nextcloud-deps:ChatKit:0.4.2'
    implementation 'joda-time:joda-time:2.14.0'
    implementation "io.coil-kt:coil:${coilKtVersion}"
    implementation "io.coil-kt:coil-gif:${coilKtVersion}"
    implementation "io.coil-kt:coil-svg:${coilKtVersion}"
    implementation "io.coil-kt:coil-compose:${coilKtVersion}"
    implementation 'com.github.natario1:Autocomplete:1.1.0'

    implementation "com.github.nextcloud-deps.hwsecurity:hwsecurity-fido:${fidoVersion}"
    implementation "com.github.nextcloud-deps.hwsecurity:hwsecurity-fido2:${fidoVersion}"

    implementation "com.afollestad.material-dialogs:core:${materialDialogsVersion}"
    implementation "com.afollestad.material-dialogs:datetime:${materialDialogsVersion}"
    implementation "com.afollestad.material-dialogs:bottomsheets:${materialDialogsVersion}"
    implementation "com.afollestad.material-dialogs:lifecycle:${materialDialogsVersion}"

    implementation 'com.google.code.gson:gson:2.13.2'

    implementation "androidx.media3:media3-exoplayer:$media3_version"
    implementation "androidx.media3:media3-ui:$media3_version"

    implementation 'com.github.chrisbanes:PhotoView:2.3.0'
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.30'

    implementation "io.noties.markwon:core:$markwonVersion"
    implementation "io.noties.markwon:ext-strikethrough:$markwonVersion"
    implementation "io.noties.markwon:ext-tasklist:$markwonVersion"
    implementation "io.noties.markwon:ext-tables:$markwonVersion"

    implementation 'com.github.nextcloud-deps:ImagePicker:2.1.0.2'
    implementation 'io.github.elye:loaderviewlibrary:3.0.0'
    implementation 'org.osmdroid:osmdroid-android:6.1.20'
    implementation ('fr.dudie:nominatim-api:3.4', {
        //noinspection DuplicatePlatformClasses
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    })

    implementation 'androidx.core:core-ktx:1.17.0'
    implementation 'androidx.activity:activity-ktx:1.12.2'
    implementation 'com.github.nextcloud.android-common:ui:0.31.0'
    implementation 'com.github.nextcloud-deps:android-talk-webrtc:132.6834.0'

    if (f7cloudAsLibrary) {
        f7Implementation 'com.google.android.gms:play-services-base:18.10.0'
        f7Implementation 'com.google.firebase:firebase-messaging:25.0.1'
    } else {
        gplayImplementation 'com.google.android.gms:play-services-base:18.10.0'
        gplayImplementation "com.google.firebase:firebase-messaging:25.0.1"
    }

    //compose
    implementation(platform("androidx.compose:compose-bom:2026.01.00"))
    implementation("androidx.compose.ui:ui")
    implementation 'androidx.compose.material3:material3:1.4.0'
    // replace with xml drawables instead of legacy fallback lib, see
    // https://developer.android.com/jetpack/androidx/releases/compose-material3#1.4.0
    implementation 'androidx.compose.material:material-icons-core:1.7.8'
    implementation("androidx.compose.material:material-icons-extended")
    implementation("androidx.compose.ui:ui-tooling-preview")
    implementation 'androidx.activity:activity-compose:1.12.2'
    debugImplementation("androidx.compose.ui:ui-tooling")

    //tests
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher:6.0.2'
    androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.10.1")
    debugImplementation("androidx.compose.ui:ui-test-manifest")

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.mockito:mockito-core:5.21.0'
    testImplementation 'androidx.arch.core:core-testing:2.2.0'

    androidTestImplementation "androidx.test:core:1.7.0"

    androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2"
    androidTestImplementation 'androidx.test:core-ktx:1.7.0'
    androidTestImplementation 'org.mockito:mockito-android:5.21.0'
    androidTestImplementation "androidx.work:work-testing:${workVersion}"
    // Espresso core
    androidTestImplementation ("androidx.test.espresso:espresso-core:$espressoVersion", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-web:$espressoVersion"
    androidTestImplementation "androidx.test.espresso:espresso-accessibility:$espressoVersion"

    androidTestImplementation "androidx.test.espresso:espresso-intents:$espressoVersion"

    androidTestImplementation('com.android.support.test.espresso:espresso-intents:3.0.2')

    androidTestImplementation(platform("androidx.compose:compose-bom:2026.01.00"))
    testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version"

    testImplementation 'org.junit.vintage:junit-vintage-engine:6.0.2' // DO NOT REMOVE
    testImplementation "androidx.room:room-testing:${roomVersion}"
    testImplementation("com.squareup.okhttp3:mockwebserver:$okhttpVersion")
    testImplementation("com.google.dagger:hilt-android-testing:2.59")
    testImplementation("org.robolectric:robolectric:4.16.1")
}

tasks.register('installGitHooks', Copy) {
    description = "Install git hooks"
    from("../scripts/hooks") {
        include '*'
    }
    into '../.git/hooks'
    enabled = !f7cloudAsLibrary
}

if (!f7cloudAsLibrary) {
    apply from: 'build.quality.gradle'
}

ksp {
    arg('room.schemaLocation', "$projectDir/schemas")
}

if (f7cloudAsLibrary) {
    apply from: 'build.f7cloud.gradle'
}