apply plugin: 'com.android.application'

import groovy.json.JsonSlurper

def resolvePackageVersionName() {
    def packageJsonFile = file("../../package.json")
    if (!packageJsonFile.exists()) {
        return "1.0"
    }

    return new JsonSlurper().parse(packageJsonFile).version.toString()
}

def versionNameToCode(String versionName) {
    def stableVersion = versionName.replaceFirst(/^v/, "").split("-")[0]
    def parts = stableVersion.tokenize(".").collect { part -> part.isInteger() ? part.toInteger() : 0 }

    while (parts.size() < 3) {
        parts.add(0)
    }

    return (parts[0] * 10000) + (parts[1] * 100) + parts[2]
}

def resolvedVersionName = "${project.findProperty("APP_VERSION_NAME") ?: resolvePackageVersionName()}"
def resolvedVersionCode = Integer.parseInt("${project.findProperty("APP_VERSION_CODE") ?: versionNameToCode(resolvedVersionName)}")

android {
    namespace "fivechan.android"
    compileSdkVersion rootProject.ext.compileSdkVersion
    defaultConfig {
        applicationId "fivechan.android"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode resolvedVersionCode
        versionName resolvedVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        aaptOptions {
             // Files and dirs to omit from the packaged assets dir, modified to accommodate modern web apps.
             // Default: https://android.googlesource.com/platform/frameworks/base/+/282e181b58cf72b6ca770dc7ca5f91f135444502/tools/aapt/AaptAssets.cpp#61
            ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~'
        }
    }
    flavorDimensions "distribution"
    productFlavors {
        github {
            dimension "distribution"
        }
        fdroid {
            dimension "distribution"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    google()
    mavenCentral()
    maven { url "https://maven.google.com" }
    flatDir{
        dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    implementation project(':capacitor-android')
    implementation project(':capacitor-cordova-android-plugins')
    implementation 'com.squareup.okhttp3:okhttp:4.11.0'
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
    implementation "org.apache.cordova:framework:$cordovaAndroidVersion"
}

apply from: 'capacitor.build.gradle'

try {
    def servicesJSON = file('google-services.json')
    if (servicesJSON.text) {
        apply plugin: 'com.google.gms.google-services'
    }
} catch(Exception e) {
    logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work")
}
