plugins {
    id 'com.android.application'
}

android {
    namespace 'io.github.divverent.aaaaxy'
    compileSdk 35
    ndkVersion "27.0.12077973"

    defaultConfig {
        applicationId 'io.github.divverent.aaaaxy'
        // minSdk 21  // What actually is required.
        minSdk 23  // For now, restrict to newer devices.
        targetSdk 35  // Play Store requirement.
        def rootDir = new File("${project.rootDir}/../..")
        def prefix =
          (aaaaxy_build_use_version_file == 'true' ? 'env AAAAXY_BUILD_USE_VERSION_FILE=true ' : '') +
          (aaaaxy_build_versioncode_add != '' ? 'env AAAAXY_BUILD_VERSIONCODE_ADD=' + aaaaxy_build_versioncode_add + ' ' : '')
        versionCode "${prefix}scripts/version.sh android".execute(null, rootDir).text.trim().toInteger()
        versionName "${prefix}scripts/version.sh semver".execute(null, rootDir).text.trim()
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        resourceConfigurations += ['en', 'de', 'de-rCH', 'la', 'pt', 'pt-rBR', 'be', 'b+be+Latn', 'uk', 'ar', 'ar-rEG', 'ja', 'b+zh+Hans', 'b+zh+Hant']
    }
    signingConfigs {
        release {
            storeFile file("${aaaaxy_keystore}")
            storePassword "${aaaaxy_keystore_password}"
            keyAlias "${aaaaxy_key_alias}"
            keyPassword "${aaaaxy_key_password}"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

task goGenerate(type: Exec) {
    environment 'CGO_CPPFLAGS', '-DNDEBUG'
    environment 'CGO_CFLAGS', '-g0 -O3'
    environment 'CGO_CXXFLAGS', '-g0 -O3'
    environment 'CGO_LDFLAGS', '-g0 -O3'
    environment 'GOOS', 'android'
    if (aaaaxy_build_use_version_file == 'true') {
        environment 'AAAAXY_BUILD_USE_VERSION_FILE', 'true'
    }
    if (aaaaxy_build_versioncode_add != '') {
        environment 'AAAAXY_BUILD_VERSIONCODE_ADD', aaaaxy_build_versioncode_add
    }
    commandLine 'go', 'generate', '-tags', aaaaxy_build_tags, '../../..'
}

task prepareLibsDir(type: Exec) {
    commandLine 'mkdir', '-p', 'libs'
}

task ebitenMobile(type: Exec) {
    dependsOn prepareLibsDir
    dependsOn goGenerate
    environment 'ANDROID_NDK_HOME', android.ndkDirectory
    environment 'CGO_CPPFLAGS', '-DNDEBUG'
    environment 'CGO_CFLAGS', '-g0 -O3'
    environment 'CGO_CXXFLAGS', '-g0 -O3'
    environment 'CGO_LDFLAGS', '-g0 -O3'
    environment 'GO386', 'sse2'
    commandLine '../../../scripts/ebitenmobile.sh', 'bind',
        '-target', aaaaxy_build_gomobile_targets,
        '-javapkg', 'io.github.divverent.aaaaxy',
        '-o', 'libs/aaaaxy.aar',
        '-androidapi', '21',
        '-tags', aaaaxy_build_tags,
        '-gcflags=all=-dwarf=false',
        '-ldflags=all=-s -w -buildid= "-extldflags=-z max-page-size=16384"',
        '-a',
        '-trimpath',
        // '-buildvcs=false',  # Not supported by ebitenmobile.
        'github.com/divVerent/aaaaxy/AndroidStudioProjects/AAAAXY/app/src/main/go/aaaaxy'
}

task prepareAssetsDir(type: Exec) {
    commandLine 'mkdir', '-p', 'src/main/assets'
}

task getAssets(type: Exec) {
    dependsOn prepareAssetsDir
    dependsOn goGenerate
    if (aaaaxy_build_tags == 'zip') {
        commandLine 'cp', '--reflink=auto', '../../../aaaaxy.dat', 'src/main/assets/aaaaxy.dat'
    } else {
        commandLine 'rm', '-f', 'src/main/assets/aaaaxy.dat'
    }
}

tasks.whenTaskAdded { theTask ->
    if (theTask.name.equals("compileReleaseJavaWithJavac") || theTask.name.equals("compileDebugJavaWithJavac") || theTask.name.equals("mergeReleaseResources") || theTask.name.equals("mergeDebugResources") || theTask.name.equals("mergeReleaseNativeLibs") || theTask.name.equals("mergeDebugNativeLibs")) {
        theTask.dependsOn ebitenMobile
        theTask.dependsOn getAssets
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation files('libs/aaaaxy.aar')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
}
