import 'package:flutter/material.dart';
import 'package:glass_liquid_navbar/glass_liquid_navbar.dart';
import 'package:unionapp/pages/score/score_page.dart';
import 'package:unionapp/pages/user/userpage.dart';
import 'pages/home/home_page.dart';
import 'pages/map_page.dart';
import 'pages/setting_page.dart';

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

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

class _HomeScreenState extends State<HomeScreen> {
  int _currentIndex = 0;

  // ✅ 定义切换 Tab 的方法
  void _switchTab(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  final List<Widget> _pages = [];

  @override
  void initState() {
    super.initState();
    // ✅ 初始化时传入回调函数，这样子页面可以调用它来切换 Tab
    _pages.addAll([
      HomePage(onSwitchTab: _switchTab),
      ScorePage(),
      MapPage(),
      UserPage(),
      SettingPage(),
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBody: true,
      body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: LiquidGlassNavbar(
        currentIndex: _currentIndex,
        onTap: (index) {
          _switchTab(index);
        },
        floatingOffset: 17,
        animationDuration: const Duration(milliseconds: 200),
        enableHaptics: true,
        showLabels: true,
        isFullWidth: false,
        items: const [
          LiquidNavItem(
            icon: Icons.home_rounded,
            activeIcon: Icons.home_filled,
            label: '首页',
          ),
          LiquidNavItem(
            icon: Icons.score,
            activeIcon: Icons.score_outlined,
            label: '成绩',
          ),
          LiquidNavItem(
            icon: Icons.map,
            activeIcon: Icons.map_outlined,
            label: '地图',
          ),
          LiquidNavItem(
            icon: Icons.person_rounded,
            activeIcon: Icons.person_pin,
            label: '我的',
          ),
          LiquidNavItem(
            icon: Icons.settings_rounded,
            activeIcon: Icons.settings_applications_rounded,
            label: '设置',
          ),
        ],
      ),
    );
  }
}