/*
 * Book's Story — free and open-source Material You eBook reader.
 * Copyright (C) 2024-2026 Acclorite
 * SPDX-License-Identifier: GPL-3.0-only
 */

package org.dueattendant149.bookshelf.ui.library

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import org.dueattendant149.bookshelf.presentation.library.model.SelectableBook
import org.dueattendant149.bookshelf.ui.common.components.common.LazyVerticalGridWithScrollbar
import org.dueattendant149.bookshelf.ui.common.data.ScrollbarData

@Composable
fun LibraryGridLayout(
    books: List<SelectableBook>,
    gridSize: Int,
    autoGridSize: Boolean,
    itemContent: @Composable (book: SelectableBook) -> Unit
) {
    LazyVerticalGridWithScrollbar(
        columns = if (autoGridSize) GridCells.Adaptive(120.dp)
        else GridCells.Fixed(gridSize.coerceAtLeast(1)),
        modifier = Modifier.fillMaxSize(),
        scrollbarSettings = ScrollbarData.primaryScrollbar,
        contentPadding = PaddingValues(8.dp)
    ) {
        items(
            books,
            key = { it.data.id }
        ) { book ->
            Box(modifier = Modifier.animateItem()) {
                itemContent(book)
            }
        }
    }
}