#include "eduwin.h"

static HWND g_tree;      /* 树控件 */
static HWND g_input;     /* 节点名称输入框 */
static HWND g_add_btn;   /* 添加子节点按钮 */
static HWND g_edit_btn;  /* 编辑节点按钮 */
static HWND g_del_btn;   /* 删除节点按钮 */
static HWND g_label;     /* 提示标签 */

/* 添加默认的几个节点 */
static void init_tree(HWND tree)
{
    /* 根级节点 */
    HWND n1 = treeview_add_text(tree, L"计算机学院");
    HWND n2 = treeview_add_text(tree, L"数学学院");
    HWND n3 = treeview_add_text(tree, L"物理学院");

    treeview_add_child(tree, n1, L"计算机科学与技术");
    HWND cs_soft = treeview_add_child(tree, n1, L"软件工程");
    treeview_add_child(tree, n1, L"网络工程");

    treeview_add_child(tree, cs_soft, L"软件工程 2021 级");
    treeview_add_child(tree, cs_soft, L"软件工程 2022 级");
    treeview_add_child(tree, cs_soft, L"软件工程 2023 级");

    treeview_add_child(tree, n2, L"应用数学");
    treeview_add_child(tree, n2, L"信息与计算科学");

    treeview_add_child(tree, n3, L"物理学");
    treeview_add_child(tree, n3, L"光电信息科学");

    /* 展开一级节点 */
    treeview_expand(tree, n1);
    treeview_expand(tree, n2);
    treeview_expand(tree, n3);
    treeview_expand(tree, cs_soft);
}

/* 更新提示 */
static void update_status(const WCHAR *msg)
{
    set_text(g_label, msg);
}

/* 添加子节点 */
static void on_add_child(void *data)
{
    HWND sel = treeview_selected(g_tree);
    if (!sel)
    {
        /* 无选中时在根级添加 */
        const WCHAR *text = get_text(g_input);
        if (text && text[0])
        {
            treeview_add_text(g_tree, text);
            update_status(L"已在根级添加节点");
        }
        return;
    }

    const WCHAR *text = get_text(g_input);
    if (!text || !text[0])
    {
        update_status(L"请先输入节点名称");
        return;
    }

    HWND child = treeview_add_child(g_tree, sel, text);
    treeview_expand(g_tree, sel);
    set_text(g_input, L"");
    update_status(L"已添加子节点");
}

/* 编辑选中节点 */
static void on_edit_node(void *data)
{
    HWND sel = treeview_selected(g_tree);
    if (!sel)
    {
        update_status(L"请先选中一个节点");
        return;
    }

    const WCHAR *text = get_text(g_input);
    if (!text || !text[0])
    {
        update_status(L"请先输入新的节点名称");
        return;
    }

    treeview_set_text(g_tree, sel, text);
    set_text(g_input, L"");
    update_status(L"已更新节点名称");
}

/* 删除选中节点 */
static void on_delete_node(void *data)
{
    HWND sel = treeview_selected(g_tree);
    if (!sel)
    {
        update_status(L"请先选中一个节点");
        return;
    }

    if (confirm(L"确认删除", L"确定要删除选中节点及其所有子节点吗？"))
    {
        treeview_delete(g_tree, sel);
        set_text(g_input, L"");
        update_status(L"已删除节点");
    }
}

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

    HWND win = create_window(L"树形结构演示", 700, 450);
    if (!win)
    {
        alert(L"错误", L"创建窗口失败");
        return 1;
    }

    /* ========= 左侧：树控件 ========= */
    create_label(win, L"组织结构", 20, 15, 120, 20);
    g_tree = create_treeview(win, 20, 40, 320, 330);

    /* ========= 右侧：操作面板 ========= */
    create_label(win, L"操作面板", 380, 15, 120, 20);

    create_label(win, L"节点名称：", 380, 50, 80, 24);
    g_input = create_text(win, L"", 460, 48, 180, 24);

    g_add_btn = create_button(win, L"添加子节点", 380, 100, 130, 28);
    on_button_click(g_add_btn, on_add_child, NULL);

    g_edit_btn = create_button(win, L"编辑节点", 530, 100, 110, 28);
    on_button_click(g_edit_btn, on_edit_node, NULL);

    g_del_btn = create_button(win, L"删除节点", 380, 145, 130, 28);
    on_button_click(g_del_btn, on_delete_node, NULL);

    /* 提示信息 */
    g_label = create_label(win, L"提示：选中节点后，可添加子节点、编辑或删除", 380, 200, 280, 60);

    /* 初始化树 */
    init_tree(g_tree);

    show_window(win);
    run_app();
    return 0;
}
