import 'package:flutter/material.dart';

class DashboardAppBar extends StatelessWidget implements PreferredSizeWidget {
  final int selectedIndex;
  final ValueChanged<int> onTabChange;

  const DashboardAppBar({Key? key, required this.selectedIndex, required this.onTabChange}) : super(key: key);

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight + 10);

  @override
  Widget build(BuildContext context) {
    final isMobile = MediaQuery.of(context).size.width < 768;
    return AppBar(
      // backgroundColor: Colors.white,
      elevation: 0,
      title: Row(
        children: [
          // Logo de la ville
          // Container(
          //   height: 32,
          //   width: 32,
          //   decoration: BoxDecoration(
          //     color: Colors.white,
          //     borderRadius: BorderRadius.circular(8),
          //   ),
          //   child: const Icon(
          //     Icons.location_city,
          //     color: Color(0xFF9CC5C0),
          //     size: 20,
          //   ),
          // ),
          SizedBox(width: MediaQuery.of(context).size.width * 0.19),
          const Text(
            "P'tit Pas",
            style: TextStyle(
              color: Color(0xFF9CC5C0),
              fontWeight: FontWeight.bold,
              fontSize: 18,
            ),
          ),
          SizedBox(width: MediaQuery.of(context).size.width * 0.1),

          // Navigation principale
          _buildNavItem(context, 'Mon tableau de bord', 0),
          const SizedBox(width: 24),
          _buildNavItem(context, 'Trouver une nounou', 1),
          const SizedBox(width: 24),
          _buildNavItem(context, 'Paramètres', 2),
        ],
      ),
      actions: isMobile
          ? [_buildMobileMenu(context)]
          : [
        // Nom de l'utilisateur
        const Padding(
          padding: EdgeInsets.symmetric(horizontal: 16),
          child: Center(
            child: Text(
              'Jean Dupont',
              style: TextStyle(
                color: Colors.black,
                fontSize: 16,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ),
        
        // Bouton déconnexion
        Padding(
          padding: const EdgeInsets.only(right: 16),
          child: TextButton(
            onPressed: () => _handleLogout(context),
            style: TextButton.styleFrom(
              backgroundColor: const Color(0xFF9CC5C0),
              foregroundColor: Colors.white,
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5),
              ),
            ),
            child: const Text('Se déconnecter'),
          ),
        ),
        SizedBox(width: MediaQuery.of(context).size.width * 0.1),
      ],
    );
  }

  Widget _buildNavItem(BuildContext context, String title, int index) {
  final bool isActive = index == selectedIndex;
  return InkWell(
    onTap: () => onTabChange(index),
    child: Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      decoration: BoxDecoration(
        color: isActive ? const Color(0xFF9CC5C0) : Colors.transparent,
        borderRadius: BorderRadius.circular(20),
        border: isActive ? null : Border.all(color: Colors.black26),
      ),
      child: Text(
        title,
        style: TextStyle(
          color: isActive ? Colors.white : Colors.black,
          fontWeight: isActive ? FontWeight.w600 : FontWeight.normal,
          fontSize: 14,
        ),
      ),
    ),
  );
}


  Widget _buildMobileMenu(BuildContext context) {
    return PopupMenuButton<int>(
      icon: const Icon(Icons.menu, color: Colors.white),
      onSelected: (value) {
        if (value == 3) {
          _handleLogout(context);
        }
      },
      itemBuilder: (context) => [
        const PopupMenuItem(value: 0, child: Text("Mon tableau de bord")),
        const PopupMenuItem(value: 1, child: Text("Trouver une nounou")),
        const PopupMenuItem(value: 2, child: Text("Paramètres")),
        const PopupMenuDivider(),
        const PopupMenuItem(value: 3, child: Text("Se déconnecter")),
      ],
    );
  }

  void _handleLogout(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('Déconnexion'),
        content: const Text('Êtes-vous sûr de vouloir vous déconnecter ?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('Annuler'),
          ),
          ElevatedButton(
            onPressed: () {
              Navigator.pop(context);
              // TODO: Implémenter la logique de déconnexion
            },
            child: const Text('Déconnecter'),
          ),
        ],
      ),
    );
  }
}