plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs.kotlin'
    id 'com.google.gms.google-services'
    id 'com.google.firebase.crashlytics'
    id 'com.google.firebase.firebase-perf'
}

def buildParams = getGradle().getStartParameter().toString().toLowerCase()

task ensureFiles {
    if (buildParams.contains("ProdRelease".toLowerCase())) {
        println("#### Checking must-have files ####")
        assert file("./src/prod/google-services.json").exists()
        assert file("./src/prod/res/values/config_ad.xml").exists()
        println("#### Must-have files included ####")
    }
}

android {
    compileSdk 33
    buildToolsVersion = "33.0.1"

    defaultConfig {
        applicationId "tw.firemaples.onscreenocr"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 117
        versionName "3.1.25"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        buildConfigField "boolean", "DISABLE_LOGGING", "true"
        buildConfigField "boolean", "ENABLE_ADS", "true"
    }

    namespace 'tw.firemaples.onscreenocr'

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    bundle {
        language {
            // Specifies that the app bundle should not support
            // configuration APKs for language resources. These
            // resources are instead packaged with each base and
            // dynamic feature APK.
            enableSplit = false
        }
        density {
            // This property is set to true by default.
            enableSplit = false
        }
        abi {
            // This property is set to true by default.
            enableSplit = false
        }
    }

    flavorDimensions += "tier"
    productFlavors {
        dev {
            dimension "tier"
            applicationId "tw.firemaples.onscreenocr_debug"

            buildConfigField "boolean", "DISABLE_LOGGING", "false"
            buildConfigField "boolean", "ENABLE_ADS", "false"
        }
        prod {
            dimension "tier"

            def signGradlePath = System.getenv("ANDROID_SIGN_GRADLE_PATH")
            if (signGradlePath != null) {
                apply from: signGradlePath
                signingConfig signingConfigs.playStore
            }
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = '17'
    }
    buildFeatures {
        viewBinding true
    }
    packagingOptions {
        jniLibs {
            pickFirsts += ['lib/*/*.so']
        }
    }
}

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

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2'
    implementation "androidx.preference:preference-ktx:1.2.1"
    implementation 'androidx.webkit:webkit:1.7.0'
    //noinspection GradleDynamicVersion
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3'
    // For using coroutines in tests
    // For runBlockingTest, CoroutineDispatcher etc.
    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
    // For InstantTaskExecutorRule
    testImplementation "androidx.arch.core:core-testing:2.2.0"

    // Data store - preference
    implementation "androidx.datastore:datastore-preferences:1.0.0"

    // Navigation component
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    // Feature module Support
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
    // Testing Navigation
    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

    // required to avoid crash on Android 12 API 31
    implementation 'androidx.work:work-runtime-ktx:2.8.1'

    // Firebase
    implementation platform('com.google.firebase:firebase-bom:32.6.0')
    implementation 'com.google.firebase:firebase-crashlytics'
    implementation 'com.google.firebase:firebase-analytics'
    //  implementation 'com.google.firebase:firebase-core'
    implementation 'com.google.firebase:firebase-config'
    implementation 'com.google.firebase:firebase-perf'

    // Google MLKit - Text recognition v2
    // To recognize Latin script
    implementation 'com.google.mlkit:text-recognition:16.0.0'
    // To recognize Chinese script
    implementation 'com.google.mlkit:text-recognition-chinese:16.0.0'
    // To recognize Devanagari script
    implementation 'com.google.mlkit:text-recognition-devanagari:16.0.0'
    // To recognize Japanese script
    implementation 'com.google.mlkit:text-recognition-japanese:16.0.0'
    // To recognize Korean script
    implementation 'com.google.mlkit:text-recognition-korean:16.0.0'

    // Google MLKit - Text translate
    implementation 'com.google.mlkit:translate:17.0.2'

    // Google MLKit - Identify Languages
    implementation 'com.google.mlkit:language-id:17.0.4'

    // Kotpref
    // core
    implementation 'com.chibatching.kotpref:kotpref:2.13.2'
    // optional, auto initialization module
    implementation 'com.chibatching.kotpref:initializer:2.13.2'
    // optional, support saving enum value and ordinal
    implementation 'com.chibatching.kotpref:enum-support:2.13.2'
    // optional, support saving json string through Gson
    implementation 'com.chibatching.kotpref:gson-support:2.13.2'
    implementation 'com.google.code.gson:gson:2.10.1'
    // optional, support LiveData observable preference
    implementation 'com.chibatching.kotpref:livedata-support:2.13.2'
//    implementation 'androidx.lifecycle:lifecycle-livedata:2.2.0'
//    // experimental, preference screen build dsl
//    implementation 'com.chibatching.kotpref:preference-screen-dsl:2.13.1'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-moshi:2.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'

    // Tesseract
    implementation 'cz.adaptech.tesseract4android:tesseract4android:4.5.0'
//    implementation 'cz.adaptech:tesseract4android:4.1.0'

    // koral--/android-gif-drawable
    implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.28'

//    // Ad network mediation - Mopub
//    implementation('com.mopub:mopub-sdk:+@aar') {
//        transitive = true
//    }
//    implementation('com.mopub:mopub-sdk-banner:+@aar') {
//        transitive = true
//    }
//    // Facebook Ad network - Audience network
//    implementation 'com.facebook.android:audience-network-sdk:6.5.0'
//    implementation 'com.mopub.mediation:facebookaudiencenetwork:6.5.0.0'
//    // Google Ad network - Admob
//    implementation 'com.google.android.gms:play-services-ads:20.6.0'
//    implementation 'com.mopub.mediation:admob:20.2.0.0'
//    // Adcolony Ad network
//    implementation 'com.adcolony:sdk:4.5.0'
//    implementation 'com.mopub.mediation:adcolony:4.5.0.1'

    // Admob
    implementation 'com.google.android.gms:play-services-ads:22.5.0'
    // Facebook Ad network
    implementation 'com.google.ads.mediation:facebook:6.16.0.0'
    // Adcolony Ad network
    implementation 'com.google.ads.mediation:adcolony:4.8.0.2'
    // To fix build failed with issue
    // Missing class com.facebook.infer.annotation.Nullsafe (referenced from: com.facebook.ads.NativeAdBase)
    // https://stackoverflow.com/a/74718984/2906153
    implementation 'com.facebook.infer.annotation:infer-annotation:0.18.0'

    // TouchImageView
    implementation 'com.github.MikeOrtiz:TouchImageView:3.6'
}
