import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:proxibuy/core/routes/app_router.dart';

class CustomBottomNav extends StatelessWidget {
  final Widget child;

  const CustomBottomNav({super.key, required this.child});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          child,
          Positioned(
            bottom: 16,
            left: 16,
            right: 16,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(16),
              child: BottomNavigationBar(
                currentIndex: _getSelectedIndex(context),
                landscapeLayout: BottomNavigationBarLandscapeLayout.centered,
                onTap: (index) => _onItemTapped(context, index),
                items: const [
                  BottomNavigationBarItem(
                      icon: Icon(Icons.home), label: "Home"),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.explore), label: "Explore"),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.settings), label: "Settings"),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  int _getSelectedIndex(BuildContext context) {
    final String location = GoRouterState.of(context).uri.toString();
    if (location.startsWith(AppRouter.explore)) return 1;
    if (location.startsWith(AppRouter.setting)) return 2;
    return 0;
  }

  void _onItemTapped(BuildContext context, int index) {
    switch (index) {
      case 0:
        context.go(AppRouter.initial);
        break;
      case 1:
        context.go(AppRouter.explore);
        break;
      case 2:
        context.go(AppRouter.setting);
        break;
    }
  }
}
