def buildAsLibrary = project.hasProperty('BUILD_AS_LIBRARY');
def buildAsApplication = !buildAsLibrary
if (buildAsApplication) {
    apply plugin: 'com.android.application'
} else {
    apply plugin: 'com.android.library'
}

android {
    if (buildAsApplication) {
        namespace "com.distractionware.vvvvvv"
    }
    compileSdkVersion 34
    defaultConfig {
        minSdkVersion 29
        targetSdkVersion 34
        versionCode 20004000
        versionName "2.4"
        applicationId "air.com.distractionware.vvvvvvmobile"
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_STL=c++_shared"
            }
        }
    }
    buildFeatures {
        prefab true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    applicationVariants.all { variant ->
        tasks["merge${variant.name.capitalize()}Assets"]
                .dependsOn("externalNativeBuild${variant.name.capitalize()}")
    }
    if (!project.hasProperty('EXCLUDE_NATIVE_LIBS')) {
        sourceSets.main {
            jniLibs.srcDir 'libs'
        }
        externalNativeBuild {
            cmake {
                path 'jni/CMakeLists.txt'
            }
        }

    }
    lint {
        abortOnError false
    }
    aaptOptions {
        noCompress 'zip'
    }

    if (buildAsLibrary) {
        libraryVariants.all { variant ->
            variant.outputs.each { output ->
                def outputFile = output.outputFile
                if (outputFile != null && outputFile.name.endsWith(".aar")) {
                    def fileName = "org.libsdl.app.aar";
                    output.outputFile = new File(outputFile.parent, fileName);
                }
            }
        }
    }

    splits {
        abi {
            enable true
        }
    }

    sourceSets {
        main {
            assets {
                srcDir layout.buildDirectory.dir("generated/main/assets")
            }
        }
    }
}

def zipRepoAssetsTask = tasks.register("zipRepoAssets", Zip) {
    from('../../fonts') { spec ->
        spec.into('graphics')
    }
    from('../../lang') { spec ->
        spec.into('lang')
    }
    archiveFileName.set('repo.zip')
    destinationDirectory.value(layout.buildDirectory.dir("generated/main/assets"))
}

project.android.applicationVariants.configureEach { variant ->
    def compressAssetsTask = project.tasks.named("merge${variant.name.capitalize()}Assets")
    compressAssetsTask.configure { task ->
        task.dependsOn zipRepoAssetsTask
    }
}

afterEvaluate {
    [lintReportDebug, lintAnalyzeDebug, lintReportRelease, lintAnalyzeRelease, lintVitalReportRelease, lintVitalAnalyzeRelease].each { task ->
        task.dependsOn zipRepoAssetsTask
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'org.jetbrains:annotations:15.0'
    implementation 'androidx.core:core:1.10.1'
    implementation 'androidx.exifinterface:exifinterface:1.3.6'
    implementation 'org.libsdl.android:SDL2:2.28.5'
}