// Top-level build file where you can add configuration options common to all
// sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2'
        classpath "com.diffplug.spotless:spotless-plugin-gradle:6.13.0"
    }
}

plugins {
    id 'org.ajoberstar.grgit' version '4.1.1'
}

apply plugin: 'com.android.application'
apply plugin: "com.diffplug.spotless"

allprojects {
    repositories {
        google()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

repositories {
    google()
    mavenCentral()
    jcenter()
    maven { url "https://jitpack.io" }
}

configurations {
    playstoreImplementation
    gitImplementation
    implementation.exclude group: 'org.jetbrains' , module:'annotations'
}

dependencies {

    implementation 'androidx.viewpager:viewpager:1.0.0'

    playstoreImplementation('com.google.firebase:firebase-messaging:23.1.2') {
        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'
    }
    playstoreImplementation 'com.android.installreferrer:installreferrer:2.2'
    implementation 'org.sufficientlysecure:openpgp-api:10.0'
    implementation('com.theartofdev.edmodo:android-image-cropper:2.8.0') {
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'exifinterface'
    }
    implementation 'im.conversations.webrtc:webrtc-android:104.0.0'
    //implementation 'org.snikket:webrtc-android:107.0.0'
    implementation 'org.jitsi:org.otr4j:0.23'
    implementation 'org.bouncycastle:bcmail-jdk15on:1.64'
    implementation 'org.gnu.inet:libidn:1.15'
    implementation 'com.google.zxing:core:3.5.0'
    implementation 'de.measite.minidns:minidns-hla:0.2.4'
    implementation 'me.leolin:ShortcutBadger:1.1.22@aar'
    implementation 'org.whispersystems:signal-protocol-android:2.6.2'
    implementation 'com.makeramen:roundedimageview:2.3.0'
    implementation 'jetty:javax.servlet:5.1.12'
    implementation 'com.google.code.gson:gson:2.8.9'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.legacy:legacy-support-v13:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'androidx.exifinterface:exifinterface:1.3.5'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.emoji2:emoji2:1.2.0'
    gitImplementation "androidx.emoji2:emoji2-bundled:1.2.0"
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.cardview:cardview:1.0.0' // for compatibility
    implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
    implementation 'com.google.android.exoplayer:exoplayer-core:2.18.1'
    implementation 'com.google.android.exoplayer:exoplayer-ui:2.18.1'
    implementation 'com.google.android.exoplayer:extension-mediasession:2.18.1'
    implementation 'pub.devrel:easypermissions:3.0.0' // version >= 3.0.0 needs android X libraries
    implementation 'com.wefika:flowlayout:0.4.1'
    implementation 'com.googlecode.ez-vcard:ez-vcard:0.11.3'
    implementation 'org.jxmpp:jxmpp-jid:1.0.3'
    implementation 'org.hsluv:hsluv:0.2'
    implementation 'com.github.martin-stone:hsv-alpha-color-picker-android:2.4.2'
    implementation 'org.conscrypt:conscrypt-android:2.5.2'
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.25'
    implementation 'me.drakeet.support:toastcompat:1.1.0'
    implementation 'org.osmdroid:osmdroid-android:6.1.14'
    implementation 'com.leinardi.android:speed-dial:3.3.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation "com.squareup.okhttp3:okhttp:4.10.0"
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.google.guava:guava:31.1-android'
    implementation 'com.github.AppIntro:AppIntro:6.2.0'
    implementation 'androidx.browser:browser:1.4.0'
    implementation 'com.otaliastudios:transcoder:0.9.1' // 0.10.4 seems to be buggy
    implementation 'com.github.singpolyma:Better-Link-Movement-Method:4df081e1e4'
    implementation project(':libs:AXML')
    implementation 'com.github.ipld:java-cid:v1.3.1'
    implementation 'com.github.woltapp:blurhash:master'
    implementation 'org.tomlj:tomlj:1.1.0'
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'io.michaelrocks:libphonenumber-android:8.12.49'
    implementation 'io.github.nishkarsh:android-permissions:2.1.6'
    implementation 'com.splitwise:tokenautocomplete:3.0.2'
    implementation 'com.github.singpolyma:android-identicons:master-SNAPSHOT'
    implementation 'com.github.woltapp:blurhash:master'
    implementation 'androidx.documentfile:documentfile:1.0.1'
    implementation 'com.caverock:androidsvg-aar:1.4'
    implementation 'org.tomlj:tomlj:1.1.0'
}

ext {
    preDexEnabled = System.getProperty("pre-dex", "true")
    abiCodes = ['armeabi-v7a': 1, 'x86': 2, 'x86_64': 3, 'arm64-v8a': 4]
}



def tags = grgit.tag.list().findAll { it.dateTime != null }.sort { it.dateTime }

android {
    namespace 'eu.siacs.conversations'
    //noinspection GradleCompatible
    compileSdkVersion 33

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 32

        //versionNameSuffix " beta_(2023-01-10)" // " beta_(XXXX-XX-XX)" // activate for beta versions
        versionCode 132
        versionName "1.7"
        //resConfigs "en"

        archivesBaseName += "-$versionName"
        //archivesBaseName += "$versionNameSuffix"  // activate for beta versions
        applicationId "de.monocles.chat"
        multiDexEnabled true
        versionName grgit.describe(always: true)
        buildConfigField("String", "LOGTAG", '"monocles chat"')
        buildConfigField("String", "DOMAIN_LOCK", 'null')
        buildConfigField("String", "MAGIC_CREATE_DOMAIN", '"monocles.eu"')
        buildConfigField("boolean", "SHOW_INTRO", 'true')
        buildConfigField("String", "UPDATE_URL", '"https://monocles.eu/chat/update/"')
        resValue "string", "applicationId", applicationId
        resValue "string", "app_name", "monocles chat"
        resValue "string", "short_app_name", "chat"
    }
    splits {
        abi {
            universalApk true
            enable true
        }
    }
    dataBinding {
        enabled true
    }

    packagingOptions {
        resources {
            excludes += ['META-INF/BCKEY.DSA', 'META-INF/BCKEY.SF']
        }
    }

    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_9
        targetCompatibility JavaVersion.VERSION_1_9
    }

    flavorDimensions("distribution")

    productFlavors {
        playstore {
            dimension "distribution"
            versionNameSuffix "-playstore"
            applicationId "de.monocles.chat"
            buildConfigField("boolean", "SHOW_MIGRATION_INFO", 'false')
            resValue "string", "applicationId", applicationId
        }
        git {
            dimension "distribution"
            buildConfigField("boolean", "SHOW_MIGRATION_INFO", 'true')
        }
    }
    if (project.hasProperty('mStoreFile') &&
            project.hasProperty('mStorePassword') &&
            project.hasProperty('mKeyAlias') &&
            project.hasProperty('mKeyPassword')) {
        signingConfigs {
            release {
                storeFile file(mStoreFile)
                storePassword mStorePassword
                keyAlias mKeyAlias
                keyPassword mKeyPassword
            }
        }
        buildTypes {
            release {
                debuggable false
                signingConfig = signingConfigs.release
                minifyEnabled true
                shrinkResources true
                runProguard true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }

            debug {
                debuggable true
                buildTypes.release.signingConfig = null
                minifyEnabled true
                shrinkResources true
                runProguard true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }

        }
    }

    subprojects {

        afterEvaluate {
            if (getPlugins().hasPlugin('android') ||
                    getPlugins().hasPlugin('android-library')) {

                configure(android.lintOptions) {
                    disable 'AndroidGradlePluginVersion', 'MissingTranslation'
                }
            }

        }
    }

    lint {
        abortOnError false
        checkReleaseBuilds true
        disable 'ExtraTranslation', 'MissingTranslation', 'InvalidPackage', 'MissingQuantity', 'AppCompatResource', 'RestrictedApi'
        error 'StringFormatInvalid', 'StringFormatMatches'
    }
    android.applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def baseAbiVersionCode = project.ext.abiCodes.get(output.getFilter(com.android.build.OutputFile.ABI))
            if (baseAbiVersionCode != null) {
                output.versionCodeOverride = (100 * project.android.defaultConfig.versionCode) + baseAbiVersionCode
            } else {
                output.versionCodeOverride = (100 * project.android.defaultConfig.versionCode) + grgit.log(includes: ["HEAD"], excludes: [tags.last()]).size()
            }
        }

    }
}