import 'package:flutter/material.dart';
import 'package:flutter_tabler_icons/flutter_tabler_icons.dart';
import 'package:get/get.dart';
import 'package:sigap/navigation_menu.dart';

class CustomBottomNavigationBar extends StatelessWidget {
  const CustomBottomNavigationBar({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = NavigationController.instance;
    final theme = Theme.of(context);

    return Container(
      decoration: BoxDecoration(
        color: theme.scaffoldBackgroundColor,
        borderRadius: const BorderRadius.vertical(top: Radius.circular(30)),
        boxShadow: [
          BoxShadow(
            color: theme.shadowColor.withOpacity(0.1),
            blurRadius: 10,
            offset: const Offset(0, -5),
          ),
        ],
      ),
      child: ClipRRect(
        borderRadius: const BorderRadius.vertical(top: Radius.circular(30)),
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 10),
          child: Obx(
            () => Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildNavItem(
                  context,
                  "Home",
                  Icons.home,
                  0,
                  controller.selectedIndex.value == 0,
                ),
                _buildNavItem(
                  context,
                  "Search",
                  Icons.search,
                  1,
                  controller.selectedIndex.value == 1,
                ),
                _buildPanicButton(context),
                _buildNavItem(
                  context,
                  "History",
                  Icons.history,
                  3,
                  controller.selectedIndex.value == 3,
                ),
                _buildNavItem(
                  context,
                  "Profile",
                  Icons.person_outline,
                  4,
                  controller.selectedIndex.value == 4,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildNavItem(
    BuildContext context,
    String label,
    IconData icon,
    int index,
    bool isSelected,
  ) {
    final theme = Theme.of(context);

    return GestureDetector(
      onTap: () => NavigationController.instance.changeIndex(index),
      behavior: HitTestBehavior.opaque,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            icon,
            color: isSelected ? theme.primaryColor : theme.hintColor,
            size: 24,
          ),
          const SizedBox(height: 4),
          Text(
            label,
            style: TextStyle(
              color: isSelected ? theme.primaryColor : theme.hintColor,
              fontSize: 12,
              fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildPanicButton(BuildContext context) {
    final theme = Theme.of(context);

    return GestureDetector(
      onTap: () => NavigationController.instance.changeIndex(2),
      child: Container(
        width: 60,
        height: 60,
        decoration: BoxDecoration(
          color: theme.primaryColor,
          shape: BoxShape.circle,
        ),
        child: Icon(
          TablerIcons.plus,
          color: theme.scaffoldBackgroundColor,
          size: 30,
        ),
      ),
    );
  }
}
