buildscript {
    ext.kotlin_version = '2.0.21'

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.7.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.2'
        classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.2'
        classpath 'com.google.firebase:perf-plugin:1.4.2'
    }
}

plugins {
    id 'com.google.devtools.ksp' version '2.0.21-1.0.28'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: 'versioning.gradle'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.firebase.firebase-perf'

repositories {
    google()
    mavenCentral()
}
dependencies {
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.7.0'

    implementation 'androidx.constraintlayout:constraintlayout:2.2.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.8.7'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.7'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.8.7'
    implementation 'androidx.preference:preference-ktx:1.2.1'
    implementation 'androidx.core:core-ktx:1.15.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.multidex:multidex:2.0.1'

    implementation 'com.google.android.material:material:1.12.0'

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0'

    implementation 'com.lazygeniouz:dfc:1.1'

    implementation 'uk.co.armedpineapple.innoextract:service:2.1.0'

    implementation 'com.github.bumptech.glide:glide:4.16.0'

    implementation 'com.google.firebase:firebase-crashlytics:19.3.0'
    implementation 'com.google.firebase:firebase-crashlytics-ndk:19.3.0'
    implementation 'com.google.firebase:firebase-analytics:22.1.2'
    implementation 'com.google.firebase:firebase-perf:21.0.3'

    implementation 'com.google.android.gms:play-services-games-v2:20.1.2'

    def room_version = "2.6.1"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    ksp "androidx.room:room-compiler:$room_version"
}

configurations.configureEach {
    exclude group: 'com.google.android.gms', module : 'play-services-ads'
    exclude group: 'com.google.android.gms', module : 'play-services-ads-identifier'
    exclude group: 'com.google.android.gms', module : 'play-services-ads-lite'
}

android {
    namespace 'uk.co.armedpineapple.cth'
    defaultConfig {
        minSdkVersion 27
        targetSdkVersion 35

        versionName = project.ext.buildVersion
        versionCode = project.ext.offsetBuildVersionCode
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

    signingConfigs {
        if (System.getenv('RELEASE_STORE_FILE') != null) {
            release {
                storeFile file(System.getenv("RELEASE_STORE_FILE"))
                storePassword System.getenv("RELEASE_STORE_PASSWORD")
                keyAlias System.getenv("RELEASE_KEY_ALIAS")
                keyPassword System.getenv("RELEASE_KEY_PASSWORD")
            }
        }
    }

    buildTypes {
        release {
            buildConfigField "boolean", "ALWAYS_UPGRADE", "false"
            if (System.getenv('RELEASE_STORE_FILE') != null) {
                signingConfig signingConfigs.release
            } else {
                signingConfig signingConfigs.debug
            }
            firebaseCrashlytics {
                nativeSymbolUploadEnabled true
            }
        }

        debug {
            buildConfigField "boolean", "ALWAYS_UPGRADE", "true"
        }
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src/Java', 'jni/SDL/android-project/app/src/main/java']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }

    applicationVariants.configureEach { variant ->
        tasks["merge${variant.name.capitalize()}Assets"]
                .dependsOn(createGameZip)
    }


    externalNativeBuild {
        ndkBuild {
            path 'jni/Android.mk'
        }
    }
    buildFeatures {
        viewBinding true
        dataBinding true
        buildConfig true
    }
    androidResources {
        noCompress += 'zip'
    }
    lint {
        abortOnError false
        checkReleaseBuilds false
    }
    compileSdk 35
}


tasks.register('saveVersion') {
    new File(projectDir, "version.txt").text = project.ext.buildVersion
}

preBuild.dependsOn(saveVersion)

// Create ZIP of CorsixTH game assets
tasks.register('createGameZip', Zip) {

    destinationDirectory = file('assets')
    archiveFileName = 'game.zip'

    from('jni/CorsixTH/CorsixTH') {
        include 'Levels/**'
        include 'Campaigns/**'
        include 'Lua/**'
        include 'CorsixTH.lua'
        include 'Bitmap/**'
        include 'Campaigns/**'
        include 'Graphics/**'
    }
}

ksp {
    arg(new RoomSchemaArgProvider(new File(projectDir, "schemas")))
}

class RoomSchemaArgProvider implements CommandLineArgumentProvider {

    @InputDirectory
    @PathSensitive(PathSensitivity.RELATIVE)
    File schemaDir

    RoomSchemaArgProvider(File schemaDir) {
        this.schemaDir = schemaDir
    }

    @Override
    Iterable<String> asArguments() {
        return ["room.schemaLocation=${schemaDir.path}".toString()]
    }
}