apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

apply from: 'git-info.gradle'

android {
    compileSdkVersion setup.compileSdk

    defaultConfig {
        applicationId 'pl.szczodrzynski.edziennik'
        minSdkVersion setup.minSdk
        targetSdkVersion setup.targetSdk

        versionCode release.versionCode
        versionName release.versionName

        buildConfigField "java.util.Map<String, String>", "GIT_INFO", gitInfoMap
        buildConfigField "long", "BUILD_TIMESTAMP", String.valueOf(System.currentTimeMillis())
        buildConfigField "String", "VERSION_BASE", "\"${release.versionName}\""

        multiDexEnabled = true

        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
            }
        }
    }

    buildTypes {
        debug {
            minifyEnabled = false
        }
        release {
            minifyEnabled = true
            shrinkResources = true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            proguardFiles fileTree('proguard').asList().toArray()
        }
    }
    flavorDimensions "platform"
    productFlavors {
        main {
            versionName gitInfo.versionHuman
        }
        official {}
        play {}
    }
    variantFilter { variant ->
        def flavors = variant.flavors*.name
        setIgnore(variant.buildType.name == "debug" && !flavors.contains("main"))
    }

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    lintOptions {
        checkReleaseBuilds = false
    }
    buildFeatures {
        dataBinding = true
        viewBinding = true
    }
    compileOptions {
        coreLibraryDesugaringEnabled = true
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    packagingOptions {
        exclude 'META-INF/library-core_release.kotlin_module'
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}

tasks.whenTaskAdded { task ->
    if (!task.name.endsWith("Release") && !task.name.endsWith("ReleaseWithR8"))
        return
    def renameTaskName = "rename${task.name.capitalize()}"

    def flavor = ""
    if (task.name.startsWith("bundle"))
        flavor = task.name.substring("bundle".length(), task.name.indexOf("Release")).uncapitalize()
    if (task.name.startsWith("assemble"))
        flavor = task.name.substring("assemble".length(), task.name.indexOf("Release")).uncapitalize()
    if (task.name.startsWith("minify"))
        flavor = task.name.substring("minify".length(), task.name.indexOf("Release")).uncapitalize()

    if (flavor != "") {
        tasks.create(renameTaskName, Copy) {
            from file("${projectDir}/${flavor}/release/"), file("${buildDir}/outputs/mapping/${flavor}Release/"), file("${buildDir}/outputs/apk/${flavor}Release/")
            include "*.aab", "*.apk", "mapping.txt", "output-metadata.json"
            destinationDir file("${projectDir}/release/")
            rename ".+?\\.(.+)", "Edziennik_${android.defaultConfig.versionName}_${flavor}." + '$1'
        }
        task.finalizedBy(renameTaskName)
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    // Language cores
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:1.1.5"

    // Android Jetpack
    implementation "androidx.appcompat:appcompat:1.2.0"
    implementation "androidx.cardview:cardview:1.0.0"
    implementation "androidx.constraintlayout:constraintlayout:2.0.4"
    implementation "androidx.core:core-ktx:1.3.2"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.4"
    implementation "androidx.recyclerview:recyclerview:1.1.0"
    implementation "androidx.room:room-runtime:2.2.6"
    implementation "androidx.work:work-runtime-ktx:2.5.0"
    kapt "androidx.room:room-compiler:2.2.6"

    // Google design libs
    implementation "com.google.android.material:material:1.3.0"
    implementation "com.google.android:flexbox:2.0.1"

    // Play Services/Firebase
    implementation "com.google.android.gms:play-services-wearable:17.0.0"
    implementation "com.google.firebase:firebase-core:18.0.2"
    implementation "com.google.firebase:firebase-crashlytics:17.4.0"
    implementation("com.google.firebase:firebase-messaging") { version { strictly "20.1.3" } }

    // OkHttp, Retrofit, Gson, Jsoup
    implementation("com.squareup.okhttp3:okhttp") { version { strictly "3.12.13" } }
    implementation "com.squareup.retrofit2:retrofit:2.9.0"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"
    implementation "com.squareup.retrofit2:converter-scalars:2.9.0"
    implementation 'com.google.code.gson:gson:2.8.6'
    implementation "org.jsoup:jsoup:1.13.1"
    implementation "pl.droidsonroids:jspoon:1.3.2"
    implementation "pl.droidsonroids.retrofit2:converter-jspoon:1.3.2"

    // Szkolny.eu libraries/forks
    implementation "eu.szkolny:agendacalendarview:1799f8ef47"
    implementation "eu.szkolny:cafebar:5bf0c618de"
    implementation "eu.szkolny.fslogin:lib:2.0.0"
    implementation "eu.szkolny:material-about-library:1d5ebaf47c"
    implementation "eu.szkolny:mhttp:af4b62e6e9"
    implementation "eu.szkolny:nachos:0e5dfcaceb"
    implementation "eu.szkolny.selective-dao:annotation:27f8f3f194"
    implementation "eu.szkolny:ssl-provider:1.0.0"
    implementation "pl.szczodrzynski:navlib:0.8.0"
    implementation "pl.szczodrzynski:numberslidingpicker:2921225f76"
    implementation "pl.szczodrzynski:recyclertablayout:700f980584"
    implementation "pl.szczodrzynski:tachyon:551943a6b5"
    kapt "eu.szkolny.selective-dao:codegen:27f8f3f194"

    // Iconics & related
    implementation "com.mikepenz:iconics-core:5.3.0-b01"
    implementation "com.mikepenz:iconics-views:5.3.0-b01"
    implementation "com.mikepenz:community-material-typeface:5.8.55.0-kotlin@aar"
    implementation "eu.szkolny:szkolny-font:1.3"

    // Other dependencies
    implementation "cat.ereza:customactivityoncrash:2.3.0"
    implementation "com.applandeo:material-calendar-view:1.5.0"
    implementation "com.daimajia.swipelayout:library:1.2.0@aar"
    implementation "com.github.antonKozyriatskyi:CircularProgressIndicator:1.2.2"
    implementation "com.github.bassaer:chatmessageview:2.0.1"
    implementation "com.github.CanHub:Android-Image-Cropper:2.2.2"
    implementation "com.github.ChuckerTeam.Chucker:library:3.0.1"
    implementation "com.github.jetradarmobile:android-snowfall:1.2.0"
    implementation "com.github.wulkanowy.uonet-request-signer:hebe-jvm:a99ca50a31"
    implementation("com.heinrichreimersoftware:material-intro") { version { strictly "1.5.8" } }
    implementation "com.hypertrack:hyperlog:0.0.10"
    implementation "com.jaredrummler:colorpicker:1.1.0"
    implementation "com.qifan.powerpermission:powerpermission-coroutines:1.3.0"
    implementation "com.qifan.powerpermission:powerpermission:1.3.0"
    implementation "com.yuyh.json:jsonviewer:1.0.6"
    implementation "io.coil-kt:coil:1.1.1"
    implementation "me.dm7.barcodescanner:zxing:1.9.8"
    implementation "me.grantland:autofittextview:0.2.1"
    implementation "me.leolin:ShortcutBadger:1.1.22@aar"
    implementation "org.greenrobot:eventbus:3.2.0"
    implementation("pl.droidsonroids.gif:android-gif-drawable") { version { strictly "1.2.15" } }

    // Debug-only dependencies
    debugImplementation "com.amitshekhar.android:debug-db:1.0.5"
}