plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jlleitschuh.gradle.ktlint' version "11.5.1"
    id 'com.google.gms.google-services'
}

android {
    namespace 'com.vitorpamplona.amethyst'
    compileSdk 34

    defaultConfig {
        applicationId "com.vitorpamplona.amethyst"
        minSdk 26
        targetSdk 34
        versionCode 293
        versionName "0.75.14"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
        resourceConfigurations += ['ar', 'cs', 'de', 'eo', 'es', 'fa', 'fr', 'hu', 'ja', 'night', 'nl', 'pt-rBR', 'ru', 'sv-rSE', 'ta', 'th', 'tr', 'uk', 'zh', 'sh-rHK', 'zh-rTW']
    }

    buildTypes {
        release {
            //signingConfig signingConfigs.debug
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), 'proguard-rules.pro'
            minifyEnabled true
            resValue "string", "app_name", "@string/app_name_release"
        }
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '-DEBUG'
            resValue "string", "app_name", "@string/app_name_debug"
        }
    }

    flavorDimensions = ["channel"]

    productFlavors {
        play {
            getIsDefault().set(true)
            dimension "channel"
        }

        fdroid {
            dimension "channel"
        }
    }

    splits {
        abi {
            enable true
            reset()
            include "x86", "x86_64", "arm64-v8a", "armeabi-v7a"
            universalApk true
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    buildFeatures {
        compose true
    }

    composeOptions {
        kotlinCompilerExtensionVersion "1.5.1"
    }

    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
    lint {
        disable 'MissingTranslation'
    }
}

dependencies {
    implementation project(path: ':quartz')
    implementation "androidx.core:core-ktx:$core_ktx_version"
    implementation 'androidx.activity:activity-compose:1.7.2'
    implementation "androidx.compose.ui:ui:$compose_ui_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"

    // Needs this to open gallery / image upload
    implementation "androidx.fragment:fragment-ktx:$fragment_version"

    // Navigation
    implementation "androidx.navigation:navigation-compose:$nav_version"

    // Observe Live data as State
    implementation "androidx.compose.runtime:runtime-livedata:$compose_ui_version"

    implementation "androidx.compose.material:material:$compose_ui_version"
    implementation "androidx.compose.material:material-icons-extended:$compose_ui_version"

    // Lifecycle
    implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"

    // Zoomable images
    implementation "net.engawapg.lib:zoomable:1.5.0"

    // Biometrics
    implementation "androidx.biometric:biometric-ktx:1.2.0-alpha05"

    // Websockets API
    implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.11'

    // HTML Parsing for Link Preview
    implementation 'org.jsoup:jsoup:1.16.1'

    // Encrypted Key Storage
    implementation 'androidx.security:security-crypto-ktx:1.1.0-alpha06'

    // view videos
    implementation "androidx.media3:media3-exoplayer:${exoplayer_version}"
    // important for proxy / tor
    implementation "androidx.media3:media3-datasource-okhttp:${exoplayer_version}"

    // Load images from the web.
    implementation "io.coil-kt:coil-compose:$coil_version"
    // view gifs
    implementation "io.coil-kt:coil-gif:$coil_version"
    // view svgs
    implementation "io.coil-kt:coil-svg:$coil_version"

    // create blurhash
    implementation group: 'io.trbl', name: 'blurhash', version: '1.0.0'

    // Permission to upload pictures:
    implementation "com.google.accompanist:accompanist-permissions:$accompanist_version"

    // For QR generation
    implementation 'com.google.zxing:core:3.5.2'
    implementation 'com.journeyapps:zxing-android-embedded:4.3.0'

    // Markdown
    //implementation "com.halilibo.compose-richtext:richtext-ui:0.16.0"
    //implementation "com.halilibo.compose-richtext:richtext-ui-material:0.16.0"
    //implementation "com.halilibo.compose-richtext:richtext-commonmark:0.16.0"

    // Markdown (With fix for full-image bleeds)
    implementation('com.github.vitorpamplona.compose-richtext:richtext-ui:a0954aba63')
    implementation('com.github.vitorpamplona.compose-richtext:richtext-ui-material:a0954aba63')
    implementation('com.github.vitorpamplona.compose-richtext:richtext-commonmark:a0954aba63')

    // Language picker and Theme chooser
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation "androidx.media3:media3-exoplayer-hls:$media3_version"
    implementation "androidx.media3:media3-ui:$media3_version"
    implementation "androidx.media3:media3-session:$media3_version"

    // Local model for language identification
    playImplementation 'com.google.mlkit:language-id:17.0.4'

    // Google services model the translate text
    playImplementation 'com.google.mlkit:translate:17.0.1'

    // PushNotifications
    playImplementation platform('com.google.firebase:firebase-bom:32.2.3')
    playImplementation 'com.google.firebase:firebase-messaging-ktx'

    // Charts
    implementation "com.patrykandpatrick.vico:core:${vico_version}"
    implementation "com.patrykandpatrick.vico:compose:${vico_version}"
    implementation "com.patrykandpatrick.vico:views:${vico_version}"
    implementation "com.patrykandpatrick.vico:compose-m2:${vico_version}"

    // GeoHash
    implementation 'com.github.drfonfon:android-kotlin-geohash:1.0'

    // Waveform visualizer
    implementation 'com.github.lincollincol:compose-audiowaveform:1.1.1'

    // Video compression lib
    implementation 'com.github.AbedElazizShe:LightCompressor:1.3.1'
    // Image compression lib
    implementation 'id.zelory:compressor:3.0.1'

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'io.mockk:mockk:1.13.7'
    androidTestImplementation 'androidx.test.ext:junit:1.2.0-alpha01'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
}

ktlint {
    disabledRules.set(["no-wildcard-imports"])
}

// https://gitlab.com/fdroid/wiki/-/wikis/HOWTO:-diff-&-fix-APKs-for-Reproducible-Builds#differing-assetsdexoptbaselineprofm-easy-to-fix
// NB: Android Studio can't find the imports; this does not affect the
// actual build since Gradle can find them just fine.
import com.android.tools.profgen.ArtProfileKt
import com.android.tools.profgen.ArtProfileSerializer
import com.android.tools.profgen.DexFile

project.afterEvaluate {
    tasks.each { task ->
        if (task.name.startsWith("compile") && task.name.endsWith("ReleaseArtProfile")) {
            task.doLast {
                outputs.files.each { file ->
                    if (file.name.endsWith(".profm")) {
                        println("Sorting ${file} ...")
                        def version = ArtProfileSerializer.valueOf("METADATA_0_0_2")
                        def profile = ArtProfileKt.ArtProfile(file)
                        def keys = new ArrayList(profile.profileData.keySet())
                        def sortedData = new LinkedHashMap()
                        Collections.sort keys, new DexFile.Companion()
                        keys.each { key -> sortedData[key] = profile.profileData[key] }
                        new FileOutputStream(file).with {
                            write(version.magicBytes$profgen)
                            write(version.versionBytes$profgen)
                            version.write$profgen(it, sortedData, "")
                        }
                    }
                }
            }
        }
    }
}
