import 'package:flutter/material.dart';
import 'package:furibase/components/bottom_navbar.dart';
import 'package:furibase/components/menu_button.dart';
import 'package:furibase/components/scan_button.dart';
import 'package:furibase/screen/Home_screen.dart';
import 'package:furibase/screen/koleksi_screen.dart';
import 'package:furibase/screen/pustaka_screen.dart';
import 'package:furibase/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: 'icons/furrybuddy.svg',
            onPress:
                () => {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => HomeScreen()),
                  ),
                },
          ),
          MenuButton(
            label: "Koleksi",
            icon: 'icons/koleksi.svg',
            onPress:
                () => {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => KoleksiScreen()),
                  ),
                },
          ),
          SizedBox(width: 100),
          MenuButton(
            label: "Pustaka",
            icon: 'icons/Pustaka.svg',
            onPress:
                () => {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => PustakaScreen()),
                  ),
                },
          ),
          MenuButton(
            label: "Saya",
            icon: 'icons/Saya.svg',
            onPress:
                () => {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => ProfileScreen()),
                  ),
                },
          ),
        ],
      ),
    );
  }

  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;
    }
  }
}
