#include "eduwin.h"

/* ── 全局控件 ── */
static HWND statusbar;
static HWND g_ctx_btn;
static HMENU g_ctx_menu;

/* ── 菜单项 ID（全局，方便回调里直接引用） ── */
static int id_new, id_open, id_save, id_exit;
static int id_undo, id_redo, id_cut, id_copy, id_paste;
static int id_status, id_toolbar, id_sort_name, id_sort_size, id_sort_date;
static int id_about, id_ctx_info, id_ctx_delete, id_sys;

/* ── 主菜单回调 ── */
static void on_new(void *data)
{
    statusbar_set_text(statusbar, L"主菜单：新建");
}

static void on_open(void *data)
{
    WCHAR buf[MAX_PATH] = {0};
    if (open_file_dialog(NULL, buf, MAX_PATH, L"所有文件\0*.*\0"))
        statusbar_set_text(statusbar, buf);
}

static void on_save(void *data)
{
    statusbar_set_text(statusbar, L"主菜单：保存");
}

static void on_exit(void *data)
{
    quit_app();
}

static void on_undo(void *data)
{
    statusbar_set_text(statusbar, L"主菜单：撤销");
}

static void on_redo(void *data)
{
    statusbar_set_text(statusbar, L"主菜单：重做");
}

static void on_edit_cut(void *data)
{
    statusbar_set_text(statusbar, L"主菜单：剪切 (Ctrl+X)");
}

static void on_edit_copy(void *data)
{
    statusbar_set_text(statusbar, L"主菜单：复制 (Ctrl+C)");
}

static void on_edit_paste(void *data)
{
    static int can_paste = 1;
    set_menu_enabled(id_paste, !can_paste);
    statusbar_set_text(statusbar, can_paste ? L"主菜单：粘贴 — 已禁用" : L"主菜单：粘贴 — 已启用");
    can_paste = !can_paste;
}

static void on_view_status(void *data)
{
    static int checked = 1;
    checked = !checked;
    set_menu_checked(id_status, checked);
    statusbar_set_text(statusbar, checked ? L"状态栏：显示" : L"状态栏：隐藏");
}

static void on_view_toolbar(void *data)
{
    static int checked = 1;
    checked = !checked;
    set_menu_checked(id_toolbar, checked);
    statusbar_set_text(statusbar, checked ? L"工具栏：显示" : L"工具栏：隐藏");
}

static void on_sort_name(void *data)
{
    statusbar_set_text(statusbar, L"子菜单：按名称排序");
}

static void on_sort_size(void *data)
{
    statusbar_set_text(statusbar, L"子菜单：按大小排序");
}

static void on_sort_date(void *data)
{
    statusbar_set_text(statusbar, L"子菜单：按日期排序");
}

static void on_about(void *data)
{
    alert(L"关于", L"eduwin 菜单功能演示\n\n演示内容：主菜单、子菜单、勾选/禁用、\n系统菜单、上下文菜单、快捷键");
}

/* ── 系统菜单回调 ── */
static void on_sys_about(void *data)
{
    alert(L"系统菜单", L"通过系统菜单（标题栏右键）打开");
}

/* ── 上下文菜单回调 ── */
static void on_ctx_info(void *data)
{
    statusbar_set_text(statusbar, L"右键菜单：查看详情");
}

static void on_ctx_delete(void *data)
{
    statusbar_set_text(statusbar, L"右键菜单：删除");
}

/* ── 按钮右键弹出上下文菜单 ── */
static WNDPROC g_old_btn_proc;

static LRESULT CALLBACK ctx_btn_wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    if (msg == WM_CONTEXTMENU) {
        POINT pt = {LOWORD(lp), HIWORD(lp)};
        show_context_menu(g_ctx_menu, (HWND)wp, pt.x, pt.y);
        return 0;
    }
    return CallWindowProc(g_old_btn_proc, hwnd, msg, wp, lp);
}

static void on_ctx_btn_click(void *data)
{
    statusbar_set_text(statusbar, L"左键点击按钮  |  右键可弹出上下文菜单");
}

/* ── 窗口关闭 ── */
static int on_close(void *data)
{
    return 1;
}

/* ── 窗口缩放时在状态栏显示客户区尺寸 ── */
static void on_size(int w, int h)
{
    WCHAR buf[128];
    str_format(buf, 128, L"Client: %d x %d", w, h);
    statusbar_set_text(statusbar, buf);
}

int main(void)
{
    if (init_app() != 0)
    {
        alert(L"错误", L"eduwin 初始化失败");
        return 1;
    }

    log_clear();
    log_info(L"=== menu_demo start ===");

    HWND win = create_window(L"eduwin 菜单功能演示", 700, 500);
    if (!win)
    {
        alert(L"错误", L"创建窗口失败");
        return 1;
    }
    on_window_close(win, on_close, NULL);
    on_window_resize(win, on_size);

    /* 状态栏 */
    statusbar = create_statusbar(win);
    statusbar_set_text(statusbar, L"就绪  |  右键按钮弹出上下文菜单");

    /* 按钮 */
    HWND btn = create_button(win, L"右键点击我查看上下文菜单", 20, 20, 350, 40);
    on_button_click(btn, on_ctx_btn_click, NULL);
    g_ctx_btn = btn;

    /* 子类化按钮以处理右键 */
    g_old_btn_proc = (WNDPROC)SetWindowLongPtr(btn, GWLP_WNDPROC, (LONG_PTR)ctx_btn_wndproc);

    /* ── 主菜单 ── */
    HMENU main_menu = create_main_menu(win);

    /* 文件菜单 */
    HMENU file_menu = add_main_menu(main_menu, L"文件(&F)");
    id_new  = add_menu_item(file_menu, L"新建\tCtrl+N");
    id_open = add_menu_item(file_menu, L"打开\tCtrl+O");
    id_save = add_menu_item(file_menu, L"保存\tCtrl+S");
    add_menu_separator(file_menu);
    id_exit = add_menu_item(file_menu, L"退出\tCtrl+Q");

    on_menu_click(id_new,  on_new,  NULL);
    on_menu_click(id_open, on_open, NULL);
    on_menu_click(id_save, on_save, NULL);
    on_menu_click(id_exit, on_exit, NULL);

    /* 编辑菜单 */
    HMENU edit_menu = add_main_menu(main_menu, L"编辑(&E)");
    id_undo  = add_menu_item(edit_menu, L"撤销\tCtrl+Z");
    id_redo  = add_menu_item(edit_menu, L"重做\tCtrl+Y");
    add_menu_separator(edit_menu);
    id_cut   = add_menu_item(edit_menu, L"剪切\tCtrl+X");
    id_copy  = add_menu_item(edit_menu, L"复制\tCtrl+C");
    id_paste = add_menu_item(edit_menu, L"粘贴\tCtrl+V");

    on_menu_click(id_undo,  on_undo,  NULL);
    on_menu_click(id_redo,  on_redo,  NULL);
    on_menu_click(id_cut,   on_edit_cut,   NULL);
    on_menu_click(id_copy,  on_edit_copy,  NULL);
    on_menu_click(id_paste, on_edit_paste, NULL);

    /* 视图菜单（含勾选项 + 子菜单） */
    HMENU view_menu = add_main_menu(main_menu, L"视图(&V)");
    id_status  = add_menu_item(view_menu, L"状态栏");
    id_toolbar = add_menu_item(view_menu, L"工具栏");
    set_menu_checked(id_status, 1);
    set_menu_checked(id_toolbar, 1);
    add_menu_separator(view_menu);

    HMENU sort_sub = add_sub_menu(view_menu, L"排序方式");
    id_sort_name = add_menu_item(sort_sub, L"按名称");
    id_sort_size = add_menu_item(sort_sub, L"按大小");
    id_sort_date = add_menu_item(sort_sub, L"按日期");

    on_menu_click(id_status,    on_view_status,  NULL);
    on_menu_click(id_toolbar,   on_view_toolbar, NULL);
    on_menu_click(id_sort_name, on_sort_name,    NULL);
    on_menu_click(id_sort_size, on_sort_size,    NULL);
    on_menu_click(id_sort_date, on_sort_date,    NULL);

    /* 帮助菜单 */
    HMENU help_menu = add_main_menu(main_menu, L"帮助(&H)");
    id_about = add_menu_item(help_menu, L"关于");
    on_menu_click(id_about, on_about, NULL);

    /* 快捷键 */
    set_menu_shortcut(id_new,  'N', 1, 0);
    set_menu_shortcut(id_open, 'O', 1, 0);
    set_menu_shortcut(id_save, 'S', 1, 0);
    set_menu_shortcut(id_undo, 'Z', 1, 0);
    set_menu_shortcut(id_redo, 'Y', 1, 0);
    set_menu_shortcut(id_cut,  'X', 1, 0);
    set_menu_shortcut(id_copy, 'C', 1, 0);
    set_menu_shortcut(id_paste,'V', 1, 0);
    set_menu_shortcut(id_exit, 'Q', 1, 0);

    /* 上下文菜单 */
    HMENU ctx = create_context_menu();
    id_ctx_info   = add_menu_item(ctx, L"查看详情");
    add_menu_separator(ctx);
    id_ctx_delete = add_menu_item(ctx, L"删除");
    on_menu_click(id_ctx_info,   on_ctx_info,   NULL);
    on_menu_click(id_ctx_delete, on_ctx_delete, NULL);
    g_ctx_menu = ctx;

    /* 系统菜单 */
    add_sys_separator(win);
    id_sys = create_sys_menu(win, L"关于 (&A)");
    on_menu_click(id_sys, on_sys_about, NULL);

    /* 显示窗口（show_window 内部已自动调用 DrawMenuBar 强制重绘菜单栏） */
    show_window(win);

    run_app();
    return 0;
}
