import com.github.triplet.gradle.androidpublisher.ReleaseStatus
import ru.cian.huawei.publish.ReleaseNote

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlinx-serialization'
apply plugin: 'kotlin-parcelize'
apply plugin: 'com.google.devtools.ksp'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.github.triplet.play'
apply plugin: 'ru.cian.huawei-publish'
apply plugin: 'com.mikepenz.aboutlibraries.plugin'
apply plugin: 'com.huawei.agconnect'
apply plugin: 'kotlin-kapt'
apply from: 'jacoco.gradle'
apply from: 'sonarqube.gradle'
apply from: 'hooks.gradle'

android {
    namespace 'io.github.wulkanowy'
    compileSdk 34

    defaultConfig {
        applicationId "io.github.wulkanowy"
        testApplicationId "io.github.tests.wulkanowy"
        minSdkVersion 21
        targetSdkVersion 34
        versionCode 169
        versionName "2.6.9"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        resValue "string", "app_name", "Wulkanowy"
        manifestPlaceholders = [admob_project_id: ""]

        buildConfigField "String", "SINGLE_SUPPORT_AD_ID", "null"
        buildConfigField "String", "DASHBOARD_TILE_AD_ID", "null"

        if (System.env.SET_BUILD_TIMESTAMP) {
            buildConfigField "long", "BUILD_TIMESTAMP", String.valueOf(System.currentTimeMillis())
        } else {
            buildConfigField "long", "BUILD_TIMESTAMP", "1486235849000"
        }
    }

    sourceSets {
        // https://github.com/robolectric/robolectric/issues/3928#issuecomment-395309991
        debug.assets.srcDirs += files("$projectDir/schemas".toString())
    }

    signingConfigs {
        release {
            storeFile file("upload-key.jks")
            storePassword System.getenv("PLAY_STORE_PASSWORD")
            keyAlias System.getenv("PLAY_KEY_ALIAS")
            keyPassword System.getenv("PLAY_KEY_PASSWORD")
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
            buildConfigField "String", "MESSAGES_BASE_URL", "\"https://messages.wulkanowy.net.pl\""
            buildConfigField "String", "SCHOOLS_BASE_URL", '"https://schools.wulkanowy.net.pl"'
        }
        debug {
            minifyEnabled false
            shrinkResources false
            resValue "string", "app_name", "Wulkanowy DEV"
            applicationIdSuffix ".dev"
            versionNameSuffix "-dev"
            buildConfigField "String", "MESSAGES_BASE_URL", "\"https://messages.wulkanowy.net.pl\""
            buildConfigField "String", "SCHOOLS_BASE_URL", '"https://schools.wulkanowy.net.pl"'
        }
    }

    flavorDimensions += "platform"

    productFlavors {
        hms {
            dimension "platform"
            manifestPlaceholders = [install_channel: "AppGallery"]
        }

        play {
            dimension "platform"
            manifestPlaceholders = [
                install_channel : "Google Play",
                admob_project_id: System.getenv("ADMOB_PROJECT_ID") ?: "ca-app-pub-3940256099942544~3347511713"
            ]
            buildConfigField "String", "SINGLE_SUPPORT_AD_ID", "\"${System.getenv("SINGLE_SUPPORT_AD_ID") ?: "ca-app-pub-3940256099942544/5354046379"}\""
            buildConfigField "String", "DASHBOARD_TILE_AD_ID", "\"${System.getenv("DASHBOARD_TILE_AD_ID") ?: "ca-app-pub-3940256099942544/6300978111"}\""

        }

        fdroid {
            dimension "platform"
            manifestPlaceholders = [install_channel: "F-Droid"]
        }
    }

    playConfigs {
        play { enabled.set(true) }
    }

    buildFeatures {
        viewBinding true
        buildConfig true
    }

    bundle {
        language {
            enableSplit = false
        }
    }

    testOptions {
        unitTests.includeAndroidResources = true
        // workaround HMS test errors https://github.com/robolectric/robolectric/issues/2750
        unitTests.all { jvmArgs '-noverify' }
    }

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

    kotlinOptions {
        jvmTarget = "17"
        freeCompilerArgs += ["-opt-in=kotlin.RequiresOptIn", "-Xjvm-default=all"]
    }

    packagingOptions {
        resources {
            excludes += ['META-INF/library_release.kotlin_module',
                         'META-INF/library-core_release.kotlin_module',
                         'META-INF/LICENSE.md',
                         'META-INF/LICENSE-notice.md']
        }
    }

    aboutLibraries {
        configPath = "app/src/main/res/raw"
    }
}

kapt {
    correctErrorTypes true
}
ksp {
    arg("room.schemaLocation", "$projectDir/schemas".toString())
}

play {
    defaultToAppBundles = false
    track = 'production'
    releaseStatus = ReleaseStatus.IN_PROGRESS
    userFraction = 0.99d
    updatePriority = 2
    enabled.set(false)
}

huaweiPublish {
    instances {
        hmsRelease {
            credentialsPath = "$rootDir/app/src/release/agconnect-credentials.json"
            buildFormat = "aab"
            deployType = "publish"
            releaseNotes = [
                new ReleaseNote(
                    "pl-PL",
                    "$projectDir/src/main/play/release-notes/pl-PL/default.txt"
                )
            ]
        }
    }
}

ext {
    work_manager = "2.9.0"
    android_hilt = "1.2.0"
    room = "2.6.1"
    chucker = "4.0.0"
    mockk = "1.13.10"
    coroutines = "1.8.0"
}

dependencies {
    implementation 'io.github.wulkanowy:sdk:2.6.8'

    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'

    implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.3"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-guava:$coroutines"

    implementation 'androidx.core:core-ktx:1.13.1'
    implementation 'androidx.core:core-splashscreen:1.0.1'
    implementation "androidx.activity:activity-ktx:1.9.0"
    implementation "androidx.appcompat:appcompat:1.6.1"
    implementation "androidx.fragment:fragment-ktx:1.7.0"
    implementation "androidx.annotation:annotation:1.7.1"
    implementation "androidx.javascriptengine:javascriptengine:1.0.0-beta01"

    implementation "androidx.preference:preference-ktx:1.2.1"
    implementation "androidx.recyclerview:recyclerview:1.3.2"
    implementation "androidx.viewpager2:viewpager2:1.1.0-rc01"
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
    implementation "androidx.constraintlayout:constraintlayout:2.1.4"
    implementation "androidx.coordinatorlayout:coordinatorlayout:1.2.0"
    implementation "com.google.android.material:material:1.10.0"
    implementation "com.github.wulkanowy:material-chips-input:2.3.1"
    implementation "com.github.PhilJay:MPAndroidChart:v3.1.0"
    implementation 'com.github.lopspower:CircularImageView:4.3.0'

    implementation "androidx.work:work-runtime:$work_manager"
    playImplementation "androidx.work:work-gcm:$work_manager"

    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.7.0"

    implementation "androidx.room:room-runtime:$room"
    implementation "androidx.room:room-ktx:$room"
    ksp "androidx.room:room-compiler:$room"

    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
    kapt "androidx.hilt:hilt-compiler:$android_hilt"
    implementation "androidx.hilt:hilt-work:$android_hilt"

    implementation 'com.github.ncapdevi:FragNav:3.3.0'
    implementation "com.github.YarikSOffice:lingver:1.3.0"

    implementation 'com.squareup.retrofit2:retrofit:2.11.0'
    implementation "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter:1.0.0"
    implementation "com.squareup.okhttp3:logging-interceptor:4.12.0"
    implementation "com.squareup.okhttp3:okhttp-urlconnection:4.12.0"

    implementation "com.jakewharton.timber:timber:5.0.1"
    implementation 'com.github.Faierbel:slf4j-timber:2.0'
    implementation 'com.github.bastienpaulfr:Treessence:1.1.2'
    implementation "com.mikepenz:aboutlibraries-core:$about_libraries"
    implementation 'io.coil-kt:coil:2.6.0'
    implementation "io.github.wulkanowy:AppKillerManager:3.0.1"
    implementation 'me.xdrop:fuzzywuzzy:1.4.0'
    implementation 'com.fredporciuncula:flow-preferences:1.9.1'
    implementation 'org.apache.commons:commons-text:1.12.0'

    playImplementation platform('com.google.firebase:firebase-bom:33.0.0')
    playImplementation 'com.google.firebase:firebase-analytics'
    playImplementation 'com.google.firebase:firebase-messaging'
    playImplementation 'com.google.firebase:firebase-crashlytics:'
    playImplementation 'com.google.firebase:firebase-config'

    playImplementation 'com.google.android.gms:play-services-ads:22.6.0'
    playImplementation "com.google.android.play:integrity:1.3.0"
    playImplementation 'com.google.android.play:app-update-ktx:2.1.0'
    playImplementation 'com.google.android.play:review-ktx:2.0.1'
    playImplementation "com.google.android.ump:user-messaging-platform:2.1.0"

    hmsImplementation 'com.huawei.hms:hianalytics:6.12.0.301'
    hmsImplementation 'com.huawei.agconnect:agconnect-crash:1.9.1.303'

    releaseImplementation "com.github.chuckerteam.chucker:library-no-op:$chucker"

    debugImplementation "com.github.chuckerteam.chucker:library:$chucker"
    debugImplementation 'com.github.amitshekhariitbhu.Android-Debug-Database:debug-db:1.0.6'
    debugImplementation 'com.github.haroldadmin:WhatTheStack:1.0.0-alpha04'

    testImplementation "junit:junit:4.13.2"
    testImplementation "io.mockk:mockk:$mockk"
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines"
    testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"

    testImplementation 'org.robolectric:robolectric:4.12.1'
    testImplementation "androidx.test:runner:1.5.2"
    testImplementation "androidx.test.ext:junit:1.1.5"
    testImplementation "androidx.test:core:1.5.0"
    testImplementation "androidx.room:room-testing:$room"
    testImplementation "com.google.dagger:hilt-android-testing:$hilt_version"
    kaptTest "com.google.dagger:hilt-android-compiler:$hilt_version"

    androidTestImplementation "androidx.test:core:1.5.0"
    androidTestImplementation "androidx.test:runner:1.5.2"
    androidTestImplementation "androidx.test.ext:junit:1.1.5"
    androidTestImplementation "io.mockk:mockk-android:$mockk"
    androidTestImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
}
