def keystoreProperties = new Properties()
def keystorePropertiesFile = file('pocketmode-release.properties')
if (keystorePropertiesFile.exists()) {
    keystorePropertiesFile.withReader('UTF-8') { reader ->
        keystoreProperties.load(reader)
    }
}

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.artemchep.pocketmode"
        minSdkVersion 28
        targetSdkVersion 32
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        def versionTag = System.getenv("POCKET_MODE_RELEASE_TAG")
            ?: "0.1.0-0"

        def versionPartsCount = 4
        def versionParts = {
            def l = it
                .split(/[^0-9]+/)
                .collect {
                    try {
                        it as int
                    } catch (Exception ignored) {
                        null
                    }
                }
                .findAll { it != null }
            l += [0] * versionPartsCount
            l.take(versionPartsCount)
        }(versionTag)

        versionCode = versionParts
            .withIndex()
            .collect { v, index ->
                def reverseIndex = versionParts.size - index - 1
                v * Math.pow(100, reverseIndex) as int
            }
            .sum()
        versionName = versionParts.join('.')
    }
    signingConfigs {
        release {
            keyAlias keystoreProperties['key_alias']
            keyPassword keystoreProperties['password_store']
            storeFile file('pocketmode-release.keystore')
            storePassword keystoreProperties['password_key']
        }
    }

    def acraUri = System.getenv("ACRA_URI") ?: ""
    def acraUsername = System.getenv("ACRA_USERNAME") ?: ""
    def acraPassword = System.getenv("ACRA_PASSWORD") ?: ""

    buildTypes {
        debug {
            applicationIdSuffix '.debug'
            versionNameSuffix '-DEBUG'

            buildConfigField "String", "MY_LICENSE_KEY", "\"debug\""
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

            buildConfigField "String", "MY_LICENSE_KEY", "\"" + keystoreProperties['license_key'] + "\""
        }
    }
    buildTypes.each {
        it.buildConfigField("String", "ACRA_URI", '"' + acraUri + '"')
        it.buildConfigField("String", "ACRA_USERNAME", '"' + acraUsername + '"')
        it.buildConfigField("String", "ACRA_PASSWORD", '"' + acraPassword + '"')
    }

    flavorDimensions "app"
    productFlavors {
        playstore {
            dimension "app"
            buildConfigField "boolean", "ANALYTICS", "true"
        }
        opensource {
            dimension "app"
            buildConfigField "boolean", "ANALYTICS", "false"
        }
    }

    packagingOptions {
        pickFirst("META-INF/atomicfu.kotlin_module")
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    buildFeatures {
        viewBinding true
    }
    lintOptions {
        disable 'MissingTranslation'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
    implementation 'com.artemchep.config:config:2.2.0'
    implementation 'com.afollestad.material-dialogs:core:3.3.0'
    implementation 'com.google.android.material:material:1.8.0-rc01'
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
    implementation 'com.eightbitlab:blurview:1.6.6'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.browser:browser:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation "androidx.work:work-runtime-ktx:2.7.1"
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'
    implementation 'com.mikepenz:fastadapter:3.3.1'
    compileOnly 'org.solovyev.android:checkout:1.2.3'
    playstoreImplementation 'org.solovyev.android:checkout:1.2.3'
    playstoreImplementation platform('com.google.firebase:firebase-bom:26.5.0')
    playstoreImplementation 'com.google.firebase:firebase-crashlytics'
    playstoreImplementation 'com.google.firebase:firebase-analytics'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test:runner:1.5.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

if (!getGradle()
        .getStartParameter()
        .getTaskRequests()
        .toString()
        .contains("Opensource")
) {
    apply plugin: 'com.google.gms.google-services'
    apply plugin: 'com.google.firebase.crashlytics'
}
