#pragma once

#include "base/base_core.h"
#include "base/base_math.h"

////////////////////////////////
// Input — accumulated per frame, consumed by the app each tick.

#define PLATFORM_MAX_CHARS_PER_FRAME 64
#define PLATFORM_MAX_KEYS_PER_FRAME  32

// Virtual key codes (subset matching Win32 VK_ codes)
enum {
    PKEY_BACKSPACE = 0x08,
    PKEY_TAB       = 0x09,
    PKEY_RETURN    = 0x0D,
    PKEY_ESCAPE    = 0x1B,
    PKEY_DELETE    = 0x2E,
    PKEY_LEFT      = 0x25,
    PKEY_UP        = 0x26,
    PKEY_RIGHT     = 0x27,
    PKEY_DOWN      = 0x28,
    PKEY_HOME      = 0x24,
    PKEY_END       = 0x23,
    PKEY_A         = 0x41,
    PKEY_C         = 0x43,
    PKEY_V         = 0x56,
    PKEY_X         = 0x58,
    PKEY_0         = 0x30,
    PKEY_EQUAL     = 0xBB,  // '='/'+'  (VK_OEM_PLUS)
    PKEY_MINUS     = 0xBD,  // '-'/'_'  (VK_OEM_MINUS)
};

struct PlatformInput {
    // Typed characters (UTF-16 code units, printable only)
    U16 chars[PLATFORM_MAX_CHARS_PER_FRAME];
    S32  char_count;

    // Key-down events (virtual key codes)
    U8  keys[PLATFORM_MAX_KEYS_PER_FRAME];
    S32  key_count;

    // Modifier state at time of last key event
    B32     ctrl_held;
    B32     shift_held;

    // Mouse state (polled per frame)
    Vec2F32  mouse_pos;
    Vec2F32  scroll_delta;
    B32      mouse_down;
    B32      was_mouse_down;
};

struct PlatformWindow;

struct PlatformWindowDesc {
    const char *title = "autosample";
    S32 width     = 1280;
    S32 height    = 720;
};

struct PlatformMenuItem {
    const char *label;      // nullptr = separator
    S32     id;         // command ID (ignored for separators)
};

struct PlatformMenu {
    const char        *label;
    PlatformMenuItem  *items;
    S32            item_count;
};

// Called by the platform layer when the window needs a frame rendered
// (e.g., during a live resize). user_data is the pointer passed to
// platform_set_frame_callback.
typedef void (*PlatformFrameCallback)(void *user_data);

PlatformWindow *platform_create_window(PlatformWindowDesc *desc);
void            platform_destroy_window(PlatformWindow *window);
B32            platform_poll_events(PlatformWindow *window);
void            platform_get_size(PlatformWindow *window, S32 *w, S32 *h);
void           *platform_get_native_handle(PlatformWindow *window);

void            platform_set_frame_callback(PlatformWindow *window, PlatformFrameCallback cb, void *user_data);
void            platform_set_menu(PlatformWindow *window, PlatformMenu *menus, S32 menu_count);
S32         platform_poll_menu_command(PlatformWindow *window);

// Returns accumulated input since last call (keyboard events + polled mouse state), then clears the buffer.
PlatformInput platform_get_input(PlatformWindow *window);

// Cursor shapes for resize handles
enum PlatformCursor {
    PLATFORM_CURSOR_ARROW   = 0,
    PLATFORM_CURSOR_SIZE_WE = 1,  // horizontal resize
    PLATFORM_CURSOR_SIZE_NS = 2,  // vertical resize
};

void platform_set_cursor(PlatformCursor cursor);

// DPI scale factor for the window's current monitor.
// Returns 1.0 at 96 DPI (100%), 1.5 at 144 DPI (150%), etc.
F32 platform_get_dpi_scale(PlatformWindow *window);

// Clipboard operations (null-terminated UTF-8 strings).
// platform_clipboard_set copies text to the system clipboard.
// platform_clipboard_get returns a pointer to a static buffer (valid until next call), or nullptr.
void        platform_clipboard_set(const char *text);
const char *platform_clipboard_get();
