import com.android.build.gradle.tasks.MergeSourceSetFolders

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}


def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

def defStoreFile = file("keystore.jks")
def defStorePassword = localProperties.getProperty('storePassword')
def defKeyAlias = localProperties.getProperty('keyAlias')
def defKeyPassword = localProperties.getProperty('keyPassword')
def isRelease = defStoreFile.exists() && defStorePassword != null && defKeyAlias != null && defKeyPassword != null

android {
    namespace "net.errorx.vpn"
    compileSdkVersion 35
    ndkVersion "27.1.12297006"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = '17'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    signingConfigs {
        if (isRelease) {
            release {
                storeFile defStoreFile
                storePassword defStorePassword
                keyAlias defKeyAlias
                keyPassword defKeyPassword
            }
        }
    }


    defaultConfig {
        applicationId "net.errorx.vpn"
        minSdkVersion 21
        targetSdkVersion 35
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        debug {
            minifyEnabled false
            applicationIdSuffix '.debug'
        }
        release {
            if (isRelease) {
                signingConfig signingConfigs.release
            } else {
                signingConfig signingConfigs.debug
            }
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
        }
    }
}

tasks.register('copyNativeLibs', Copy) {
    delete('src/main/jniLibs')
    from('../../libclash/android')
    into('src/main/jniLibs')
}

tasks.withType(MergeSourceSetFolders).configureEach {
    dependsOn copyNativeLibs
}

flutter {
    source '../..'
}

dependencies {
    implementation 'androidx.core:core-splashscreen:1.0.1'
    implementation 'com.google.code.gson:gson:2.10'
    implementation("com.android.tools.smali:smali-dexlib2:3.0.7") {
        exclude group: "com.google.guava", module: "guava"
    }
    implementation "androidx.work:work-runtime-ktx:2.9.0"
}


afterEvaluate {
    assembleDebug.dependsOn copyNativeLibs

    assembleRelease.dependsOn copyNativeLibs
}
