package com.ponzischeme89.memby.data

import com.ponzischeme89.memby.data.model.BaseItem

/**
 * Browsing a genre: what came back, where it started, and how much there is.
 *
 * Kept apart from the search results it shares a pane with, because the two answer
 * different questions. A search is one response and it is either everything or nothing;
 * a genre is a shelf somebody scrolls, so what matters about a page is where it sits in
 * the whole and whether there is another one behind it.
 */
data class GenrePage(
    val items: List<BaseItem>,
    val offset: Int,
    /**
     * How many titles the genre holds. Zero from a backend that would not count, which
     * [hasMoreGenreItems] reads as "this is all there is" rather than as an invitation to
     * keep asking — a grid that asks forever is worse than one that stops early, since the
     * viewer can always search.
     */
    val total: Int,
)

/** A page this size, in items. Several television screenfuls, so the scroll stays ahead. */
const val GENRE_PAGE_SIZE = 48

/**
 * Whether the grid should ask for another page.
 *
 * Two things end a scroll and both have to, because either one alone leaves a real case
 * broken. Reaching the total is the ordinary end. A page that came back *short* of what was
 * asked for is the other: a backend that did not count says nothing useful with its total,
 * and without this the grid would go on asking for pages of a genre that ran out.
 */
fun hasMoreGenreItems(loaded: Int, total: Int, lastPageSize: Int, pageSize: Int): Boolean {
    if (loaded == 0) return false
    if (lastPageSize < pageSize) return false
    return loaded < total
}
