// display_playdate.c - Cell integration for Playdate Display API
// Wraps pd_api_display.h functions for JavaScript access

#include "cell.h"
#include "common.h"

// --- Display API Wrappers ---

JSC_CCALL(display_getWidth,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    return JS_NewInt32(js, pd_display->getWidth());
)

JSC_CCALL(display_getHeight,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    return JS_NewInt32(js, pd_display->getHeight());
)

JSC_CCALL(display_setRefreshRate,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    float rate = (float)js2number(js, argv[0]);
    pd_display->setRefreshRate(rate);
    return JS_NULL;
)

JSC_CCALL(display_getRefreshRate,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    return JS_NewFloat64(js, pd_display->getRefreshRate());
)

JSC_CCALL(display_getFPS,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    return JS_NewFloat64(js, pd_display->getFPS());
)

JSC_CCALL(display_setInverted,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    int flag = JS_ToBool(js, argv[0]);
    pd_display->setInverted(flag);
    return JS_NULL;
)

JSC_CCALL(display_setScale,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    unsigned int scale = (unsigned int)js2number(js, argv[0]);
    pd_display->setScale(scale);
    return JS_NULL;
)

JSC_CCALL(display_setMosaic,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    unsigned int x = (unsigned int)js2number(js, argv[0]);
    unsigned int y = (unsigned int)js2number(js, argv[1]);
    pd_display->setMosaic(x, y);
    return JS_NULL;
)

JSC_CCALL(display_setFlipped,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    int x = JS_ToBool(js, argv[0]);
    int y = JS_ToBool(js, argv[1]);
    pd_display->setFlipped(x, y);
    return JS_NULL;
)

JSC_CCALL(display_setOffset,
    if (!pd_display) return JS_RaiseDisrupt(js, "display not initialized");
    int x = (int)js2number(js, argv[0]);
    int y = (int)js2number(js, argv[1]);
    pd_display->setOffset(x, y);
    return JS_NULL;
)

static const JSCFunctionListEntry js_display_funcs[] = {
    MIST_FUNC_DEF(display, getWidth, 0),
    MIST_FUNC_DEF(display, getHeight, 0),
    MIST_FUNC_DEF(display, setRefreshRate, 1),
    MIST_FUNC_DEF(display, getRefreshRate, 0),
    MIST_FUNC_DEF(display, getFPS, 0),
    MIST_FUNC_DEF(display, setInverted, 1),
    MIST_FUNC_DEF(display, setScale, 1),
    MIST_FUNC_DEF(display, setMosaic, 2),
    MIST_FUNC_DEF(display, setFlipped, 2),
    MIST_FUNC_DEF(display, setOffset, 2),
};

JSValue js_display_use(JSContext *js) {
    JSValue mod = JS_NewObject(js);
    JS_SetPropertyFunctionList(js, mod, js_display_funcs, countof(js_display_funcs));
    return mod;
}
