import 'package:flutter/material.dart';

import '../../../../api/marianumconnect/queries/get_ticker_nav/get_ticker_nav_response.dart';
import '../../../../api/marianumconnect/queries/get_ticker_page/get_ticker_page_response.dart';
import '../../../../theming/app_theme.dart';

/// Shared navigation for the ticker module: the "Aktuelles" home entry followed
/// by the section-grouped pages. Used as the permanent tablet sidebar
/// ([TickerNavList]) and, via [tickerNavItems], inside the phone bottom sheet.
/// [selectedSlug] is null while the home surface is shown.
class TickerNavList extends StatelessWidget {
  final List<TickerNavSection> sections;
  final String? selectedSlug;
  final VoidCallback onSelectHome;
  final void Function(TickerNavPage page) onSelectPage;
  final void Function(TickerNavPage page) onRedirect;

  const TickerNavList({
    super.key,
    required this.sections,
    required this.selectedSlug,
    required this.onSelectHome,
    required this.onSelectPage,
    required this.onRedirect,
  });

  @override
  Widget build(BuildContext context) => ListView(
    padding: EdgeInsets.zero,
    children: [
      Padding(
        padding: const EdgeInsets.fromLTRB(
          AppSpacing.md,
          AppSpacing.md,
          AppSpacing.md,
          AppSpacing.sm,
        ),
        child: Text('Ticker', style: Theme.of(context).textTheme.titleLarge),
      ),
      ...tickerNavItems(
        context,
        sections: sections,
        selectedSlug: selectedSlug,
        onSelectHome: onSelectHome,
        onSelectPage: onSelectPage,
        onRedirect: onRedirect,
      ),
    ],
  );
}

/// The ticker nav rows (Aktuelles home entry + section-grouped pages) as a flat
/// widget list, so they render both inside the sidebar's [ListView] and inside
/// the bottom sheet's shared [Column] without a bounded-height wrapper.
List<Widget> tickerNavItems(
  BuildContext context, {
  required List<TickerNavSection> sections,
  required String? selectedSlug,
  required VoidCallback onSelectHome,
  required void Function(TickerNavPage page) onSelectPage,
  required void Function(TickerNavPage page) onRedirect,
}) {
  final theme = Theme.of(context);
  return [
    ListTile(
      leading: const Icon(Icons.campaign_outlined),
      title: const Text('Aktuelles'),
      selected: selectedSlug == null,
      selectedTileColor: theme.colorScheme.secondaryContainer,
      onTap: onSelectHome,
    ),
    for (final section in sections) ...[
      Padding(
        padding: const EdgeInsets.fromLTRB(
          AppSpacing.md,
          AppSpacing.md,
          AppSpacing.md,
          AppSpacing.xs,
        ),
        child: Text(
          section.title,
          style: theme.textTheme.titleSmall?.copyWith(
            color: theme.colorScheme.primary,
          ),
        ),
      ),
      for (final page in section.pages)
        ListTile(
          leading: Icon(_iconFor(page.kind)),
          title: Text(page.title, overflow: TextOverflow.ellipsis),
          trailing: page.kind == TickerPageKind.redirect
              ? const Icon(Icons.open_in_new)
              : null,
          selected: page.slug == selectedSlug,
          selectedTileColor: theme.colorScheme.secondaryContainer,
          onTap: () => page.kind == TickerPageKind.redirect
              ? onRedirect(page)
              : onSelectPage(page),
        ),
    ],
  ];
}

IconData _iconFor(String kind) {
  switch (kind) {
    case TickerPageKind.redirect:
      return Icons.link;
    case TickerPageKind.proxiedFile:
      return Icons.picture_as_pdf_outlined;
    default:
      return Icons.article_outlined;
  }
}
