apply plugin: "com.android.application"
apply plugin: "com.mikepenz.aboutlibraries.plugin"

android {
    compileSdk 36
    namespace "com.faltenreich.diaguard"

    defaultConfig {
        minSdk 21
        targetSdk 36
        versionCode 68
        versionName "3.15.1"
        vectorDrawables.useSupportLibrary true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        testInstrumentationRunnerArguments clearPackageData: "true"
    }

    buildTypes {
        debug {
            shrinkResources false
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro", "proguard-rules-dontobfuscate.pro"
            testProguardFiles "proguard-rules.pro"
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    flavorDimensions += "version"
    productFlavors {
        demo {
            applicationId "com.faltenreich.diaguard"
            resValue "string", "package_name", applicationId
        }
        beta {
            applicationId "com.faltenreich.diaguard.beta"
            resValue "string", "package_name", applicationId
        }
        store {
            applicationId "com.faltenreich.diaguard"
            resValue "string", "package_name", applicationId
        }
    }

    buildFeatures {
        buildConfig true
        viewBinding true
    }

    testOptions {
        execution "ANDROID_TEST_ORCHESTRATOR"
        animationsDisabled true
        unitTests.includeAndroidResources true
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

tasks.withType(Test).configureEach {
    testLogging {
        exceptionFormat = "full"
    }
}

configurations {
    // Workaround for Error: commons-logging defines classes that conflict with classes now provided by Android
    all*.exclude group: "commons-logging", module: "commons-logging"
    all*.exclude group: "org.apache.commons", module: "commons-io"
}

def espresso = "3.6.1"
def jodaTime = "2.12.5"
def ormLite = "5.1"
def retrofit = "2.9.0"

dependencies {
    implementation project(":pdfjet")

    testImplementation "junit:junit:4.13.2"
    testImplementation "androidx.test.ext:junit:1.2.1"
    testImplementation "androidx.test.espresso:espresso-core:$espresso"
    testImplementation "androidx.test.espresso:espresso-contrib:$espresso"
    testImplementation "joda-time:joda-time:$jodaTime"
    debugImplementation "androidx.fragment:fragment-testing-manifest:1.8.7"
    androidTestImplementation "androidx.fragment:fragment-testing:1.8.7"

    androidTestImplementation "com.google.guava:guava:32.1.2-android"
    androidTestImplementation "junit:junit:4.13.2"
    androidTestImplementation "androidx.test.ext:junit:1.2.1"
    androidTestImplementation "androidx.test.espresso:espresso-core:$espresso"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$espresso"
    androidTestImplementation "androidx.test:runner:1.6.2"
    androidTestUtil "androidx.test:orchestrator:1.5.1"

    implementation "com.google.android.material:material:1.12.0"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
    implementation "androidx.constraintlayout:constraintlayout:2.2.1"
    implementation "androidx.preference:preference:1.2.1"

    implementation "com.j256.ormlite:ormlite-core:$ormLite"
    implementation "com.j256.ormlite:ormlite-android:$ormLite"
    implementation "net.danlew:android.joda:$jodaTime"
    implementation "org.greenrobot:eventbus:3.3.1"

    implementation "com.squareup.retrofit2:retrofit:$retrofit"
    implementation "com.squareup.okhttp3:okhttp"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit"
    implementation "com.google.code.gson:gson:2.11.0"

    // Newer versions do not support XML views
    // noinspection GradleDependency
    implementation "com.mikepenz:aboutlibraries-core:$aboutLibrariesVersion"
    implementation "com.mikepenz:aboutlibraries:$aboutLibrariesVersion"

    implementation "org.apache.commons:commons-text:1.10.0"
    // Workaround: Versioning is currently broken
    // https://github.com/square/picasso/issues/2179
    // noinspection GradleDependency
    implementation "com.squareup.picasso:picasso:2.8"
    implementation "com.opencsv:opencsv:5.8"
    implementation "com.github.PhilJay:MPAndroidChart:v3.1.0"
    implementation "com.fmsirvent:parallaxeverywhere:1.0.4"
    implementation "com.code-troopers.betterpickers:library:3.1.0"
}

tasks.register("applyVersionToReadme") {
    def string = "img.shields.io/badge/Release-"
    def regex = "$string([0-9.]+)"
    def android = project.extensions.findByName("android")
    def versionName = android.defaultConfig.versionName
    def with = "$string$versionName"
    ant.replaceregexp(file: "${rootProject.projectDir}/README.md", match: regex, flags: "g", replace: with)
}

tasks.register("applyDateToReadme") {
    def string = "2013-"
    def regex = "$string([0-9.]+)"
    int year = new Date().getYear() + 1900
    def with = "$string$year"
    ant.replaceregexp(file: "${rootProject.projectDir}/README.md", match: regex, flags: "g", replace: with)
}

assemble.dependsOn applyVersionToReadme
assemble.dependsOn applyDateToReadme