import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../notifications/notification_provider.dart';
import '../notifications/notifications_screen.dart';
import '../shift/shift_screen.dart';
import '../tables/table_board_screen.dart';

class HomeScreen extends ConsumerStatefulWidget {
  const HomeScreen({super.key});

  @override
  ConsumerState<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends ConsumerState<HomeScreen> {
  int _tab = 0;

  static const _screens = [
    NotificationsScreen(),
    TableBoardScreen(),
    ShiftScreen(),
  ];

  @override
  Widget build(BuildContext context) {
    final unread = ref.watch(
      notificationProvider.select((s) => s.unreadCount),
    );

    return Directionality(
      textDirection: TextDirection.rtl,
      child: Scaffold(
        body: IndexedStack(index: _tab, children: _screens),
        bottomNavigationBar: NavigationBar(
          selectedIndex: _tab,
          onDestinationSelected: (i) => setState(() => _tab = i),
          destinations: [
            NavigationDestination(
              icon: Badge(
                isLabelVisible: unread > 0,
                label: Text(unread > 9 ? '۹+' : '$unread'),
                child: const Icon(Icons.notifications_outlined),
              ),
              selectedIcon: Badge(
                isLabelVisible: unread > 0,
                label: Text(unread > 9 ? '۹+' : '$unread'),
                child: const Icon(Icons.notifications),
              ),
              label: 'اعلان‌ها',
            ),
            const NavigationDestination(
              icon: Icon(Icons.table_restaurant_outlined),
              selectedIcon: Icon(Icons.table_restaurant),
              label: 'میزها',
            ),
            const NavigationDestination(
              icon: Icon(Icons.person_outline),
              selectedIcon: Icon(Icons.person),
              label: 'شیفت من',
            ),
          ],
        ),
      ),
    );
  }
}
