import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_color.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_font_size.dart';
import 'package:niogu_ecommerce_v1/features/cart/presentation/providers/cart_provider.dart';
import 'package:sizer/sizer.dart';

class BottomBarApp extends ConsumerWidget {
  final int currentIndex;
  final void Function(int) onTap;

  const BottomBarApp({
    super.key,
    required this.currentIndex,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final bool isTablet = 100.w >= 600;

    final double height = isTablet ? 14.h : 12.h;

    final cartItems = ref.watch(cartItemProvider);

    return Container(
      height: height,
      decoration: BoxDecoration(
        color: Colors.white,
        border: BoxBorder.fromLTRB(
          top: BorderSide(color: Colors.grey.shade300),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          _buildNavItem(Icons.home_filled, "Beranda", 0),

          _buildNavItem(Icons.favorite_outline, "Favorit", 1),

          _buildNavItem(
            Icons.shopping_cart_checkout_outlined,
            "Keranjang",
            2,
            showBadge: true,
            badgeCount: cartItems.length,
          ),

          _buildNavItem(Icons.shopping_bag_outlined, "Pesanan", 3),

          _buildNavItem(Icons.person_outline_outlined, "Akun", 4),
        ],
      ),
    );
  }

  Widget _buildNavItem(
    IconData icon,
    String label,
    int index, {
    bool showBadge = false,
    int badgeCount = 0,
  }) {
    final bool isActive = currentIndex == index;

    return InkWell(
      onTap: () => onTap(index),
      borderRadius: BorderRadius.circular(2.5.w),
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        padding: EdgeInsets.symmetric(horizontal: 3.w, vertical: 1.h),
        decoration: BoxDecoration(
          color: isActive
              ? AppColor.primaryColor.withOpacity(0.08)
              : Colors.transparent,
          borderRadius: BorderRadius.circular(2.5.w),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Badge(
              isLabelVisible: showBadge && badgeCount > 0,
              label: Text(
                badgeCount.toString(),
                style: TextStyle(
                  fontSize: (AppFontSize.small - 1.25).sp,
                  color: Colors.white,
                ),
              ),
              backgroundColor: AppColor.primaryColor,
              child: Icon(
                icon,
                color: isActive ? AppColor.primaryColor : Colors.grey,
                size: 6.w,
              ),
            ),
            SizedBox(height: 0.5.h),
            Text(
              label,
              style: TextStyle(
                fontSize: AppFontSize.small.sp,
                fontWeight: isActive ? FontWeight.bold : FontWeight.normal,
                color: isActive ? AppColor.primaryColor : Colors.grey,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
