#include "ui.h"

int main(int argc, char **argv) {
    int bbs_mode = 0;
    const char *drop_explicit = NULL;
    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "--bbs") == 0) bbs_mode = 1;
        else if (starts_with(argv[i], "--dropfile=")) drop_explicit = argv[i] + 11;
    }

    if (init_platform() != 0) {
        fprintf(stderr, "Platform init failed\n");
        return 1;
    }

    config_t cfg;
    config_init(&cfg);
    config_load(&cfg);

    bookmark_t initial = cfg.count ? cfg.items[0] : (bookmark_t){"New","irc.libera.chat",6667,"StingRay","##chat",0};
    dropfile_info_t drop;
    memset(&drop, 0, sizeof(drop));
    char found[260];
    if (bbs_mode || dropfile_detect(drop_explicit, found, sizeof(found))) {
        bbs_mode = 1;
        if (dropfile_load(found, &drop, &initial) == 0) {
            // keep loaded bookmark
        }
    }

    irc_client_t irc;
    irc_init(&irc);
    ui_state_t ui;
    ui_init(&ui, &initial, bbs_mode);
    ui.drop = drop;

    ui_run(&ui, &cfg, &irc);

    irc_disconnect(&irc, "Bye");
    ui_shutdown(&ui);
    config_save(&cfg);
    shutdown_platform();
    return 0;
}
