/* webview_bridge: WebView2 Bridge 演示（加载外部 HTML/CSS/JS） */
#include "eduwin.h"

#define WIN_W  960
#define WIN_H  680
#define TOOL_H 42
#define STAT_H 24
#define BTN_W  40
#define BTN_H  30
#define PAD    4

static HWND g_wv;
static HWND g_url;
static HWND g_go;
static HWND g_status;
static HWND g_win;

/* ──── toolbar callbacks ──── */

static void on_back(void *d)       { webview_back(g_wv); }
static void on_forward(void *d)    { webview_forward(g_wv); }
static void on_reload(void *d)     { webview_reload(g_wv); }

static void on_go(void *d)
{
    const WCHAR *text = get_text(g_url);
    if (text && text[0])
    {
        set_text(g_status, L"Loading...");
        webview_navigate(g_wv, text);
    }
}

/* ──── resize ──── */

static void on_resize(int w, int h)
{
    int addr_w = w - (BTN_W + PAD) * 4;
    int go_x = (BTN_W + PAD) * 3 + PAD + addr_w + PAD;

    move(g_url, (BTN_W + PAD) * 3 + PAD, PAD);
    set_size(g_url, addr_w, BTN_H);
    move(g_go, go_x, PAD);

    set_size(g_wv, w, h - TOOL_H - STAT_H);
    move(g_status, 0, h - STAT_H);
    set_size(g_status, w, STAT_H);
}

/* ──── webview callbacks ──── */

static void on_ready(HWND hwv, void *d)
{
    /* 启用 Bridge（自动注入 eduwin.on_message / eduwin.call） */
    webview_bridge_init(hwv);

    /* 将当前 exe 目录映射为 eduwin.local，加载外部 HTML/CSS/JS */
    webview_set_virtual_host(hwv, L"eduwin.local", L".");

    set_text(g_url, L"https://eduwin.local/index.html");
    set_text(g_status, L"Loading...");
    webview_navigate(hwv, L"https://eduwin.local/index.html");
}

static void on_nav_complete(HWND hwv, int success, int status_code, void *d)
{
    (void)hwv; (void)status_code; (void)d;
    set_text(g_status, success ? L"Done" : L"Failed");
}

/* ──── Bridge 处理函数 ──── */

static void on_bridge_hello(HWND hwv, const char *func, const char *params, void *d)
{
    (void)func; (void)params; (void)d;
    webview_post_message(hwv,
        "{\"reply\":\"你好，eduwin bridge 工作正常！\"}");
}

static void on_bridge_get_time(HWND hwv, const char *func, const char *params, void *d)
{
    (void)func; (void)params; (void)d;
    SYSTEMTIME st;
    GetLocalTime(&st);
    WCHAR buf[64];
    str_format(buf, 64, L"\"%04d-%02d-%02d %02d:%02d:%02d\"",
               st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
    char *utf8 = wtou8(buf);
    if (utf8)
    {
        char resp[256];
        str_format_a(resp, 256, "{\"time\":%s}", utf8);
        webview_post_message(hwv, resp);
        free(utf8);
    }
}

static void on_bridge_add(HWND hwv, const char *func, const char *params, void *d)
{
    (void)func; (void)d;
    int a = 0, b = 0;
    json_t *root = json_parse(params);
    if (root)
    {
        a = json_get_int(root, "a");
        b = json_get_int(root, "b");
        json_free(root);
    }
    char resp[128];
    str_format_a(resp, 128, "{\"result\":%d}", a + b);
    webview_post_message(hwv, resp);
}

/* ──── main ──── */

int main(void)
{
    HWND btn_back, btn_fwd, btn_reload;

    webview_init();
    init_app();

    g_win = create_window(L"Eduwin WebView Bridge Demo", WIN_W, WIN_H);

    /* ── toolbar ── */
    btn_back   = create_button(g_win, L"\x25C0", PAD,           PAD, BTN_W, BTN_H);
    btn_fwd    = create_button(g_win, L"\x25B6", BTN_W+PAD*2,   PAD, BTN_W, BTN_H);
    btn_reload = create_button(g_win, L"\x21BB", (BTN_W+PAD)*2+PAD, PAD, BTN_W, BTN_H);

    g_url = create_text(g_win, L"", (BTN_W+PAD)*3+PAD, PAD, WIN_W-(BTN_W+PAD)*4, BTN_H);
    g_go  = create_button(g_win, L"Go", WIN_W-BTN_W-PAD, PAD, BTN_W, BTN_H);

    on_button_click(btn_back, on_back, NULL);
    on_button_click(btn_fwd, on_forward, NULL);
    on_button_click(btn_reload, on_reload, NULL);
    on_button_click(g_go, on_go, NULL);

    /* ── webview ── */
    g_wv = create_webview(g_win, 0, TOOL_H, WIN_W, WIN_H - TOOL_H - STAT_H);
    on_webview_ready(g_wv, on_ready, NULL);
    on_navigation_completed(g_wv, on_nav_complete, NULL);

    /* ── 注册 bridge 处理函数 ── */
    webview_bridge_register("hello",      on_bridge_hello,    NULL);
    webview_bridge_register("get_time",   on_bridge_get_time, NULL);
    webview_bridge_register("add_numbers", on_bridge_add,     NULL);

    /* ── status bar ── */
    g_status = create_label(g_win, L"Ready", 0, WIN_H - STAT_H, WIN_W, STAT_H);

    on_window_resize(g_win, on_resize);

    show_window(g_win);
    run_app();

    webview_cleanup();
    return 0;
}
