apply plugin: 'com.android.application'
apply from: '../variables.gradle'

static def getVersionCodeFromVersionName(String versionName) {
    def versionParts = versionName.split("\\.")
    while (versionParts.size() < 3) {
        versionParts += "0"
    }
    return versionParts[0].toInteger() * 10000 + versionParts[1].toInteger() * 100 + versionParts[2].toInteger()
}

android {
    namespace "tv.megacubo.app"
    compileSdk rootProject.ext.compileSdk
    defaultConfig {
        applicationId "tv.megacubo.app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionName '17.6.1'
        versionCode getVersionCodeFromVersionName(versionName)
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        aaptOptions {
            ignoreAssetsPattern '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~'
        }
        vectorDrawables.useSupportLibrary = true
        resConfigs "en"
    }
    buildTypes {
        debug {
            ndk {
                abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
            }
        }
        release {
            ndk {
                abiFilters 'armeabi-v7a', 'arm64-v8a'
            }
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    splits {
        abi {
            enable false
            reset()
            include 'armeabi-v7a', 'arm64-v8a', 'x86_64'
            universalApk true
        }
    }
    packagingOptions {
        jniLibs {
            useLegacyPackaging = true
            pickFirsts += [
                'lib/armeabi-v7a/libnative-lib.so',
                'lib/armeabi-v7a/libc++_shared.so',
                'lib/arm64-v8a/libnative-lib.so',
                'lib/arm64-v8a/libc++_shared.so',
                'lib/x86_64/libnative-lib.so',
                'lib/x86_64/libc++_shared.so'
            ]
            excludes += ['**/*~', 'lib/x86/**']
        }
        resources {
            excludes += [
                'META-INF/NOTICE',
                'META-INF/LICENSE',
                'META-INF/DEPENDENCIES',
                'META-INF/LICENSE.txt',
                'META-INF/NOTICE.txt',
                'META-INF/ASL2.0',
                '**/.DS_Store',
                '**/thumbs.db',
                '**/*.bak',
                '**/*~'
            ]
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = [
                'src/main/jniLibs',
                '../capacitor-cordova-android-plugins/src/main/libs',
                'libs'
            ]
        }
    }
}

configurations.all {
    resolutionStrategy {
        force 'androidx.constraintlayout:constraintlayout:2.1.4'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    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 ignored) {
    logger.info("google-services.json not found, google-services plugin not applied. Push Notifications won't work")
}