#include <gear/ui/console.h>
#include <gear/ui/gui.h>

void g_console_draw(g_gui_context* ctx, g_gui_layer* layer) {
    g_console* console = layer->data;

    if (nk_begin(ctx, "Console", nk_rect(50, 50, 500, 500),
                 NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_CLOSABLE | NK_WINDOW_SCALABLE)) {
        float frame_height = nk_window_get_content_region(ctx).h;

        nk_layout_row_dynamic(ctx, frame_height - 42.5f, 1);
        snk_edit_string(ctx, NK_EDIT_CLIPBOARD | NK_EDIT_SELECTABLE | NK_EDIT_MULTILINE | NK_EDIT_GOTO_END_ON_ACTIVATE,
                        console->output.buf, &console->output.len, 
                        G_CONSOLE_BUFFER_SIZE, nk_filter_default);

        nk_layout_row_dynamic(ctx, 30.0f, 1);
        enum nk_edit_events event = snk_edit_string(ctx, NK_EDIT_FIELD | NK_EDIT_SIG_ENTER, 
                        console->input.buf, &console->input.len,
                        G_CONSOLE_MAX_LEN, nk_filter_default);
        if(event & NK_EDIT_COMMITED) {
            log_info("cmd: %s", console->input);
            memset(console->input.buf, 0, G_CONSOLE_MAX_LEN);
            console->input.len = 0;
        }
    }

    nk_end(ctx);
}
