import com.android.tools.profgen.ArtProfileKt
import com.android.tools.profgen.ArtProfileSerializer
import com.android.tools.profgen.DexFile

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id("org.jetbrains.kotlin.plugin.compose") version "2.0.21"
}

android {
    namespace 'org.privacymatters.safespace'
    compileSdk 36

    defaultConfig {
        applicationId "org.privacymatters.safespace"

        minSdk 29
        targetSdk 35
        versionCode 46
        versionName "2.3.3"

        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.5.1'
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    dependenciesInfo {
        // Disables dependency metadata when building APKs.
        includeInApk = false
        // Disables dependency metadata when building Android App Bundles.
        includeInBundle = false
    }
}

dependencies {

//  For Security
    implementation "androidx.biometric:biometric-ktx:1.2.0-alpha05"
    implementation "androidx.security:security-crypto:1.0.0"

//  For Audio/Video
    implementation 'androidx.media3:media3-exoplayer:1.7.1'
    implementation 'androidx.media3:media3-ui:1.7.1'
    implementation 'androidx.media3:media3-common:1.7.1'

//  For Pictures
    implementation 'com.github.bumptech.glide:glide:4.16.0'
    implementation "com.github.bumptech.glide:compose:1.0.0-beta01"
    implementation 'androidx.preference:preference-ktx:1.2.1'
    implementation 'androidx.activity:activity-ktx:1.10.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.9.2'
    implementation 'androidx.lifecycle:lifecycle-runtime-compose-android:2.9.2'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
    implementation 'com.github.chrisbanes:PhotoView:2.3.0'

//  Compose
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.9.2'
    implementation 'androidx.activity:activity-compose:1.10.1'
    implementation platform('androidx.compose:compose-bom:2025.07.00')
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    debugImplementation 'androidx.compose.ui:ui-tooling'
    implementation "androidx.compose.runtime:runtime-livedata:1.8.3"
    debugImplementation 'androidx.compose.ui:ui-test-manifest'
    androidTestImplementation platform('androidx.compose:compose-bom:2025.07.00')
    androidTestImplementation 'androidx.compose.ui:ui-test-junit4'

//  For Camera
    def camerax_version = "1.4.2"
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    implementation "androidx.camera:camera-video:${camerax_version}"

    implementation "androidx.camera:camera-view:${camerax_version}"
    implementation "androidx.camera:camera-extensions:${camerax_version}"

//  For App functionality
    implementation 'androidx.core:core-ktx:1.16.0'
    implementation "androidx.recyclerview:recyclerview:1.4.0"
    implementation 'androidx.appcompat:appcompat:1.7.1'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.2.1'
    implementation "androidx.fragment:fragment-ktx:1.8.8"
    implementation "androidx.preference:preference-ktx:1.2.1"

    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.14'
}

// https://f-droid.org/docs/Reproducible_Builds/#bug-baselineprofm-not-deterministic
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, "")
                        }
                    }
                }
            }
        }
    }
}