import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:p_tits_pas/models/couple_garde.dart'; import 'package:p_tits_pas/services/api/api_config.dart'; import 'package:p_tits_pas/widgets/common/auth_network_image.dart'; import 'package:p_tits_pas/widgets/quotidien/quotidien_theme.dart'; /// Rôle qui consulte le bandeau : côté parent (enfant | nounou) ou côté AM /// (enfant | parent(s)). Le composant est le même, seule la lecture change. /// Ticket #167 (parent) ; #170 réutilise en mode AM. enum CoupleBandeauMode { parent, assistanteMaternelle } /// Bandeau « couple de garde » en haut de la colonne gauche du TdB. /// /// - Plusieurs couples → contrôle unique avec chevron (dropdown de bascule). /// - Un seul couple → affichage informatif (pas de chevron, pas de menu). /// - Aucun couple → état vide discret. class CoupleSelectorBandeau extends StatefulWidget { final CoupleBandeauMode mode; final List couples; final String? selectedCoupleId; final ValueChanged? onCoupleSelected; final bool loading; final String? errorMessage; final VoidCallback? onRetry; const CoupleSelectorBandeau({ super.key, this.mode = CoupleBandeauMode.parent, required this.couples, this.selectedCoupleId, this.onCoupleSelected, this.loading = false, this.errorMessage, this.onRetry, }); @override State createState() => _CoupleSelectorBandeauState(); } class _CoupleSelectorBandeauState extends State { // On mémorise l'assignation des couleurs par ID pour que l'enfant garde SA couleur // même si l'ordre change, mais on garantit l'unicité lors de la première vue. final Map _colorMap = {}; CoupleGarde? get _selected { if (widget.couples.isEmpty) return null; if (widget.selectedCoupleId != null) { for (final c in widget.couples) { if (c.id == widget.selectedCoupleId) return c; } } for (final c in widget.couples) { if (c.courant) return c; } return widget.couples.first; } void _assignColors() { int nextColorIndex = 0; // On assigne séquentiellement une couleur à chaque nouveau couple. // S'il était déjà connu, il garde sa couleur (dans _colorMap). for (final c in widget.couples) { if (!_colorMap.containsKey(c.id)) { // On s'assure de ne pas écraser une couleur déjà prise // (pour les cas où on a moins de 5 enfants) while (_colorMap.values.contains(nextColorIndex % 5) && _colorMap.length < 5) { nextColorIndex++; } _colorMap[c.id] = nextColorIndex++; } } } @override Widget build(BuildContext context) { if (!widget.loading && widget.couples.isNotEmpty) { _assignColors(); } return LayoutBuilder(builder: (context, constraints) { if (widget.loading) return const _CoupleBandeauSkeleton(); if (widget.errorMessage != null) { return _CoupleBandeauError(message: widget.errorMessage!, onRetry: widget.onRetry); } if (widget.couples.isEmpty) return const _CoupleBandeauEmpty(); final selected = _selected!; final multi = widget.couples.length > 1; final card = _CoupleCard( mode: widget.mode, couple: selected, showChevron: multi, colorIndex: _colorMap[selected.id], ); if (!multi) return card; return Theme( data: Theme.of(context).copyWith( hoverColor: Colors.transparent, splashColor: Colors.transparent, highlightColor: Colors.transparent, focusColor: Colors.transparent, ), child: PopupMenuButton( tooltip: 'Changer de garde', offset: const Offset(0, 95), // Décalé sous la carte principale (+5px de marge) color: Colors.transparent, // Fond de la popup transparent elevation: 0, constraints: BoxConstraints.tightFor(width: constraints.maxWidth), padding: EdgeInsets.zero, onSelected: (id) { final chosen = widget.couples.firstWhere((c) => c.id == id); widget.onCoupleSelected?.call(chosen); }, itemBuilder: (context) => [ for (final c in widget.couples) if (c.id != selected.id) PopupMenuItem( value: c.id, padding: EdgeInsets.zero, height: 100, // Hauteur de la carte + un peu de marge child: Padding( padding: const EdgeInsets.only(bottom: 10), // Marge entre les cartes du menu child: _CoupleCard( mode: widget.mode, couple: c, showChevron: false, selected: false, isMenuItem: true, colorIndex: _colorMap[c.id], ), ), ), ], child: card, ), ); }); } } /// Carte principale : enfant à gauche, séparateur, AM/parents à droite. class _CoupleCard extends StatefulWidget { final CoupleBandeauMode mode; final CoupleGarde couple; final bool showChevron; final bool selected; final bool isMenuItem; final int? colorIndex; const _CoupleCard({ required this.mode, required this.couple, required this.showChevron, this.selected = true, this.isMenuItem = false, this.colorIndex, }); @override State<_CoupleCard> createState() => _CoupleCardState(); } class _CoupleCardState extends State<_CoupleCard> { bool _isHovered = false; @override Widget build(BuildContext context) { return MouseRegion( onEnter: (_) => setState(() => _isHovered = true), onExit: (_) => setState(() => _isHovered = false), cursor: SystemMouseCursors.click, child: AnimatedContainer( duration: const Duration(milliseconds: 150), height: 90, decoration: BoxDecoration( image: DecorationImage( image: AssetImage(QuotidienTheme.bandeauAssetForCouple(widget.couple.id, colorIndex: widget.colorIndex)), fit: BoxFit.fill, colorFilter: (!widget.selected && widget.isMenuItem) ? ColorFilter.mode(Colors.white.withValues(alpha: _isHovered ? 0.15 : 0.4), BlendMode.lighten) : (_isHovered && !widget.isMenuItem ? ColorFilter.mode(Colors.white.withValues(alpha: 0.15), BlendMode.lighten) : null), ), ), padding: const EdgeInsets.symmetric(horizontal: 24), child: Row( children: [ Expanded( child: _MembreTile( photoUrl: widget.couple.enfant.photoUrl, name: widget.couple.enfant.displayName(fallback: 'Enfant'), fallbackIcon: Icons.child_care, ), ), Container( width: 1, height: 44, margin: const EdgeInsets.symmetric(horizontal: 12), color: QuotidienTheme.ink.withValues(alpha: 0.15), ), Expanded( child: _MembreTile( photoUrl: widget.couple.am.photoUrl, name: widget.couple.am.displayName( fallback: widget.mode == CoupleBandeauMode.parent ? 'Nounou' : 'Parent', ), fallbackIcon: widget.mode == CoupleBandeauMode.parent ? Icons.volunteer_activism : Icons.person_outline, ), ), if (widget.showChevron) const Padding( padding: EdgeInsets.only(left: 4), child: Icon( Icons.keyboard_arrow_down, color: QuotidienTheme.ink, ), ), ], ), ), ); } } /// Photo ronde + nom (une moitié du couple). class _MembreTile extends StatelessWidget { final String? photoUrl; final String name; final IconData fallbackIcon; const _MembreTile({ required this.photoUrl, required this.name, required this.fallbackIcon, }); @override Widget build(BuildContext context) { return Row( children: [ _Avatar(photoUrl: photoUrl, fallbackIcon: fallbackIcon, size: 48), const SizedBox(width: 10), Expanded( child: Text( name, maxLines: 1, overflow: TextOverflow.ellipsis, style: GoogleFonts.merienda( fontSize: 15, fontWeight: FontWeight.bold, color: QuotidienTheme.ink, ), ), ), ], ); } } class _Avatar extends StatelessWidget { final String? photoUrl; final IconData fallbackIcon; final double size; const _Avatar({ required this.photoUrl, required this.fallbackIcon, required this.size, }); @override Widget build(BuildContext context) { final url = ApiConfig.absoluteMediaUrl(photoUrl); final placeholder = Container( width: size, height: size, color: QuotidienTheme.lavender.withValues(alpha: 0.35), child: Icon(fallbackIcon, size: size * 0.5, color: QuotidienTheme.ink), ); return ClipOval( child: SizedBox( width: size, height: size, child: url.isEmpty ? placeholder : AuthNetworkImage( url: url, width: size, height: size, fit: BoxFit.cover, errorBuilder: (_, __, ___) => placeholder, ), ), ); } } class _CoupleBandeauSkeleton extends StatelessWidget { const _CoupleBandeauSkeleton(); @override Widget build(BuildContext context) { return Container( height: 90, decoration: BoxDecoration( image: DecorationImage( image: const AssetImage(QuotidienTheme.bandeauLime), // Un asset existant fit: BoxFit.fill, colorFilter: ColorFilter.mode( Colors.white.withValues(alpha: 0.5), BlendMode.lighten, ), ), ), alignment: Alignment.center, child: const SizedBox( width: 26, height: 26, child: CircularProgressIndicator(strokeWidth: 2), ), ); } } class _CoupleBandeauEmpty extends StatelessWidget { const _CoupleBandeauEmpty(); @override Widget build(BuildContext context) { return Container( height: 90, decoration: const BoxDecoration( image: DecorationImage( image: AssetImage(QuotidienTheme.bandeauLime), // Un asset existant par défaut fit: BoxFit.fill, ), ), padding: const EdgeInsets.symmetric(horizontal: 24), child: Row( children: [ const Icon(Icons.info_outline, color: QuotidienTheme.muted), const SizedBox(width: 12), Expanded( child: Text( 'Aucune garde active pour le moment.', style: GoogleFonts.merriweather( fontSize: 14, color: QuotidienTheme.muted, ), ), ), ], ), ); } } class _CoupleBandeauError extends StatelessWidget { final String message; final VoidCallback? onRetry; const _CoupleBandeauError({required this.message, this.onRetry}); @override Widget build(BuildContext context) { return Container( height: 90, decoration: BoxDecoration( image: DecorationImage( image: const AssetImage(QuotidienTheme.bandeauLime), // Un asset existant fit: BoxFit.fill, colorFilter: ColorFilter.mode( QuotidienTheme.coral.withValues(alpha: 0.3), BlendMode.srcATop, ), ), ), padding: const EdgeInsets.symmetric(horizontal: 24), child: Row( children: [ const Icon(Icons.error_outline, color: QuotidienTheme.ink), const SizedBox(width: 12), Expanded( child: Text( message, style: GoogleFonts.merriweather( fontSize: 13, color: QuotidienTheme.ink, ), ), ), if (onRetry != null) TextButton( onPressed: onRetry, child: const Text('Réessayer'), ), ], ), ); } }