#include <android/log.h>
#include <android/native_window.h>
#include <android_native_app_glue.h>

static void draw_color(ANativeWindow* win, uint32_t argb) {
    if (!win) return;
    ANativeWindow_setBuffersGeometry(win, 0, 0, WINDOW_FORMAT_RGBA_8888);
    ANativeWindow_Buffer buf;
    if (ANativeWindow_lock(win, &buf, NULL) != 0) return;
    uint32_t* p = (uint32_t*)buf.bits;
    for (int y = 0; y < buf.height; ++y) {
        for (int x = 0; x < buf.stride; ++x) {
            p[y * buf.stride + x] = argb; //  0xAARRGGBB
        }
    }
    ANativeWindow_unlockAndPost(win);
}

static void handle_cmd(struct android_app* app, int32_t cmd) {
    switch (cmd) {
        case APP_CMD_INIT_WINDOW:
            if (app->window) draw_color(app->window, 0xFF2222AA); // opaque blue-ish
            break;
        case APP_CMD_TERM_WINDOW:
            break;
        case APP_CMD_RESUME:
            __android_log_print(ANDROID_LOG_INFO, "HelloNative", "Hello from NativeActivity!");
            break;
    }
}

void android_main(struct android_app* app) {
    app->onAppCmd = handle_cmd;

    int events;
    struct android_poll_source* source;
    for (;;) {
        int id = ALooper_pollOnce(-1, NULL, &events, (void**)&source);
        if (id >= 0 && source) source->process(app, source);
        if (app->destroyRequested) return;
    }
}
