import {useEffect, useState} from "react"
import RecipeListItem from "./RecipeListItem"
import type {RecipeModel} from "../../models/RecipeModel"
import {fetchRecipeList} from "../../api/points/CompactRecipePoint"
import {useNavigate} from "react-router-dom"
import {getRecipeAddUrl, getRecipeDetailUrl, getRecipeListUrl} from "../../routes"
import RecipeListToolbar from "./RecipeListToolbar"
import StickyHeader from "../basics/StickyHeader.tsx";
import ContentBackground from "../basics/ContentBackground.tsx";
import PageContainer from "../basics/PageContainer.tsx";

/**
 * Displays a list of recipes in a sidebar layout.
 * Each recipe link fills the available width.
 */
export default function RecipeListPage() {

    const navigate = useNavigate()
    const [recipeList, setRecipeList] = useState<RecipeModel[] | null>(null)
    const [searchString, setSearchString] = useState<string>("")
    // load recipes once on render and whenever search string changes
    // @todo add delay. Only reload list if the search string hasn't changed for ~200 ms
    useEffect(() => {
        console.log("loading recipe list with searchString", searchString)
        const loadRecipeList = async () => {
            try {
                // Fetch recipe list
                const data = await fetchRecipeList(searchString)
                // @todo add and use compact recipe mapper
                setRecipeList(data)
            } catch (err) {
                console.error(err)
            }
        }
        loadRecipeList()
    }, [searchString])

    const handleAdd = () => {
        navigate(getRecipeAddUrl())
    }

    if (!recipeList) {
        return <div>Loading!</div>
    }
    return (
        /*Container spanning entire screen used to center content horizontally */
        <PageContainer>
            {/* Container defining the maximum width of the content */}
            <ContentBackground>
                {/* Header - remains in position when scrolling */}
                <StickyHeader>
                    <h1>Recipes</h1>
                    <RecipeListToolbar
                        onAddClicked={handleAdd}
                        onSearchStringChanged={setSearchString}
                        numberOfRecipes={recipeList.length}
                    />
                </StickyHeader>
                {/*Content - List of recipe cards */}
                <div className="w-full pt-4">
                    <div
                        className="grid gap-6 grid-cols-[repeat(auto-fit,minmax(220px,auto))] max-w-6xl mx-auto justify-center">
                        {recipeList.map((recipe) => (
                            <RecipeListItem
                                key={recipe.id}
                                title={recipe.title}
                                targetPath={recipe.id !== undefined ? getRecipeDetailUrl(recipe.id) : getRecipeListUrl()} // @todo proper error handling
                            />
                        ))}
                    </div>
                </div>
            </ContentBackground>
        </PageContainer>
    )
}  
