apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-parcelize'
apply plugin: 'kotlin-kapt'

def rootCMakeLists = "../../CMakeLists.txt"
def rootCMakeListsContent = file(rootCMakeLists).text
static def grepVersionComponent(content, varname) {
    def match = content =~ /set\($varname ([0-9]+)\)/
    if(!match.find())
        throw new GradleException("Failed to find $varname in CMakeLists.txt")
    return match.group(1)
}
def chiakiVersionMajor = grepVersionComponent(rootCMakeListsContent, "CHIAKI_VERSION_MAJOR")
def chiakiVersionMinor = grepVersionComponent(rootCMakeListsContent, "CHIAKI_VERSION_MINOR")
def chiakiVersionPatch = grepVersionComponent(rootCMakeListsContent, "CHIAKI_VERSION_PATCH")
def chiakiVersion = "$chiakiVersionMajor.$chiakiVersionMinor.$chiakiVersionPatch"
println("Determined Chiaki Version: $chiakiVersion")

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.metallic.chiaki"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 12
        versionName chiakiVersion+"-axi-240628"
        externalNativeBuild {
            cmake {
                arguments "-DCHIAKI_ENABLE_TESTS=OFF",
                        "-DCHIAKI_ENABLE_CLI=OFF",
                        "-DCHIAKI_ENABLE_GUI=OFF",
                        "-DCHIAKI_ENABLE_SETSU=OFF",
                        "-DCHIAKI_ENABLE_ANDROID=ON",
                        "-DCHIAKI_ENABLE_FFMPEG_DECODER=OFF",
                        "-DCHIAKI_ENABLE_PI_DECODER=OFF",
                        "-DCHIAKI_LIB_ENABLE_OPUS=OFF",
                        "-DCHIAKI_LIB_OPENSSL_EXTERNAL_PROJECT=ON"
            }
        }
    }
    buildFeatures {
        viewBinding true
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
    externalNativeBuild {
        cmake {
            version "3.22.1"
            path rootCMakeLists
        }
    }
    signingConfigs {
        release
    }
    buildTypes {
        debug{
//            applicationIdSuffix = ".axi"
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }


    Properties properties = new Properties()
    def propertiesFile = file("../local.properties")
    if (propertiesFile.exists()) {
        properties.load(propertiesFile.newDataInputStream())
    }

	if(properties.containsKey("chiakiKeystore")) {
        println("Enabling Local Signing")
        buildTypes.release.signingConfig = signingConfigs.release
        buildTypes.debug.signingConfig = signingConfigs.release
        signingConfigs.release.storeFile = file(properties.get("chiakiKeystore"))
        signingConfigs.release.storePassword = properties.get("chiakiKeystorePW")
        signingConfigs.release.keyAlias = properties.get("chiakiKeyAlias")
        signingConfigs.release.keyPassword = properties.get("chiakiKeyPW")
    } else {
        println("Signing not enabled")
    }
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        freeCompilerArgs += "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'
    implementation 'androidx.preference:preference:1.2.0'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    implementation 'androidx.lifecycle:lifecycle-reactivestreams:2.5.1'
    implementation "io.reactivex.rxjava2:rxjava:2.2.20"
    implementation "io.reactivex.rxjava2:rxkotlin:2.4.0"
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    def room_version = "2.5.0"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    implementation "androidx.room:room-rxjava2:$room_version"
    implementation "com.squareup.moshi:moshi:1.14.0"
    kapt "com.squareup.moshi:moshi-kotlin-codegen:1.14.0"
}
