#include <menu.h>
#include <menu_private.h>
#include <gui.h>
#include <gui/button.h>
#include <gui/container.h>
#include <gui/rounded_rect.h>
#include <util.h>
#include <system.h>
#include <stdbool.h>

enum role
{
    CREDITS_NAME_COPYRIGHT,
    CREDITS_ROLE_GFX,
    CREDITS_ROLE_PROGRAMMING,
    CREDITS_ROLE_ENGINE,
    CREDITS_ROLE_LANGUAGE,

    MAX_CREDIT_ROLES
};

struct main_menu
{
    bool start, settings, exit;
    struct gui_button play, settings_btn, exit_btn;
    struct gui_container cnt;
    struct gui_label title, roles[MAX_CREDIT_ROLES];
};

static int update(struct menu_common *const c, void *const arg)
{
    struct main_menu *const m = arg;

    if (gui_update(&m->play.common, &c->p, &c->cam, &c->in))
        return -1;

    return 0;
}

static int render(const struct menu_common *const c, void *const arg)
{
    const struct main_menu *const m = arg;

    if (gui_render(&m->cnt.common))
        return -1;

    return 0;
}

static void update_roles(struct main_menu *const m)
{
    static const char *const roles[] =
    {
        [CREDITS_NAME_COPYRIGHT] = "(C) 2024 Xavier Del Campo Romero",
        [CREDITS_ROLE_GFX] = "Gfx: Kenney, Ivan Voirol",
        [CREDITS_ROLE_PROGRAMMING] = "Coding: me",
        [CREDITS_ROLE_ENGINE] = "Engine: also me lol",
        [CREDITS_ROLE_LANGUAGE] = "Written in good old C"
    };

    UTIL_STATIC_ASSERT(sizeof roles / sizeof *roles
        == sizeof m->roles / sizeof *m->roles,
        "unexpected sizes");

    for (size_t i = 0; i < sizeof roles / sizeof *roles; i++)
    {
        struct gui_label *const l = &m->roles[i];

        gui_label_init(l);
        l->common.hcentered = true;
        l->text = roles[i];
        gui_add_child(&m->cnt.common, &l->common);
    }
}

int menu_main(struct menu_common *const c)
{
    do
    {
        struct main_menu m = {0};

        {
            struct gui_container *const c = &m.cnt;

            gui_container_init(c);
            c->mode = GUI_CONTAINER_MODE_V;
            c->common.hcentered = true;
            c->common.vcentered = true;
            c->spacing = 4;
        }

        {
            struct gui_label *const l = &m.title;

            gui_label_init(l);
            l->common.hcentered = true;
            l->text = "Jancity, a city sandbox";
            gui_add_child(&m.cnt.common, &l->common);
        }

        {
            struct gui_button *const b = &m.play;

            gui_button_init(b, GUI_BUTTON_TYPE_1);
            b->on_pressed = menu_on_pressed;
            b->arg = &m.start;
            b->u.type1.w = 140;
            b->common.hcentered = true;
            b->u.type1.label.text = "Play";
            gui_add_child(&m.cnt.common, &b->common);
        }

        {
            struct gui_button *const b = &m.settings_btn;

            gui_button_init(b, GUI_BUTTON_TYPE_1);
            b->on_pressed = menu_on_pressed;
            b->arg = &m.settings;
            b->u.type1.w = 140;
            b->common.hcentered = true;
            b->u.type1.label.text = "Settings";
            gui_add_child(&m.cnt.common, &b->common);
        }

        if (system_can_exit())
        {
            struct gui_button *const b = &m.exit_btn;

            gui_button_init(b, GUI_BUTTON_TYPE_1);
            b->arg = &m.exit;
            b->u.type1.w = 140;
            b->common.hcentered = true;
            b->u.type1.label.text = "Exit";
            b->on_pressed = menu_on_pressed;
            gui_add_child(&m.cnt.common, &b->common);
        }

        update_roles(&m);

        while (!m.start && !m.settings && !c->p.common.exit)
        {
            if (menu_update(c, update, render, &m))
                return -1;

            if (m.exit)
                return 0;
        }

        if (m.start && menu_hostjoin(c))
            return -1;
        else if (m.settings && menu_settings(c))
            return -1;

    } while (!c->p.common.exit);

    return 0;
}
