"""Main gallery component that combines controls, views, and preview."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/components/gallery.ipynb.

# %% auto #0
__all__ = ['render_gallery_content', 'render_media_gallery']

# %% ../../nbs/components/gallery.ipynb #c3d4e5f6
from typing import Any, Callable, List, Optional

from fasthtml.common import Div

# Tailwind utilities
from cjm_fasthtml_tailwind.utilities.sizing import w, h
from cjm_fasthtml_tailwind.utilities.flexbox_and_grid import flex_display, flex_direction, flex
from cjm_fasthtml_tailwind.utilities.borders import border
from cjm_fasthtml_tailwind.utilities.layout import overflow
from cjm_fasthtml_tailwind.core.base import combine_classes

# DaisyUI utilities
from cjm_fasthtml_daisyui.utilities.semantic_colors import bg_dui, border_dui
from cjm_fasthtml_daisyui.utilities.border_radius import border_radius

# Local imports
from cjm_file_discovery.core.models import FileInfo, FileType
from ..core.config import GalleryConfig, ViewMode
from ..core.html_ids import GalleryHtmlIds
from .controls import render_gallery_controls
from cjm_fasthtml_media_gallery.components.grid_view import (
    render_grid_view, render_grid_empty_state
)
from cjm_fasthtml_media_gallery.components.list_view import (
    render_list_view, render_list_empty_state
)
from .preview import render_preview_modal
from ..patterns.pagination import render_pagination, PaginationInfo

# %% ../../nbs/components/gallery.ipynb #e5f6a7b8
def render_gallery_content(
    files: List[FileInfo],            # Files to display
    config: GalleryConfig,            # Gallery configuration
    view_mode: ViewMode,              # Current view mode
    get_file_url: Optional[Callable[[str], str]] = None,  # Function to get file URL
    selected_paths: Optional[List[str]] = None,  # Currently selected paths
    preview_url: Optional[str] = None,  # URL for preview action
    select_url: Optional[str] = None,   # URL for selection action
    hx_target: Optional[str] = None,    # HTMX target for swaps
    start_index: int = 0,               # Starting index (for pagination)
) -> Any:  # Content component
    """Render the gallery content (grid or list view)."""
    # Handle empty state
    if not files:
        if view_mode == ViewMode.GRID:
            return render_grid_empty_state()
        else:
            return render_list_empty_state()
    
    # Render appropriate view
    if view_mode == ViewMode.GRID:
        return render_grid_view(
            files=files,
            config=config,
            get_file_url=get_file_url,
            selected_paths=selected_paths,
            preview_url=preview_url,
            select_url=select_url,
            hx_target=hx_target,
            start_index=start_index
        )
    else:
        return render_list_view(
            files=files,
            config=config,
            selected_paths=selected_paths,
            preview_url=preview_url,
            select_url=select_url,
            hx_target=hx_target,
            start_index=start_index
        )

# %% ../../nbs/components/gallery.ipynb #h8c9d0e1
def render_media_gallery(
    files: List[FileInfo],            # Files to display
    config: GalleryConfig,            # Gallery configuration
    view_mode: Optional[ViewMode] = None,  # Current view mode (defaults to config)
    active_types: Optional[List[FileType]] = None,  # Active type filters
    get_file_url: Optional[Callable[[str], str]] = None,  # Function to get file URL
    selected_paths: Optional[List[str]] = None,  # Currently selected paths
    # Route URLs
    toggle_view_url: Optional[str] = None,  # URL for view toggle
    filter_url: Optional[str] = None,       # URL for type filter
    preview_url: Optional[str] = None,      # URL for preview action
    select_url: Optional[str] = None,       # URL for selection action
    page_url: Optional[str] = None,         # URL for pagination
    # Pagination
    current_page: int = 1,                  # Current page number
    total_items: Optional[int] = None,      # Total items (for pagination)
    type_counts: Optional[dict[FileType, int]] = None,  # File counts per type
) -> Any:  # Complete gallery component
    """Render the complete media gallery."""
    # Defaults
    view_mode = view_mode or config.default_view
    active_types = active_types if active_types is not None else config.filter.enabled_types
    selected_paths = selected_paths or []
    
    # HTMX target for content swaps
    content_target = GalleryHtmlIds.as_selector(config.content_id)
    gallery_target = GalleryHtmlIds.as_selector(config.gallery_id)
    
    # Filter files by active types
    filtered_files = [
        f for f in files
        if f.file_type in active_types and config.filter.matches(f)
    ]
    
    # Calculate pagination info
    total_filtered = len(filtered_files)
    page_size = config.pagination.items_per_page
    start_idx = (current_page - 1) * page_size
    end_idx = start_idx + page_size
    paginated_files = filtered_files[start_idx:end_idx]
    
    # Build gallery
    components = []
    
    # Controls bar
    if config.allow_view_toggle or config.filter.allow_type_filter:
        components.append(
            render_gallery_controls(
                config=config,
                current_view=view_mode,
                active_types=active_types,
                toggle_view_url=toggle_view_url or "",
                filter_url=filter_url or "",
                hx_target=gallery_target,
                type_counts=type_counts,
                controls_id=config.controls_id
            )
        )
    
    # Content area
    content = render_gallery_content(
        files=paginated_files,
        config=config,
        view_mode=view_mode,
        get_file_url=get_file_url,
        selected_paths=selected_paths,
        preview_url=preview_url,
        select_url=select_url,
        hx_target=gallery_target,
        start_index=start_idx
    )
    
    components.append(
        Div(
            content,
            id=config.content_id,
            cls=combine_classes(bg_dui.base_100, overflow.auto, flex(1))
        )
    )
    
    # Pagination controls
    if config.pagination.show_pagination and page_url:
        pagination_info = PaginationInfo(
            total_items=total_filtered,
            items_per_page=page_size,
            current_page=current_page
        )
        pagination_component = render_pagination(
            info=pagination_info,
            page_url=page_url,
            hx_target=gallery_target
        )
        if pagination_component:  # None if only 1 page
            components.append(pagination_component)
    
    # Preview modal
    if config.preview.enable_preview:
        components.append(
            render_preview_modal(modal_id=config.preview_modal_id)
        )
    
    # Container
    container_cls = combine_classes(
        flex_display, flex_direction.col,
        w.full, h.full,
        border(), border_dui.base_300, border_radius.box,
        overflow.hidden
    )
    
    return Div(
        *components,
        id=config.gallery_id,
        cls=container_cls
    )
