#include <os/Timer.h>
#include <time.h>
#include <ui/App.h>
#include <ui/Label.h>

ui::Label* g_label;

void update_time()
{
    time_t t = time(NULL);
    struct tm* tp = localtime(&t);

    static char buf[2048];
    strftime(buf, sizeof(buf), "%H:%M:%S", tp);

    g_label->set_text(StringView { buf });

    ui::App::the().main_window()->draw();
}

Result<int> luna_main(int, char**)
{
    ui::App app;
    TRY(app.init());

    auto* window = TRY(ui::Window::create(ui::Rect { 500, 400, 100, 50 }));
    app.set_main_window(window);

    window->set_title("Clock");
    window->set_background(ui::GRAY);

    g_label = TRY(make<ui::Label>("00:00:00"));
    g_label->set_font(ui::Font::default_bold_font());
    g_label->set_color(ui::BLACK);

    window->set_main_widget(*g_label);

    update_time();

    auto timer = TRY(os::Timer::create_repeating(1000, update_time));

    return app.run();
}