import com.github.triplet.gradle.androidpublisher.ReleaseStatus

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"

final int versionMajor = 2
final int versionMinor = 0
final int versionPatch = getBuildNumber()
final int androidVersionCode = getBuildNumber()

final String semanticVersion = "${versionMajor}.${versionMinor}.${versionPatch}"

repositories {
    google()
    mavenCentral()
    maven { url "https://repo.commonsware.com.s3.amazonaws.com" }
    maven { url "https://s3.amazonaws.com/repo.commonsware.com" }
}

android {
    buildFeatures {
        buildConfig = true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }

    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'
        }
    }
    androidResources {
        noCompress 'zip'
    }
    lint {
        lintConfig file("$rootDir/buildsystem/codequality/lint.xml")
    }
    namespace 'uk.co.alt236.btlescan'

    defaultConfig {
        versionCode androidVersionCode
        versionName semanticVersion
    }

    buildTypes {
        release {
            minifyEnabled false
            resValue "string", "app_name", "Bluetooth LE Scanner"
            if(isRunningOnCi()) {
                signingConfig signingConfigs.release
            }
        }

        debug {
            minifyEnabled false
            applicationIdSuffix ".debug"
            resValue "string", "app_name", "Debug Bluetooth LE Scanner"
            signingConfig signingConfigs.debug
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = 17
    }
}

dependencies {
    implementation project(':library')

    implementation libs.androidx.appcompat
    implementation libs.androidx.recyclerview
    implementation libs.permissionx
    implementation libs.easycursor.android

    implementation libs.hilt.android
    kapt libs.hilt.compiler

    testImplementation libs.junit4
    testImplementation libs.mockito
}

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}/sample_app/build/outputs/apk/release/")
    } else {
        enabled = false
    }
}