#include <settings.h>
#include <gfx.h>
#include <peripheral.h>
#include <stdio.h>

int settings_apply(const struct settings *const s,
    const struct settings_rt *const r)
{
    if (gfx_fullscreen_available()
        && gfx_set_fullscreen(s->screen_w, s->screen_h))
        return -1;

    const struct peripheral_cfg cfg =
    {
        .padn = s->pad_i,
        .type = s->periph_type
    };

    peripheral_init(&cfg, r->p);
    return 0;
}

int settings_load_ex(const char *const path, struct settings *const s)
{
    int ret = -1;
    FILE *const f = fopen(path, "rb");

    if (!f)
        goto end;

    ret = 0;

end:
    if (ret)
    {
        *s = (const struct settings)
        {
            .name = "Player",
        };

        gfx_display_size(&s->screen_w, &s->screen_h);
    }

    if (f)
        fclose(f);

    return ret;
}
