"""Gallery control components for view toggle and type filtering."""

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

# %% auto #0
__all__ = ['FILE_TYPE_LABELS', 'render_view_toggle', 'render_type_filter_button', 'render_type_filters',
           'render_gallery_controls']

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

from fasthtml.common import Div, Button, Span

# Tailwind utilities
from cjm_fasthtml_tailwind.utilities.spacing import p, m
from cjm_fasthtml_tailwind.utilities.flexbox_and_grid import (
    flex_display, items, justify, flex_wrap, gap
)
from cjm_fasthtml_tailwind.utilities.borders import rounded
from cjm_fasthtml_tailwind.core.base import combine_classes

# DaisyUI utilities
from cjm_fasthtml_daisyui.components.actions.button import (
    btn, btn_colors, btn_sizes, btn_styles
)
from cjm_fasthtml_daisyui.components.data_display.badge import badge, badge_colors, badge_styles
from cjm_fasthtml_daisyui.utilities.semantic_colors import bg_dui, text_dui
from cjm_fasthtml_daisyui.components.navigation.pagination import join, join_item

# Local imports
from cjm_file_discovery.core.models import FileType
from ..core.config import GalleryConfig, ViewMode
from cjm_fasthtml_media_gallery.core.icons import (
    get_gallery_icon, get_media_type_icon, MEDIA_TYPE_BADGE_COLORS
)

# %% ../../nbs/components/controls.ipynb #e5f6a7b8
def render_view_toggle(
    current_mode: ViewMode,           # Current view mode
    toggle_url: str,                  # URL for toggle handler
    hx_target: Optional[str] = None,  # HTMX target for swap
) -> Any:  # View toggle button group
    """Render view mode toggle buttons."""
    def make_button(mode: ViewMode, icon_key: str, label: str) -> Button:
        is_active = current_mode == mode
        
        btn_cls = combine_classes(
            btn, btn_sizes.sm, join_item,
            btn_colors.primary if is_active else btn_styles.ghost
        )
        
        attrs = {
            "cls": btn_cls,
            "title": label,
            "aria-label": label,
        }
        
        if not is_active:
            attrs["hx_post"] = toggle_url
            attrs["hx_vals"] = f'{{"view_mode": "{mode.value}"}}'
            if hx_target:
                attrs["hx_target"] = hx_target
            attrs["hx_swap"] = "outerHTML"
        
        return Button(
            get_gallery_icon(icon_key, size=4),
            **attrs
        )
    
    return Div(
        make_button(ViewMode.GRID, "grid_view", "Grid view"),
        make_button(ViewMode.LIST, "list_view", "List view"),
        cls=str(join)
    )

# %% ../../nbs/components/controls.ipynb #h8c9d0e1
# Display names for file types
FILE_TYPE_LABELS: dict[FileType, str] = {
    FileType.AUDIO: "Audio",
    FileType.VIDEO: "Video",
    FileType.IMAGE: "Images",
    FileType.DOCUMENT: "Documents",
    FileType.CODE: "Code",
    FileType.DATA: "Data",
    FileType.ARCHIVE: "Archives",
    FileType.OTHER: "Other",
}

# %% ../../nbs/components/controls.ipynb #i9d0e1f2
def render_type_filter_button(
    file_type: FileType,              # File type for this button
    is_active: bool,                  # Whether this type is currently shown
    filter_url: str,                  # URL for filter handler
    hx_target: Optional[str] = None,  # HTMX target for swap
    count: Optional[int] = None,      # Number of files of this type
) -> Button:  # Filter button
    """Render a single type filter button."""
    label = FILE_TYPE_LABELS.get(file_type, file_type.value.title())
    badge_color = MEDIA_TYPE_BADGE_COLORS.get(file_type, str(badge_styles.ghost))
    
    btn_cls = combine_classes(
        btn, btn_sizes.sm,
        btn_colors.primary if is_active else btn_styles.ghost,
        gap(2)
    )
    
    attrs = {
        "cls": btn_cls,
        "hx_post": filter_url,
        "hx_vals": f'{{"file_type": "{file_type.value}", "toggle": "true"}}',
    }
    if hx_target:
        attrs["hx_target"] = hx_target
    attrs["hx_swap"] = "outerHTML"
    
    # Build button content
    content = [
        get_media_type_icon(file_type, size=4, with_color=not is_active),
        Span(label),
    ]
    
    # Add count badge if provided
    if count is not None:
        content.append(
            Span(
                str(count),
                cls=combine_classes(badge, badge_color if is_active else badge_styles.ghost)
            )
        )
    
    return Button(*content, **attrs)

# %% ../../nbs/components/controls.ipynb #k1f2g3h4
def render_type_filters(
    available_types: List[FileType],  # File types to show buttons for
    active_types: List[FileType],     # Currently active (shown) types
    filter_url: str,                  # URL for filter handler
    hx_target: Optional[str] = None,  # HTMX target for swap
    type_counts: Optional[dict[FileType, int]] = None,  # File counts per type
) -> Any:  # Type filter button group
    """Render type filter buttons."""
    buttons = []
    for file_type in available_types:
        is_active = file_type in active_types
        count = type_counts.get(file_type) if type_counts else None
        buttons.append(
            render_type_filter_button(
                file_type, is_active, filter_url, hx_target, count
            )
        )
    
    return Div(
        *buttons,
        cls=combine_classes(flex_display, flex_wrap.wrap, gap(2))
    )

# %% ../../nbs/components/controls.ipynb #n4i5j6k7
def render_gallery_controls(
    config: GalleryConfig,            # Gallery configuration
    current_view: ViewMode,           # Current view mode
    active_types: List[FileType],     # Currently active type filters
    toggle_view_url: str,             # URL for view toggle
    filter_url: str,                  # URL for type filter
    hx_target: Optional[str] = None,  # HTMX target
    type_counts: Optional[dict[FileType, int]] = None,  # File counts per type
    controls_id: Optional[str] = None,  # HTML ID for controls container
) -> Any:  # Controls bar component
    """Render the gallery controls bar."""
    controls = []
    
    # View toggle
    if config.allow_view_toggle:
        controls.append(
            render_view_toggle(current_view, toggle_view_url, hx_target)
        )
    
    # Type filters
    if config.filter.allow_type_filter:
        controls.append(
            render_type_filters(
                available_types=config.filter.enabled_types,
                active_types=active_types,
                filter_url=filter_url,
                hx_target=hx_target,
                type_counts=type_counts
            )
        )
    
    container_cls = combine_classes(
        flex_display, flex_wrap.wrap, items.center, justify.between,
        gap(4), p(4),
        bg_dui.base_200,
        rounded.t.lg
    )
    
    attrs = {"cls": container_cls}
    if controls_id:
        attrs["id"] = controls_id
    
    return Div(*controls, **attrs)
