import 'package:flutter/material.dart';
import 'package:freekake/components/bottom_navbar.dart';
import 'package:freekake/components/menu_button.dart';
import 'package:freekake/components/scan_button.dart';
import 'package:freekake/screen/Home_screen.dart';
import 'package:freekake/screen/koleksi_screen.dart';
import 'package:freekake/screen/pustaka_screen.dart';
import 'package:freekake/screen/saya/profile_screen.dart';
import 'package:freekake/providers/menu_selection_provider.dart';
import 'package:provider/provider.dart';

class MainMenu extends StatefulWidget {
  const MainMenu({super.key});
  _MainMenuState createState() => _MainMenuState();
}

class _MainMenuState extends State<MainMenu> {
  int selectedIndex = 0;

  final List<Widget> _screens = [
    HomeScreen(),
    KoleksiScreen(),
    PustakaScreen(),
    ProfileScreen(),
  ];

  // void _onItemTapped(int index) {
  //   Future.delayed(Duration(milliseconds: 100), () {
  //     Navigator.pushReplacement(
  //       context,
  //       MaterialPageRoute(builder: (context) => _screens[index]),
  //     );
  //   });

  //   setState(() {
  //     selectedIndex = index;
  //   });
  // }

  @override
  Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    final selectedIndex =
        Provider.of<MenuSelectionProvider>(context).selectedIndex;
    return Container(
      color: Colors.transparent,
      padding: const EdgeInsets.symmetric(vertical: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        mainAxisSize: MainAxisSize.min,
        children: [
          MenuButton(
            label: "E-furibuddy",
            icon: 'assets/icons/furrybuddy.svg',
            onPress: () => _onItemTapped(context, 0),
            isSelected: selectedIndex == 0,
          ),
          MenuButton(
            label: "Koleksi",
            icon: 'assets/icons/Koleksi.svg',
            onPress: () => _onItemTapped(context, 1),
            isSelected: selectedIndex == 1,
          ),
          SizedBox(width: 100),
          MenuButton(
            label: "Pustaka",
            icon: 'assets/icons/Pustaka.svg',
            onPress: () => _onItemTapped(context, 2),
            isSelected: selectedIndex == 2,
          ),
          MenuButton(
            label: "Saya",
            icon: 'assets/icons/Saya.svg',
            onPress: () => _onItemTapped(context, 3),
            isSelected: selectedIndex == 3,
          ),
        ],
      ),
    );
  }

  void _onItemTapped(BuildContext context, int index) {
    // Mengubah selectedIndex di MenuSelectionProvider
    Provider.of<MenuSelectionProvider>(context, listen: false).selectedIndex =
        index;

    // Pindah halaman sesuai index yang dipilih
    switch (index) {
      case 0:
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => HomeScreen()),
        );
        break;
      case 1:
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => KoleksiScreen()),
        );
        break;
      case 2:
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => PustakaScreen()),
        );
        break;
      case 3:
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => ProfileScreen()),
        );
        break;
    }
  }
}
