plugins {
    id 'com.android.application'
    alias(libs.plugins.kotlin.android)
    alias(libs.plugins.kotlin.kapt)
    alias(libs.plugins.kotlin.parcelize)
    alias(libs.plugins.hilt.android)
    alias(libs.plugins.triplet.play)
}

apply from: "${project.rootDir}/buildsystem/android-defaults.gradle"

android {

    buildFeatures {
        buildConfig = true
    }

    defaultConfig {
        def buildNumber = getBuildNumber()
        multiDexEnabled true

        namespace "aws.apps.usbDeviceEnumerator"
        versionCode buildNumber
        versionName "3.0.0." + buildNumber
    }

    signingConfigs {
        release {
            storeFile file(System.getenv("ANDROID_KEYSTORE") ?: "[KEY_NOT_DEFINED]")
            storePassword System.getenv("KEYSTORE_PASSWORD")
            keyAlias System.getenv("KEY_ALIAS")
            keyPassword System.getenv("KEY_PASSWORD")
        }

        debug {
            storeFile file("${project.rootDir}/buildsystem/signing_keys/debug.keystore")
            keyAlias 'androiddebugkey'
            keyPassword 'android'
            storePassword 'android'
        }
    }

    dependencies {
        implementation project(':libs-jvm:usbdeviceenumerator')
        implementation project(':libs-android:usbmanager')
        implementation project(':libs-android:database')

        implementation libs.androidx.appcompat
        implementation libs.android.material
        implementation libs.android.support.multidex

        testImplementation libs.junit4

        androidTestImplementation libs.androidx.runner
        implementation libs.hilt.android
        kapt libs.hilt.compiler
    }

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

        debug {
            applicationIdSuffix ".debug"
            signingConfig signingConfigs.debug
            enableUnitTestCoverage true
            enableAndroidTestCoverage true
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = 17
    }

    // Allow references to generated code
    kapt {
        correctErrorTypes true
    }
    androidResources {
        noCompress 'zip'
    }
    lint {
        lintConfig file("$rootDir/buildsystem/codequality/lint.xml")
    }
}

import com.github.triplet.gradle.androidpublisher.ReleaseStatus
play {
    def credentialsPath = System.getenv("GPLAY_DEPLOY_KEY") ?: "[KEY_NOT_DEFINED]"
    def lastCommitMessage = getLastGitCommitMessage().take(50)
    logger.warn("GPP Config: $credentialsPath")
    logger.warn("Release Name: '$lastCommitMessage'")

    if (isRunningOnCi()) {
        enabled = true
        track = "internal"
        //userFraction = 1.0
        releaseStatus = ReleaseStatus.COMPLETED
        serviceAccountCredentials = file(credentialsPath)
        releaseName = lastCommitMessage
        artifactDir = file("${project.rootDir}/app/build/outputs/apk/release/")
    } else {
        enabled = false
    }
}
