import 'package:flutter/material.dart';
import 'collections_screen.dart';
import 'scan_tab.dart';
import 'profile_screen.dart';

/// Bottom-navigation shell that hosts the three main tabs.
class HomeShell extends StatefulWidget {
  const HomeShell({super.key});

  @override
  State<HomeShell> createState() => _HomeShellState();
}

class _HomeShellState extends State<HomeShell> {
  int _currentIndex = 0;
  final _collectionsKey = GlobalKey<CollectionsScreenState>();
  final _scanKey = GlobalKey<ScanTabState>();

  late final List<Widget> _pages = <Widget>[
    CollectionsScreen(key: _collectionsKey),
    ScanTab(key: _scanKey),
    const ProfileScreen(),
  ];

  void _onTabSelected(int i) {
    setState(() => _currentIndex = i);
    if (i == 0) {
      _collectionsKey.currentState?.refresh();
    }
    if (i == 1) {
      _scanKey.currentState?.refresh();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: NavigationBar(
        selectedIndex: _currentIndex,
        onDestinationSelected: _onTabSelected,
        destinations: const [
          NavigationDestination(
            icon: Icon(Icons.collections_bookmark_outlined),
            selectedIcon: Icon(Icons.collections_bookmark),
            label: 'Collections',
          ),
          NavigationDestination(
            icon: Icon(Icons.qr_code_scanner_outlined),
            selectedIcon: Icon(Icons.qr_code_scanner),
            label: 'Scan',
          ),
          NavigationDestination(
            icon: Icon(Icons.person_outline),
            selectedIcon: Icon(Icons.person),
            label: 'Profile',
          ),
        ],
      ),
    );
  }
}
