import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:logging/logging.dart';
import 'package:provider/provider.dart';

import '../routes.dart';
import '../services/notifications_manager.dart';
import '../utils/active_profile_selector.dart';

enum NavBarButtons {
  timelines,
  notifications,
  contacts,
  search,
}

class AppBottomNavBar extends StatelessWidget {
  static final _logger = Logger('$AppBottomNavBar');
  final NavBarButtons currentButton;
  final Function()? onHomeButtonReclick;

  const AppBottomNavBar({
    super.key,
    required this.currentButton,
    this.onHomeButtonReclick,
  });

  @override
  Widget build(BuildContext context) {
    final nmResult = context
        .watch<ActiveProfileSelector<NotificationsManager>>()
        .activeEntry;
    final hasNotifications = nmResult.fold(
        onSuccess: (manager) =>
            manager.notifications.where((n) => !n.dismissed).isNotEmpty,
        onError: (error) {
          _logger.info('Error getting notifications manager: $error');
          return false;
        });
    return BottomNavigationBar(
      onTap: (index) {
        final newButton = _indexToButton(index);
        if (newButton == currentButton) {
          if (newButton == NavBarButtons.timelines &&
              onHomeButtonReclick != null) {
            onHomeButtonReclick!();
          }
          return;
        }

        switch (newButton) {
          case NavBarButtons.timelines:
            context.go(ScreenPaths.timelines);
            break;
          case NavBarButtons.notifications:
            context.pushNamed(ScreenPaths.notifications);
            break;
          case NavBarButtons.contacts:
            context.pushNamed(ScreenPaths.contacts);
            break;
          case NavBarButtons.search:
            context.pushNamed(ScreenPaths.search);
            break;
        }
      },
      type: BottomNavigationBarType.fixed,
      currentIndex: _buttonToIndex(currentButton),
      items: _menuItems(context, hasNotifications),
    );
  }

  int _buttonToIndex(NavBarButtons button) {
    switch (button) {
      case NavBarButtons.timelines:
        return 0;
      case NavBarButtons.notifications:
        return 1;
      case NavBarButtons.contacts:
        return 2;
      case NavBarButtons.search:
        return 3;
    }
  }

  NavBarButtons _indexToButton(int index) {
    if (index == 0) {
      return NavBarButtons.timelines;
    }

    if (index == 1) {
      return NavBarButtons.notifications;
    }

    if (index == 2) {
      return NavBarButtons.contacts;
    }

    if (index == 3) {
      return NavBarButtons.search;
    }

    throw ArgumentError('$index has no button type');
  }

  List<BottomNavigationBarItem> _menuItems(
      BuildContext context, bool hasNotifications) {
    return [
      const BottomNavigationBarItem(
        label: 'Timelines',
        icon: Icon(Icons.home_outlined),
        activeIcon: Icon(Icons.home_filled),
      ),
      BottomNavigationBarItem(
        label: 'Notifications',
        icon: Icon(hasNotifications
            ? Icons.notifications_active_outlined
            : Icons.notifications_none_outlined),
        activeIcon: Icon(hasNotifications
            ? Icons.notifications_active
            : Icons.notifications),
      ),
      const BottomNavigationBarItem(
        label: 'Contacts',
        icon: Icon(Icons.people_outline),
        activeIcon: Icon(Icons.people_sharp),
      ),
      const BottomNavigationBarItem(
        label: 'Search',
        icon: Icon(Icons.search),
        activeIcon: Icon(Icons.search),
      ),
    ];
  }
}
