import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers.dart';
import '../game/enums.dart';
import 'battle_screen.dart';
import 'inventory_screen.dart';
import 'settings_screen.dart';
import '../widgets.dart';
import '../game/config.dart';

class MainWrapper extends StatefulWidget {
  const MainWrapper({super.key});

  @override
  State<MainWrapper> createState() => _MainWrapperState();
}

class _MainWrapperState extends State<MainWrapper> {
  int _currentIndex = 0;

  final List<Widget> _screens = [
    const BattleScreen(),
    const InventoryScreen(),
    const SettingsScreen(),
  ];

  @override
  Widget build(BuildContext context) {
    return Consumer<BattleProvider>(
      builder: (context, battleProvider, child) {
        // Determine the first tab's icon and label based on StageType
        String stageLabel = "Battle";
        IconData stageIcon = Icons.flash_on;

        // Ensure we check null safety if currentStage isn't ready (though it should be)
        // battleProvider.currentStage might be accessed safely if standardized
        // Assuming battleProvider.currentStage is accessible or we check stage type logic
        try {
          final stageType = battleProvider.currentStage.type;
          switch (stageType) {
            case StageType.battle:
            case StageType.elite:
              stageLabel = "Battle";
              stageIcon = Icons.flash_on;
              break;
            case StageType.shop:
              stageLabel = "Shop";
              stageIcon = Icons.store;
              break;
            case StageType.rest:
              stageLabel = "Rest";
              stageIcon = Icons.hotel;
              break;
          }
        } catch (e) {
          // Fallback if not initialized
        }

        return Scaffold(
          backgroundColor: ThemeConfig.mainMenuBgTop,
          body: Center(
            child: ResponsiveContainer(
              child: Scaffold(
                body: IndexedStack(index: _currentIndex, children: _screens),
                bottomNavigationBar: Theme(
                  data: Theme.of(
                    context,
                  ).copyWith(canvasColor: ThemeConfig.mainMenuBgBottom),
                  child: BottomNavigationBar(
                    currentIndex: _currentIndex,
                    onTap: (index) {
                      setState(() {
                        _currentIndex = index;
                      });
                    },
                    backgroundColor: ThemeConfig.mainMenuBgBottom,
                    selectedItemColor: ThemeConfig.mainIconColor,
                    unselectedItemColor: ThemeConfig.textColorGrey,
                    selectedFontSize:
                        ThemeConfig.fontSizeLarge, // Highlight selection
                    unselectedFontSize: ThemeConfig.fontSizeSmall,
                    type: BottomNavigationBarType
                        .fixed, // Ensure consistent formatting
                    items: [
                      BottomNavigationBarItem(
                        icon: Icon(stageIcon),
                        label: stageLabel,
                      ),
                      const BottomNavigationBarItem(
                        icon: Icon(Icons.backpack),
                        label: 'Inventory',
                      ),
                      const BottomNavigationBarItem(
                        icon: Icon(Icons.settings),
                        label: 'Settings',
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
