apply plugin: 'com.android.application'

// Version from scripts/native-build.js (2.4.0 → 2040000); version.properties covers Android Studio builds.
def versionProps = new Properties()
def versionPropsFile = file('version.properties')
if (versionPropsFile.exists()) {
    versionPropsFile.withReader('UTF-8') { versionProps.load(it) }
}
def rangookVersionName = findProperty('rangookVersionName') ?: versionProps.getProperty('versionName') ?: '0.0.0-dev'
def rangookVersionCode = (findProperty('rangookVersionCode') ?: versionProps.getProperty('versionCode') ?: '1') as Integer

// One signing key for every store, from android/keystore.properties (gitignored) or RANGOOK_* env vars.
def keystoreProps = new Properties()
def keystorePropsFile = rootProject.file('keystore.properties')
if (keystorePropsFile.exists()) {
    keystorePropsFile.withReader('UTF-8') { keystoreProps.load(it) }
}
def signingValue = { String key, String env ->
    keystoreProps.getProperty(key) ?: System.getenv(env)
}
def releaseStoreFile = signingValue('storeFile', 'RANGOOK_KEYSTORE_FILE')
def hasReleaseSigning = releaseStoreFile != null

android {
    namespace = "ir.rangook.app"
    compileSdk = rootProject.ext.compileSdkVersion

    buildFeatures {
        // RangookAppPlugin reads the channel and version from BuildConfig.
        buildConfig = true
    }

    defaultConfig {
        // Same as appId in capacitor.config.ts; permanent after the first upload.
        applicationId "ir.rangook.app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rangookVersionCode
        versionName rangookVersionName
        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:!*~'
        }
    }

    // One flavor per store; only DISTRIBUTION_CHANNEL differs. A new store also needs
    // src/lib/platform/channels.ts and public/app-update.json.
    flavorDimensions = ["channel"]
    productFlavors {
        bazaar {
            dimension "channel"
            buildConfigField "String", "DISTRIBUTION_CHANNEL", '"bazaar"'
        }
        myket {
            dimension "channel"
            buildConfigField "String", "DISTRIBUTION_CHANNEL", '"myket"'
        }
        // The website APK; updates download in the browser, so no install permission is needed.
        direct {
            dimension "channel"
            buildConfigField "String", "DISTRIBUTION_CHANNEL", '"direct"'
        }
    }

    signingConfigs {
        if (hasReleaseSigning) {
            release {
                storeFile rootProject.file(releaseStoreFile)
                storePassword signingValue('storePassword', 'RANGOOK_KEYSTORE_PASSWORD')
                keyAlias signingValue('keyAlias', 'RANGOOK_KEY_ALIAS')
                keyPassword signingValue('keyPassword', 'RANGOOK_KEY_PASSWORD')
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (hasReleaseSigning) {
                signingConfig signingConfigs.release
            }
        }
    }
}

repositories {
    flatDir{
        dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    implementation "androidx.coordinatorlayout:coordinatorlayout:$androidxCoordinatorLayoutVersion"
    implementation "androidx.core:core-splashscreen:$coreSplashScreenVersion"
    implementation project(':capacitor-android')
    testImplementation "junit:junit:$junitVersion"
    androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
    androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
    implementation project(':capacitor-cordova-android-plugins')
}

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")
}
