plugins {
    id 'com.android.application'
}

def resolveCcachePath = {
    def fromEnv = System.getenv('CCACHE') ?: System.getenv('NDK_CCACHE')
    if (fromEnv) {
        return fromEnv
    }
    if (System.getenv('ANDROIDCAST_CCACHE') in ['0', 'false', 'no', 'off']) {
        return null
    }
    try {
        def out = providers.exec {
            commandLine 'sh', '-c', 'command -v ccache || true'
        }.standardOutput.asText.get().trim()
        return out ?: null
    } catch (Exception ignored) {
        return null
    }
}()

def gitCommitShort = 'unknown'
def gitCommitAuthor = 'unknown'
def buildTimeEpochMs = System.currentTimeMillis()
def buildTimeZoneId = java.util.TimeZone.getDefault().getID()
def buildTimeDisplay = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", java.util.Locale.US)
        .format(new java.util.Date(buildTimeEpochMs))
def otaMajor = 0
def otaMinor = 1
def otaBuild = 0
def otaChannelUrlDefault = ''
def otaManifestUrlDefault = ''
try {
    def lpFile = rootProject.file('local.properties')
    if (lpFile.exists()) {
        def lp = new Properties()
        lp.load(new FileInputStream(lpFile))
        otaMajor = (lp.getProperty('ota.major', "${otaMajor}") ?: otaMajor) as int
        otaMinor = (lp.getProperty('ota.minor', "${otaMinor}") ?: otaMinor) as int
        otaBuild = (lp.getProperty('ota.build', "${otaBuild}") ?: otaBuild) as int
        otaChannelUrlDefault = lp.getProperty('ota.channel.url', '') ?: ''
        otaManifestUrlDefault = lp.getProperty('ota.manifest.url', '') ?: ''
    }
} catch (Exception ignored) {
}
def otaVersionCode = otaMajor * 10000 + otaMinor * 100 + otaBuild
def otaVersionName = "${otaMajor}.${otaMinor}.${otaBuild}"

try {
    gitCommitShort = ['git', 'rev-parse', '--short', 'HEAD'].execute(null, rootProject.projectDir).text.trim()
    gitCommitAuthor = ['git', 'log', '-1', '--format=%an'].execute(null, rootProject.projectDir).text.trim()
} catch (Exception ignored) {
}

android {
    namespace 'com.foxx.androidcast'
    compileSdk 34

    defaultConfig {
        applicationId "com.foxx.androidcast"
        minSdk 29
        targetSdk 34
        versionCode otaVersionCode
        versionName otaVersionName
        buildConfigField 'int', 'VERSION_MAJOR', "${otaMajor}"
        buildConfigField 'int', 'VERSION_MINOR', "${otaMinor}"
        buildConfigField 'int', 'VERSION_BUILD', "${otaBuild}"
        buildConfigField 'String', 'GIT_COMMIT_SHORT', "\"${gitCommitShort}\""
        buildConfigField 'String', 'GIT_COMMIT_AUTHOR', "\"${gitCommitAuthor.replace('\\\\', '\\\\\\\\').replace('\"', '\\\\\"')}\""
        buildConfigField 'long', 'BUILD_TIME_EPOCH_MS', "${buildTimeEpochMs}L"
        buildConfigField 'String', 'BUILD_TIME_ZONE_ID', "\"${buildTimeZoneId.replace('\\\\', '\\\\\\\\').replace('\"', '\\\\\"')}\""
        buildConfigField 'String', 'BUILD_TIME_DISPLAY', "\"${buildTimeDisplay.replace('\\\\', '\\\\\\\\').replace('\"', '\\\\\"')}\""
        buildConfigField 'String', 'OTA_CHANNEL_URL_DEFAULT', "\"${otaChannelUrlDefault.replace('\\\\', '\\\\\\\\').replace('\"', '\\\\\"')}\""
        buildConfigField 'String', 'OTA_MANIFEST_URL_DEFAULT', "\"${otaManifestUrlDefault.replace('\\\\', '\\\\\\\\').replace('\"', '\\\\\"')}\""
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared"
                if (resolveCcachePath) {
                    arguments "-DANDROID_CCACHE=${resolveCcachePath}"
                }
                cppFlags ""
            }
        }
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86_64'
        }
    }

    externalNativeBuild {
        cmake {
            path file("${rootProject.projectDir}/ndk/CMakeLists.txt")
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    buildFeatures {
        aidl true
        buildConfig true
    }

    packaging {
        resources {
            excludes += '/META-INF/versions/9/OSGI-INF/MANIFEST.MF'
        }
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = true
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.7.0'
    implementation 'com.google.android.material:material:1.12.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.2.0'
    implementation 'androidx.recyclerview:recyclerview:1.3.2'
    implementation 'com.google.android.gms:play-services-cronet:18.1.0'
    implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.5'
    implementation 'com.android.billingclient:billing:7.1.1'
    implementation 'com.google.android.play:review:2.0.2'

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.json:json:20240303'

    implementation 'com.github.mwiede:jsch:0.2.21'

    implementation project(':tunnel')
}
