import 'package:flutter/material.dart'; import 'package:geosector_app/core/theme/app_theme.dart'; import 'package:geosector_app/chat/chat_module.dart'; import 'package:geosector_app/core/services/current_user_service.dart'; import 'package:geosector_app/core/services/api_service.dart'; import 'package:geosector_app/core/services/current_amicale_service.dart'; class UserCommunicationPage extends StatefulWidget { const UserCommunicationPage({super.key}); @override State createState() => _UserCommunicationPageState(); } class _UserCommunicationPageState extends State { bool _isChatInitialized = false; bool _isInitializing = false; String? _initError; @override void initState() { super.initState(); _initializeChat(); } Future _initializeChat() async { if (_isInitializing) return; setState(() { _isInitializing = true; _initError = null; }); try { // Récupérer les informations utilisateur final currentUser = CurrentUserService.instance; final apiService = ApiService.instance; final currentAmicale = CurrentAmicaleService.instance.currentAmicale; if (currentUser.currentUser == null) { throw Exception('Utilisateur non connecté'); } // Initialiser le module chat avec les informations de l'utilisateur await ChatModule.init( apiUrl: apiService.baseUrl, userId: currentUser.currentUser!.id, userName: currentUser.userName ?? currentUser.userEmail ?? 'Utilisateur', userRole: currentUser.currentUser!.role, userEntite: currentUser.fkEntite ?? currentAmicale?.id, authToken: currentUser.sessionId, ); setState(() { _isChatInitialized = true; _isInitializing = false; }); } catch (e) { setState(() { _initError = e.toString(); _isInitializing = false; }); debugPrint('Erreur initialisation chat: $e'); } } @override Widget build(BuildContext context) { final theme = Theme.of(context); return Scaffold( backgroundColor: Colors.transparent, body: Container( margin: const EdgeInsets.all(16.0), decoration: BoxDecoration( color: theme.colorScheme.surface, borderRadius: BorderRadius.circular(24), boxShadow: [ BoxShadow( color: theme.shadowColor.withOpacity(0.1), blurRadius: 20, spreadRadius: 1, offset: const Offset(0, 4), ), ], ), child: ClipRRect( borderRadius: BorderRadius.circular(24), child: _buildContent(theme), ), ), ); } Widget _buildContent(ThemeData theme) { if (_isInitializing) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const CircularProgressIndicator(), const SizedBox(height: 16), Text( 'Initialisation du chat...', style: theme.textTheme.bodyLarge, ), ], ), ); } if (_initError != null) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.error_outline, size: 64, color: theme.colorScheme.error, ), const SizedBox(height: 16), Text( 'Erreur d\'initialisation', style: theme.textTheme.headlineSmall?.copyWith( color: theme.colorScheme.error, ), ), const SizedBox(height: 8), Text( _initError!, style: theme.textTheme.bodyMedium, textAlign: TextAlign.center, ), const SizedBox(height: 24), ElevatedButton.icon( onPressed: _initializeChat, icon: const Icon(Icons.refresh), label: const Text('Réessayer'), ), ], ), ); } if (_isChatInitialized) { // Afficher directement le module chat return ChatModule.getRoomsPage(); } // État initial return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.chat_bubble_outline, size: 80, color: theme.colorScheme.primary.withOpacity(0.3), ), const SizedBox(height: 24), Text( 'Chat non initialisé', style: theme.textTheme.titleLarge?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.5), ), ), const SizedBox(height: 16), ElevatedButton( onPressed: _initializeChat, child: const Text('Initialiser le chat'), ), ], ), ); } @override void dispose() { // Ne pas disposer le chat ici car il est partagé super.dispose(); } }