import java.util.Properties

plugins {
    alias(libs.plugins.android.application)
}

android {
    namespace = "de.ladkau.schwertundmagieonpebblecompanionapp"
    compileSdk {
        version = release(36) {
            minorApiLevel = 1
        }
    }

    ndkVersion = "30.0.14904198"

    defaultConfig {
        applicationId = "de.ladkau.schwertundmagieonpebblecompanionapp"
        minSdk = 24
        targetSdk = 36
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

        ndk {
            abiFilters += listOf("arm64-v8a", "x86_64")
        }
        externalNativeBuild {
            cmake {
                arguments("-DANDROID_STL=c++_shared")
                // HAVE_VICE_SRC is detected automatically by CMakeLists.txt
                // based on whether vice-libs/<abi>/libvice.a exists.
            }
        }
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }
    externalNativeBuild {
        cmake {
            path = file("src/main/jni/CMakeLists.txt")
            version = "3.22.1"
        }
    }
}

// ---------------------------------------------------------------------------
// VICE cross-compilation task
//
// Runs automatically before any CMake build step.
// Skipped entirely if vice-libs/<abi>/libvice.a already exists.
// The tarball at <project>/res/vice-3.8.tar.gz is unpacked by build_vice.sh.
// ---------------------------------------------------------------------------

val viceLibArm   = layout.projectDirectory.file("src/main/jni/vice-libs/arm64-v8a/libvice.a")
val viceLibX86   = layout.projectDirectory.file("src/main/jni/vice-libs/x86_64/libvice.a")
val viceTarball  = layout.projectDirectory.file("../res/vice-3.8.tar.gz")

tasks.register("buildVice") {
    group = "build"
    description = "Cross-compile VICE 3.8 headless for Android (ARM64 + x86_64)"

    inputs.file(viceTarball)
    outputs.file(viceLibArm)
    outputs.file(viceLibX86)

    doLast {
        if (viceLibArm.asFile.exists() && viceLibX86.asFile.exists()) {
            logger.lifecycle("VICE already built — skipping")
            return@doLast
        }

        val localProps = Properties().apply {
            rootProject.file("local.properties").takeIf { it.exists() }
                ?.reader()?.use { load(it) }
        }
        val sdkDir = localProps.getProperty("sdk.dir")
            ?: System.getenv("ANDROID_HOME")
            ?: ""
        val ndkPath = localProps.getProperty("ndk.dir")
            ?: System.getenv("ANDROID_NDK_HOME")
            ?: System.getenv("ANDROID_NDK_ROOT")
            ?: System.getenv("NDK")
            ?: sdkDir.takeIf { it.isNotEmpty() }?.let { sdk ->
                // NDK side-by-side: pick the first installed version
                file("$sdk/ndk").takeIf { it.isDirectory }
                    ?.listFiles()?.sorted()?.lastOrNull()?.absolutePath
                    ?: "$sdk/ndk-bundle".takeIf { file("$sdk/ndk-bundle").isDirectory }
            }
            ?: throw GradleException(
                "Android NDK not found.\n" +
                "Install via: Android Studio → SDK Manager → SDK Tools → NDK (Side by side)"
            )
        logger.lifecycle("Building VICE with NDK at $ndkPath …")

        val proc = ProcessBuilder("bash", "build_vice.sh")
            .directory(layout.projectDirectory.dir("src/main/jni").asFile)
            .redirectErrorStream(true)
            .also { it.environment()["NDK"] = ndkPath }
            .start()
        proc.inputStream.bufferedReader().forEachLine { logger.lifecycle(it) }
        val exit = proc.waitFor()
        if (exit != 0) throw GradleException("build_vice.sh failed (exit $exit)")

        // Touch CMakeLists.txt so CMake re-evaluates the EXISTS check on the
        // next build and picks up the newly created libvice.a.
        layout.projectDirectory.file("src/main/jni/CMakeLists.txt")
            .asFile.setLastModified(System.currentTimeMillis())
        logger.lifecycle("VICE build complete — CMakeLists.txt touched for re-evaluation")
    }
}

// Run buildVice before any CMake configure or build step.
tasks.whenTaskAdded {
    if (name.startsWith("configureCMake") || name.startsWith("buildCMake") ||
        name.startsWith("externalNativeBuild")) {
        dependsOn("buildVice")
    }
}

// ---------------------------------------------------------------------------
// ROM extraction task
//
// Extracts C64 and drive ROMs from the bundled VICE tarball into the Android
// assets directory so they are packaged into the APK automatically.
// Output files are gitignored — the tarball is the single source of truth.
// ---------------------------------------------------------------------------

val assetsDir = layout.projectDirectory.dir("src/main/assets")

tasks.register<Copy>("extractViceRoms") {
    group = "build"
    description = "Extract C64 and 1541 ROMs from the VICE tarball into assets/"

    from(tarTree(viceTarball.asFile)) {
        include("vice-3.8/data/C64/kernal-901227-03.bin")
        include("vice-3.8/data/C64/basic-901226-01.bin")
        include("vice-3.8/data/C64/chargen-901225-01.bin")
        include("vice-3.8/data/DRIVES/dos1541-325302-01+901229-05.bin")
        eachFile {
            // Flatten the tarball directory structure: place each ROM directly
            // in assets/ with its short name rather than the versioned filename.
            relativePath = RelativePath(true, when (name) {
                "kernal-901227-03.bin"            -> "kernal"
                "basic-901226-01.bin"             -> "basic"
                "chargen-901225-01.bin"           -> "chargen"
                "dos1541-325302-01+901229-05.bin" -> "1541"
                else -> name
            })
        }
        includeEmptyDirs = false
    }
    into(assetsDir)
}

// Run extractViceRoms before assets are merged into the APK.
tasks.whenTaskAdded {
    if (name.startsWith("merge") && name.endsWith("Assets")) {
        dependsOn("extractViceRoms")
    }
}

dependencies {
    implementation(libs.androidx.activity.ktx)
    implementation(libs.androidx.appcompat)
    implementation(libs.androidx.constraintlayout)
    implementation(libs.androidx.core.ktx)
    implementation(libs.material)
    implementation(libs.nanohttpd)
    implementation(libs.androidx.documentfile)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(libs.androidx.junit)
}
