//
// Login screen. Three text inputs (server URL, username, password) and a
// big chunky CONNECT button.
//

draw_login_view :: () {
    w := cast(float) app.window_width;
    h := cast(float) app.window_height;
    k := h * 0.05;

    // Title.
    {
        label_theme := app.theme.label_theme;
        label_theme.font = app.title_font;
        label_theme.alignment = .Center;
        label_theme.text_color = .{1, 0.4, 0.8, 1};
        r := get_rect(0, h * 0.06, w, app.title_font.character_height * 1.2);
        label(r, "PLAYER", *label_theme);
    }
    {
        label_theme := app.theme.label_theme;
        label_theme.font = app.body_font;
        label_theme.alignment = .Center;
        label_theme.text_color = .{0.7, 0.7, 0.9, 1};
        r := get_rect(0, h * 0.06 + app.title_font.character_height * 1.2, w, app.body_font.character_height * 2.0);
        label(r, "a jellyfin music player", *label_theme);
    }

    // Form column.
    field_w := min(w * 0.5, 12.0 * k);
    field_x := (w - field_w) * 0.5;
    field_h := app.button_font.character_height * 1.7;

    text_theme := app.theme.text_input_theme;
    text_theme.font = app.button_font;

    label_theme := app.theme.label_theme;
    label_theme.font = app.body_font;
    label_theme.alignment = .Left;

    line_h := app.body_font.character_height * 1.2;
    gap    := field_h * 1.3;
    cursor_y := h * 0.40;

    text_field(field_x, *cursor_y, field_w, line_h, field_h, "server URL", *app.jellyfin.server_url, *label_theme, *text_theme);
    cursor_y += gap - line_h - field_h;
    text_field(field_x, *cursor_y, field_w, line_h, field_h, "username",   *app.jellyfin.username,   *label_theme, *text_theme);
    cursor_y += gap - line_h - field_h;
    text_field(field_x, *cursor_y, field_w, line_h, field_h, "password",   *app.jellyfin.password,   *label_theme, *text_theme);
    cursor_y += gap - line_h - field_h;

    // CONNECT button.
    cursor_y += k * 0.5;
    button_theme := app.theme.button_theme;
    button_theme.font = app.button_font;
    button_theme.label_theme.alignment = .Center;
    label_text := ifx app.jellyfin.login_pending then "CONNECTING..." else "CONNECT";
    if button(get_rect(field_x, cursor_y, field_w, field_h * 1.2), label_text, *button_theme) {
        if !app.jellyfin.login_pending {
            jellyfin_login_async(*app.jellyfin);
        }
    }
}

//
// Stack a label + text_input. Each call gets its own widget identity by
// passing the caller's source location through to the widgets — same trick
// GetRect uses internally. The identifier= disambiguates the two widgets
// drawn at the same loc.
//
text_field :: (
    x: float, y: *float, w: float, label_h: float, input_h: float,
    label_text: string, value: *string,
    label_theme: *Label_Theme, text_theme: *Text_Input_Theme,
    loc := #caller_location,
) {
    label(get_rect(x, y.*, w, label_h), label_text, label_theme);
    y.* += label_h;
    action, new_text := text_input(get_rect(x, y.*, w, input_h), value.*, text_theme, loc=loc);
    if action & .ENTERED  modify_string(value, new_text);
    y.* += input_h;
}

modify_string :: (ps: *string, new_value: string) {
    free(ps.*);
    ps.* = copy_string(new_value);
}
