//
// Now playing — adapts to three layout modes based on the window aspect ratio:
//   TALL   (aspect < 0.90) — portrait: art on top, info stacked below
//   NORMAL (0.90 – 1.90)  — landscape: art left, info right
//   WIDE   (aspect > 1.90) — ultra-wide: more padding, larger art
//

draw_now_playing_view :: () {
    w := cast(float) app.window_width;
    h := cast(float) app.window_height;

    has_track := app.current_stream != null || app.current_track.id.count > 0;

    draw_now_playing_backdrop(w, h, has_track);

    aspect := w / h;
    if aspect < 0.90 {
        draw_now_playing_tall(w, h, has_track);
    } else if aspect > 1.90 {
        draw_now_playing_wide(w, h, has_track);
    } else {
        draw_now_playing_normal(w, h, has_track);
    }
}

#scope_file

// ── Shared backdrop ───────────────────────────────────────────────────────────

draw_now_playing_backdrop :: (w: float, h: float, has_track: bool) {
    if has_track && app.current_track.artist_id.count > 0 {
        backdrop := image_request(app.current_track.artist_id, .BACKDROP);
        if backdrop && backdrop.failed {
            backdrop = image_request(app.current_track.artist_id, .LARGE);
        }
        if backdrop && backdrop.loaded {
            draw_image_cover(0, 0, w, h, backdrop, tint=Vector4.{1, 1, 1, 0.5});
        }
    }
    // Visualizer on top of backdrop. Background quad at 50% so the artist
    // image shows through, bars drawn fully opaque on top.
    gfx_draw_visualizer_background(w, h, bg_alpha=0.5);
}

// ── Shared transport ─────────────────────────────────────────────────────────

draw_now_playing_transport :: (cx: float, by: float, bw: float, bh: float, sp: float, has_track: bool, font: *Simp.Dynamic_Font = null) {
    bt := app.theme.button_theme;
    bt.font = ifx font then font else app.button_font;
    bt.label_theme.alignment = .Center;

    total_btn_w := bw * 5 + sp * 4;
    bx := cx - total_btn_w * 0.5;

    shuf_label := ifx queue.shuffle_mode == .OFF then "seq" else ifx queue.shuffle_mode == .ALL then "shuf" else "shuf*";
    if button(get_rect(bx, by, bw, bh), shuf_label, *bt, identifier=4) {
        if queue.shuffle_mode == .OFF       queue_set_shuffle(.ALL);
        else if queue.shuffle_mode == .ALL  queue_set_shuffle(.FAVOURITES);
        else                                queue_set_shuffle(.OFF);
    }
    bx += bw + sp;
    if button(get_rect(bx, by, bw, bh), "<<", *bt, identifier=1)   queue_prev();
    bx += bw + sp;
    pp_label := ifx audio_is_paused() then "PLAY" else "PAUSE";
    if button(get_rect(bx, by, bw, bh), pp_label, *bt, identifier=2)  audio_toggle_pause();
    bx += bw + sp;
    if button(get_rect(bx, by, bw, bh), ">>", *bt, identifier=3)   queue_next();
    bx += bw + sp;

    fav_bt := bt;
    if has_track && app.current_track.is_favourite {
        fav_bt.surface_color = .{0.6, 0.05, 0.25, 1};
        fav_bt.text_color    = .{1, 0.7, 0.8, 1};
    }
    fav_label := ifx (has_track && app.current_track.is_favourite) then "* fav" else "fav";
    if button(get_rect(bx, by, bw, bh), fav_label, *fav_bt, identifier=6) && has_track {
        jellyfin_toggle_favourite(app.current_track.id, app.current_track.is_favourite);
        app.current_track.is_favourite = !app.current_track.is_favourite;
    }
}

draw_now_playing_seek :: (slider_x: float, slider_y: float, slider_w: float, slider_h: float, has_track: bool) {
    if !has_track || !app.current_stream || !app.current_stream.sound_data  return;

    rate  := cast(float) app.current_stream.sound_data.sampling_rate;
    total := cast(float) app.current_track.duration_ticks / 10_000_000.0;
    if total <= 0  total = max(cast(float) app.current_stream.play_cursor / rate + 1, 1);

    app.scrub_seconds = clamp(cast(float) app.current_stream.play_cursor / rate, 0, total);

    st := make_palette_slider_theme(app.button_font);
    changed, _ := slider(
        get_rect(slider_x, slider_y, slider_w, slider_h),
        *app.scrub_seconds, 0.0, total, 30.0, *st, "", "s",
    );
    if changed  audio_seek_seconds(app.scrub_seconds);

    col_album := now_playing_album_color();
    time_lt := app.theme.label_theme;
    time_lt.font      = app.body_font;
    time_lt.text_color = col_album;
    time_lt.alignment  = .Left;
    label(get_rect(slider_x, slider_y + slider_h + 2, slider_w * 0.5, app.body_font.character_height * 1.3),
          format_seconds(app.scrub_seconds), *time_lt);
    time_lt.alignment = .Right;
    label(get_rect(slider_x + slider_w * 0.5, slider_y + slider_h + 2, slider_w * 0.5, app.body_font.character_height * 1.3),
          format_seconds(total), *time_lt);
}

draw_now_playing_volume :: (vx: float, vy: float, vw: float, vh: float) {
    vst := make_palette_slider_theme(app.body_font);
    before := app.master_volume;
    slider(get_rect(vx, vy, vw, vh), *app.master_volume, 0.0, 1.0, 0.05, *vst, "vol ", "", identifier=99);
    if before != app.master_volume  config_save();
}

// ── NORMAL layout (landscape) ────────────────────────────────────────────────

draw_now_playing_normal :: (w: float, h: float, has_track: bool) {
    k := h * 0.05;

    {   // Back to library
        bt := app.theme.button_theme;
        bt.font = app.button_font;
        bt.label_theme.alignment = .Center;
        if button(get_rect(k * 0.6, k * 0.6, w * 0.16, k), "library", *bt)
            app.current_view = .LIBRARY;
    }

    art_size := min(w * 0.32, h * 0.52);
    art_x    := w * 0.06;
    art_y    := h * 0.12;
    info_x   := art_x + art_size + w * 0.05;
    info_w   := w - info_x - w * 0.04;

    if has_track  draw_image(art_x, art_y, art_size, art_size, image_request(app.current_track.album_id, .LARGE));
    else {
        Simp.set_shader_for_color();
        Simp.immediate_quad(art_x, art_y, art_x + art_size, art_y + art_size, .{0.10, 0.05, 0.18, 1});
    }

    draw_now_playing_info(info_x, art_y, info_w, has_track, align_center=false);

    draw_now_playing_seek(w * 0.14, h * 0.67, w * 0.72, k * 0.55, has_track);

    draw_now_playing_transport(w * 0.5, h * 0.79, k * 2.4, k * 1.5, k * 0.35, has_track);

    draw_now_playing_volume(w - w * 0.17 - k * 0.5, h - k * 0.5 - k * 0.5, w * 0.17, k * 0.5);
}

// ── TALL layout (portrait) ───────────────────────────────────────────────────

draw_now_playing_tall :: (w: float, h: float, has_track: bool) {
    // In tall/narrow windows, fonts are sized by HEIGHT which makes them huge.
    // Use row_font for title and body_font for sub-text so everything fits.
    pad := w * 0.05;

    {
        bt := app.theme.button_theme;
        bt.font = app.body_font;
        bt.label_theme.alignment = .Center;
        if button(get_rect(pad * 0.8, pad * 0.4, w * 0.30, app.body_font.character_height * 1.6), "library", *bt)
            app.current_view = .LIBRARY;
    }

    // Art — centered, capped so it doesn't dominate
    art_size := min(w * 0.82, h * 0.38);
    art_x    := (w - art_size) * 0.5;
    art_y    := h * 0.07;

    if has_track  draw_image(art_x, art_y, art_size, art_size, image_request(app.current_track.album_id, .LARGE));
    else {
        Simp.set_shader_for_color();
        Simp.immediate_quad(art_x, art_y, art_x + art_size, art_y + art_size, .{0.10, 0.05, 0.18, 1});
    }

    // Running y cursor — everything stacks below art
    y := art_y + art_size + h * 0.025;

    // Info: row_font title, body_font sub — avoids the enormous h/8 title
    y = draw_now_playing_info(pad, y, w - pad * 2, has_track, align_center=true,
                              title_font=app.row_font, sub_font=app.body_font);
    y += h * 0.02;

    // Seek slider
    seek_h := app.body_font.character_height * 1.1;
    draw_now_playing_seek(pad, y, w - pad * 2, seek_h, has_track);
    y += seek_h + app.body_font.character_height * 1.4 + h * 0.025;

    // Transport: use body_font so "PAUSE" fits in narrow buttons
    bw := (w - pad * 2 - 4 * pad * 0.5) / 5.0;  // 5 buttons filling the width
    bh := bw * 0.55;
    sp := pad * 0.5;
    draw_now_playing_transport(w * 0.5, y, bw, bh, sp, has_track, font=app.body_font);
    y += bh + h * 0.025;

    // Volume
    vw := w * 0.65;
    draw_now_playing_volume((w - vw) * 0.5, y, vw, app.body_font.character_height * 1.1);
}

// ── WIDE layout (ultra-wide) ─────────────────────────────────────────────────

draw_now_playing_wide :: (w: float, h: float, has_track: bool) {
    k := h * 0.05;

    {   // Back to library
        bt := app.theme.button_theme;
        bt.font = app.button_font;
        bt.label_theme.alignment = .Center;
        if button(get_rect(k * 0.6, k * 0.6, w * 0.10, k), "library", *bt)
            app.current_view = .LIBRARY;
    }

    // Art — left side, generous size
    art_size := min(w * 0.28, h * 0.60);
    art_x    := w * 0.05;
    art_y    := (h - art_size) * 0.45;

    if has_track  draw_image(art_x, art_y, art_size, art_size, image_request(app.current_track.album_id, .LARGE));
    else {
        Simp.set_shader_for_color();
        Simp.immediate_quad(art_x, art_y, art_x + art_size, art_y + art_size, .{0.10, 0.05, 0.18, 1});
    }

    // Info — center column
    info_x := art_x + art_size + w * 0.04;
    info_w := w * 0.42;
    draw_now_playing_info(info_x, art_y, info_w, has_track, align_center=false);

    // Seek slider — under info
    seek_y := art_y + art_size * 0.65;
    draw_now_playing_seek(info_x, seek_y, info_w, k * 0.50, has_track);

    // Transport centered under seek
    bw := k * 2.2;
    draw_now_playing_transport(info_x + info_w * 0.5, seek_y + k * 0.9, bw, k * 1.4, k * 0.3, has_track);

    // Volume — bottom right
    draw_now_playing_volume(w - w * 0.14 - k * 0.5, h - k * 0.5 - k * 0.5, w * 0.14, k * 0.5);
}

// ── Info text block ──────────────────────────────────────────────────────────

// Returns the y coordinate just below the last line of text.
draw_now_playing_info :: (x: float, y: float, w: float, has_track: bool, align_center: bool,
                          title_font: *Simp.Dynamic_Font = null, sub_font: *Simp.Dynamic_Font = null) -> float {
    tf := ifx title_font then title_font else app.big_font;
    sf := ifx sub_font   then sub_font   else app.row_font;

    col_title  := now_playing_title_color();
    col_artist := now_playing_artist_color();
    col_album  := now_playing_album_color();
    align := ifx align_center then Text_Alignment.Center else Text_Alignment.Left;

    text := ifx has_track then app.current_track.name else "— silence —";
    {
        lt := app.theme.label_theme;
        lt.font = tf;  lt.alignment = align;  lt.text_color = col_title;
        label(get_rect(x, y, w, tf.character_height * 1.4), text, *lt);
    }
    if !has_track  return y + tf.character_height * 1.4;

    row_y := y + tf.character_height * 1.6;
    {
        lt := app.theme.label_theme;
        lt.font = sf;  lt.alignment = align;  lt.text_color = col_artist;
        label(get_rect(x, row_y, w, sf.character_height * 1.4), app.current_track.artist, *lt);
        row_y += sf.character_height * 1.7;
    }
    {
        lt := app.theme.label_theme;
        lt.font = app.body_font;  lt.alignment = align;  lt.text_color = col_album;
        label(get_rect(x, row_y, w, app.body_font.character_height * 1.4), app.current_track.album, *lt);
        row_y += app.body_font.character_height * 1.5;
    }
    return row_y;
}

// ── Palette colour helpers ────────────────────────────────────────────────────

now_playing_title_color :: () -> Vector4 {
    return ifx app.palette_ready then app.palette[1] else Vector4.{1.0, 0.4, 0.8, 1};
}

now_playing_artist_color :: () -> Vector4 {
    if !app.palette_ready  return .{0.8, 0.8, 1.0, 1};
    p := app.palette[2];
    return .{p.x * 0.65 + 0.35, p.y * 0.65 + 0.35, p.z * 0.65 + 0.35, 1};
}

now_playing_album_color :: () -> Vector4 {
    if !app.palette_ready  return .{0.6, 0.6, 0.8, 1};
    p := app.palette[3];
    return .{p.x * 0.55 + 0.35, p.y * 0.55 + 0.35, p.z * 0.55 + 0.35, 1};
}

// ── Slider theme ─────────────────────────────────────────────────────────────

make_palette_slider_theme :: (font: *Simp.Dynamic_Font) -> Slider_Theme {
    hi   := ifx app.palette_ready then app.palette[1] else Vector4.{0.0, 0.65, 0.45, 1};
    dark := ifx app.palette_ready
            then Vector4.{app.palette[0].x * 1.3, app.palette[0].y * 1.3, app.palette[0].z * 1.3, 1}
            else Vector4.{0.0, 0.10, 0.06, 1};
    hi2  := Vector4.{min(hi.x + 0.12, 1), min(hi.y + 0.12, 1), min(hi.z + 0.12, 1), 1};
    dk2  := Vector4.{dark.x * 1.6, dark.y * 1.6, dark.z * 1.6, 1};

    st: Slider_Theme;
    st.foreground.surface_color       = hi;
    st.foreground.surface_color_over  = hi2;
    st.foreground.surface_color_flash = hi2;
    st.foreground.surface_color_down  = hi;
    st.foreground.frame_color         = Vector4.{hi.x * 0.55, hi.y * 0.55, hi.z * 0.55, 1};
    st.foreground.frame_color_over    = hi;
    st.foreground.text_color          = .{1, 1, 1, 1};
    st.foreground.font                = font;
    st.background.surface_color       = dark;
    st.background.surface_color_over  = dark;
    st.background.frame_color         = dk2;
    st.background.frame_color_over    = dk2;
    return st;
}

format_seconds :: (s: float) -> string {
    if !(s >= 0)  return "0:00";
    total := cast(int) s;
    m := total / 60;
    sec := total - m * 60;
    return tprint("%:%", m, formatInt(sec, minimum_digits=2));
}
