import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; import 'package:geolocator/geolocator.dart'; import '../providers/messages_provider.dart'; import '../providers/contacts_provider.dart'; import '../providers/map_provider.dart'; import '../providers/connection_provider.dart'; import '../models/message.dart'; import '../models/sar_marker.dart'; class MessagesTab extends StatefulWidget { final VoidCallback onNavigateToMap; const MessagesTab({super.key, required this.onNavigateToMap}); @override State createState() => _MessagesTabState(); } class _MessagesTabState extends State { final TextEditingController _textController = TextEditingController(); final FocusNode _focusNode = FocusNode(); int _characterCount = 0; static const int _maxCharacters = 160; @override void initState() { super.initState(); _textController.addListener(_updateCharacterCount); } @override void dispose() { _textController.dispose(); _focusNode.dispose(); super.dispose(); } void _updateCharacterCount() { setState(() { _characterCount = _textController.text.length; }); } Future _sendMessage() async { final text = _textController.text.trim(); if (text.isEmpty) return; final connectionProvider = context.read(); final contactsProvider = context.read(); if (!connectionProvider.deviceInfo.isConnected) { if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Not connected to device'), backgroundColor: Colors.red, ), ); return; } try { // Default to sending to room/channel (first available room) final rooms = contactsProvider.rooms; if (rooms.isNotEmpty) { // Send to first available room final defaultRoom = rooms.first; final channelIdx = defaultRoom.outPath.isNotEmpty ? defaultRoom.outPath[0] : 0; await connectionProvider.sendChannelMessage( channelIdx: channelIdx, text: text, ); } else { // No rooms available, show error if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('No channels available'), backgroundColor: Colors.orange, ), ); return; } _textController.clear(); _focusNode.unfocus(); if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Message sent'), backgroundColor: Colors.green, duration: Duration(seconds: 1), ), ); } catch (e) { if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Failed to send: $e'), backgroundColor: Colors.red, ), ); } } void _showSarDialog() { showModalBottomSheet( context: context, isScrollControlled: true, backgroundColor: Colors.transparent, builder: (context) => _SarUpdateSheet( onSend: (sarType, position, notes) async { await _sendSarMessage(sarType, position, notes); }, ), ); } Future _sendSarMessage( SarMarkerType sarType, Position position, String? notes, ) async { final connectionProvider = context.read(); final contactsProvider = context.read(); if (!connectionProvider.deviceInfo.isConnected) { if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Not connected to device'), backgroundColor: Colors.red, ), ); return; } try { // Format: S::, final sarMessage = 'S:${sarType.emoji}:${position.latitude},${position.longitude}'; // Add notes if provided final fullMessage = notes != null && notes.isNotEmpty ? '$sarMessage $notes' : sarMessage; // Default to sending to room/channel (first available room) final rooms = contactsProvider.rooms; if (rooms.isNotEmpty) { // Send to first available room final defaultRoom = rooms.first; final channelIdx = defaultRoom.outPath.isNotEmpty ? defaultRoom.outPath[0] : 0; await connectionProvider.sendChannelMessage( channelIdx: channelIdx, text: fullMessage, ); } else { // No rooms available, show error if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('No channels available'), backgroundColor: Colors.orange, ), ); return; } if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('${sarType.displayName} marker sent'), backgroundColor: Colors.green, duration: const Duration(seconds: 2), ), ); } catch (e) { if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Failed to send SAR marker: $e'), backgroundColor: Colors.red, ), ); } } @override Widget build(BuildContext context) { return Consumer( builder: (context, messagesProvider, child) { final messages = messagesProvider.getRecentMessages(count: 100); return Column( children: [ // Messages list Expanded( child: messages.isEmpty ? Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon( Icons.message_outlined, size: 64, color: Theme.of(context).disabledColor, ), const SizedBox(height: 16), Text( 'No messages yet', style: Theme.of(context).textTheme.titleLarge, ), const SizedBox(height: 8), Text( 'Connect to a device to start receiving messages', style: Theme.of(context).textTheme.bodyMedium, textAlign: TextAlign.center, ), ], ), ) : ListView.builder( reverse: true, padding: const EdgeInsets.all(8), itemCount: messages.length, itemBuilder: (context, index) { final message = messages[index]; return _MessageBubble( message: message, onTap: message.isSarMarker && message.sarGpsCoordinates != null ? () { final mapProvider = context.read(); mapProvider.navigateToLocation( location: message.sarGpsCoordinates!, zoom: 15.0, ); widget.onNavigateToMap(); } : null, ); }, ), ), // Message input area Container( decoration: BoxDecoration( color: Theme.of(context).colorScheme.surface, border: Border( top: BorderSide( color: Theme.of(context).dividerColor, width: 1, ), ), ), padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8), child: Row( crossAxisAlignment: CrossAxisAlignment.end, children: [ // SAR quick action button IconButton( icon: const Icon(Icons.add_location_alt), tooltip: 'Send SAR marker', onPressed: _showSarDialog, style: IconButton.styleFrom( backgroundColor: Theme.of(context).colorScheme.primaryContainer, foregroundColor: Theme.of(context).colorScheme.onPrimaryContainer, ), ), const SizedBox(width: 8), // Text field with embedded send button Expanded( child: TextField( controller: _textController, focusNode: _focusNode, maxLength: _maxCharacters, maxLines: null, maxLengthEnforcement: MaxLengthEnforcement.enforced, style: const TextStyle(fontSize: 14), decoration: InputDecoration( hintText: 'Message to channel...', hintStyle: const TextStyle(fontSize: 14), border: OutlineInputBorder( borderRadius: BorderRadius.circular(24), ), contentPadding: const EdgeInsets.symmetric( horizontal: 16, vertical: 10, ), isDense: true, counterText: _characterCount >= 150 ? '$_characterCount/$_maxCharacters' : '', counterStyle: TextStyle( fontSize: 10, color: _characterCount > _maxCharacters * 0.9 ? Colors.orange : Theme.of(context).textTheme.bodySmall?.color, ), suffixIcon: IconButton( icon: Icon( Icons.send_rounded, size: 22, color: _textController.text.trim().isEmpty ? Theme.of(context).disabledColor : Theme.of(context).colorScheme.primary, ), onPressed: _textController.text.trim().isEmpty ? null : _sendMessage, tooltip: 'Send', ), ), textInputAction: TextInputAction.send, onSubmitted: (_) => _sendMessage(), ), ), ], ), ), ], ); }, ); } } class _MessageBubble extends StatelessWidget { final Message message; final VoidCallback? onTap; const _MessageBubble({ required this.message, this.onTap, }); @override Widget build(BuildContext context) { final isSarMarker = message.isSarMarker; return GestureDetector( onTap: onTap, child: Container( margin: const EdgeInsets.only(bottom: 8), padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: isSarMarker ? _getSarMarkerColor(context) : Theme.of(context).colorScheme.surfaceVariant, borderRadius: BorderRadius.circular(12), border: isSarMarker ? Border.all( color: Theme.of(context).colorScheme.primary, width: 2, ) : null, ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Header: Sender and time Row( children: [ if (message.isChannelMessage) const Icon(Icons.tag, size: 16) else const Icon(Icons.person, size: 16), const SizedBox(width: 4), Text( message.displaySender, style: Theme.of(context).textTheme.labelMedium?.copyWith( fontWeight: FontWeight.bold, ), ), const Spacer(), if (isSarMarker) Container( padding: const EdgeInsets.symmetric( horizontal: 6, vertical: 2, ), decoration: BoxDecoration( color: Theme.of(context).colorScheme.primary, borderRadius: BorderRadius.circular(4), ), child: Text( 'SAR', style: Theme.of(context).textTheme.labelSmall?.copyWith( color: Theme.of(context).colorScheme.onPrimary, fontWeight: FontWeight.bold, ), ), ), const SizedBox(width: 8), Text( message.timeAgo, style: Theme.of(context).textTheme.labelSmall, ), ], ), const SizedBox(height: 8), // SAR marker content if (isSarMarker && message.sarMarkerType != null) ...[ Row( children: [ Text( message.sarMarkerType!.emoji, style: const TextStyle(fontSize: 32), ), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( message.sarMarkerType!.displayName, style: Theme.of(context).textTheme.titleMedium?.copyWith( fontWeight: FontWeight.bold, ), ), if (message.sarGpsCoordinates != null) Text( '${message.sarGpsCoordinates!.latitude.toStringAsFixed(5)}, ${message.sarGpsCoordinates!.longitude.toStringAsFixed(5)}', style: Theme.of(context).textTheme.bodySmall, ), ], ), ), Icon( Icons.chevron_right, color: Theme.of(context).colorScheme.primary, ), ], ), const SizedBox(height: 4), Text( 'Tap to view on map', style: Theme.of(context).textTheme.labelSmall?.copyWith( color: Theme.of(context).colorScheme.primary, fontStyle: FontStyle.italic, ), ), ] // Regular message content else Text( message.text, style: Theme.of(context).textTheme.bodyMedium, ), ], ), ), ); } Color _getSarMarkerColor(BuildContext context) { return Theme.of(context).colorScheme.primaryContainer; } } // SAR Update Sheet class _SarUpdateSheet extends StatefulWidget { final Future Function(SarMarkerType, Position, String?) onSend; const _SarUpdateSheet({required this.onSend}); @override State<_SarUpdateSheet> createState() => _SarUpdateSheetState(); } class _SarUpdateSheetState extends State<_SarUpdateSheet> { SarMarkerType _selectedType = SarMarkerType.foundPerson; Position? _currentPosition; bool _loadingLocation = false; String? _locationError; final TextEditingController _notesController = TextEditingController(); @override void initState() { super.initState(); _getCurrentLocation(); } @override void dispose() { _notesController.dispose(); super.dispose(); } Future _getCurrentLocation() async { setState(() { _loadingLocation = true; _locationError = null; }); try { // Check if location services are enabled bool serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { setState(() { _locationError = 'Location services are disabled'; _loadingLocation = false; }); return; } // Check permissions LocationPermission permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { setState(() { _locationError = 'Location permission denied'; _loadingLocation = false; }); return; } } if (permission == LocationPermission.deniedForever) { setState(() { _locationError = 'Location permission permanently denied'; _loadingLocation = false; }); return; } // Get current position final position = await Geolocator.getCurrentPosition( locationSettings: const LocationSettings( accuracy: LocationAccuracy.best, distanceFilter: 0, ), ); if (mounted) { setState(() { _currentPosition = position; _loadingLocation = false; }); } } catch (e) { if (mounted) { setState(() { _locationError = 'Failed to get location: $e'; _loadingLocation = false; }); } } } @override Widget build(BuildContext context) { return Container( decoration: BoxDecoration( color: Theme.of(context).colorScheme.surface, borderRadius: const BorderRadius.vertical(top: Radius.circular(20)), ), padding: EdgeInsets.only( bottom: MediaQuery.of(context).viewInsets.bottom, ), child: Column( mainAxisSize: MainAxisSize.min, children: [ // Header with drag handle Container( padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 20), child: Column( children: [ // Drag handle Container( width: 40, height: 4, decoration: BoxDecoration( color: Theme.of(context).colorScheme.onSurfaceVariant.withValues(alpha: 0.4), borderRadius: BorderRadius.circular(2), ), ), const SizedBox(height: 16), // Title Row( children: [ Icon( Icons.add_location_alt, size: 24, color: Theme.of(context).colorScheme.primary, ), const SizedBox(width: 12), Expanded( child: Text( 'Send SAR Marker', style: Theme.of(context).textTheme.titleLarge?.copyWith( fontWeight: FontWeight.bold, ), ), ), IconButton( icon: const Icon(Icons.close), onPressed: () => Navigator.pop(context), ), ], ), ], ), ), const Divider(height: 1), // Content Flexible( child: SingleChildScrollView( padding: const EdgeInsets.all(20), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ // Marker type selection Text( 'Marker Type', style: Theme.of(context).textTheme.titleSmall?.copyWith( fontWeight: FontWeight.bold, ), ), const SizedBox(height: 12), _MarkerTypeChip( type: SarMarkerType.foundPerson, isSelected: _selectedType == SarMarkerType.foundPerson, onTap: () => setState(() => _selectedType = SarMarkerType.foundPerson), ), const SizedBox(height: 8), _MarkerTypeChip( type: SarMarkerType.fire, isSelected: _selectedType == SarMarkerType.fire, onTap: () => setState(() => _selectedType = SarMarkerType.fire), ), const SizedBox(height: 8), _MarkerTypeChip( type: SarMarkerType.stagingArea, isSelected: _selectedType == SarMarkerType.stagingArea, onTap: () => setState(() => _selectedType = SarMarkerType.stagingArea), ), const SizedBox(height: 8), _MarkerTypeChip( type: SarMarkerType.object, isSelected: _selectedType == SarMarkerType.object, onTap: () => setState(() => _selectedType = SarMarkerType.object), ), const SizedBox(height: 24), // Location display Text( 'Current Location', style: Theme.of(context).textTheme.titleSmall?.copyWith( fontWeight: FontWeight.bold, ), ), const SizedBox(height: 12), if (_loadingLocation) Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Theme.of(context).colorScheme.surfaceContainerHighest, borderRadius: BorderRadius.circular(12), ), child: const Row( children: [ SizedBox( width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2), ), SizedBox(width: 16), Text('Getting location...'), ], ), ) else if (_locationError != null) Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.red.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(12), border: Border.all( color: Colors.red.withValues(alpha: 0.3), width: 1, ), ), child: Row( children: [ const Icon(Icons.error_outline, color: Colors.red, size: 24), const SizedBox(width: 12), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'Location Error', style: TextStyle( color: Colors.red, fontWeight: FontWeight.bold, fontSize: 13, ), ), const SizedBox(height: 4), Text( _locationError!, style: TextStyle( color: Colors.red.shade700, fontSize: 12, ), ), ], ), ), IconButton( icon: const Icon(Icons.refresh, color: Colors.red), onPressed: _getCurrentLocation, tooltip: 'Retry', ), ], ), ) else if (_currentPosition != null) Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Theme.of(context).colorScheme.primaryContainer.withValues(alpha: 0.3), borderRadius: BorderRadius.circular(12), border: Border.all( color: Theme.of(context).colorScheme.primary.withValues(alpha: 0.3), width: 1, ), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Icon( Icons.location_on, size: 20, color: Theme.of(context).colorScheme.primary, ), const SizedBox(width: 8), Expanded( child: Text( '${_currentPosition!.latitude.toStringAsFixed(5)}, ${_currentPosition!.longitude.toStringAsFixed(5)}', style: const TextStyle( fontFamily: 'monospace', fontSize: 13, fontWeight: FontWeight.w500, ), ), ), IconButton( icon: const Icon(Icons.refresh, size: 20), onPressed: _getCurrentLocation, padding: EdgeInsets.zero, constraints: const BoxConstraints(), tooltip: 'Refresh location', ), ], ), if (_currentPosition!.accuracy != null) ...[ const SizedBox(height: 8), Row( children: [ Icon( Icons.my_location, size: 14, color: Theme.of(context).colorScheme.onSurfaceVariant, ), const SizedBox(width: 6), Text( 'Accuracy: ±${_currentPosition!.accuracy!.round()}m', style: Theme.of(context).textTheme.bodySmall, ), ], ), ], ], ), ), const SizedBox(height: 24), // Optional notes Text( 'Notes (optional)', style: Theme.of(context).textTheme.titleSmall?.copyWith( fontWeight: FontWeight.bold, ), ), const SizedBox(height: 12), TextField( controller: _notesController, maxLines: 3, maxLength: 100, decoration: InputDecoration( hintText: 'Add additional information...', hintStyle: const TextStyle(fontSize: 14), border: OutlineInputBorder( borderRadius: BorderRadius.circular(12), ), contentPadding: const EdgeInsets.all(16), ), style: const TextStyle(fontSize: 14), ), const SizedBox(height: 8), ], ), ), ), // Bottom action buttons Container( padding: const EdgeInsets.all(20), decoration: BoxDecoration( color: Theme.of(context).colorScheme.surface, border: Border( top: BorderSide( color: Theme.of(context).dividerColor, width: 1, ), ), ), child: SafeArea( top: false, child: Row( children: [ Expanded( child: OutlinedButton( onPressed: () => Navigator.pop(context), style: OutlinedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 14), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), child: const Text('Cancel'), ), ), const SizedBox(width: 12), Expanded( flex: 2, child: FilledButton.icon( onPressed: _currentPosition == null ? null : () async { await widget.onSend( _selectedType, _currentPosition!, _notesController.text.trim().isEmpty ? null : _notesController.text.trim(), ); if (context.mounted) { Navigator.pop(context); } }, style: FilledButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 14), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), icon: const Icon(Icons.send, size: 20), label: const Text('Send SAR Marker'), ), ), ], ), ), ), ], ), ); } } // Marker Type Chip widget class _MarkerTypeChip extends StatelessWidget { final SarMarkerType type; final bool isSelected; final VoidCallback onTap; const _MarkerTypeChip({ required this.type, required this.isSelected, required this.onTap, }); Color _getMarkerColor() { switch (type) { case SarMarkerType.foundPerson: return Colors.green; case SarMarkerType.fire: return Colors.red; case SarMarkerType.stagingArea: return Colors.orange; case SarMarkerType.object: return Colors.purple; case SarMarkerType.unknown: return Colors.grey; } } @override Widget build(BuildContext context) { final color = _getMarkerColor(); return InkWell( onTap: onTap, borderRadius: BorderRadius.circular(12), child: Container( width: double.infinity, padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: isSelected ? color.withValues(alpha: 0.15) : Colors.transparent, border: Border.all( color: isSelected ? color : Colors.grey.withValues(alpha: 0.3), width: isSelected ? 2 : 1, ), borderRadius: BorderRadius.circular(12), ), child: Row( children: [ Container( width: 48, height: 48, decoration: BoxDecoration( color: isSelected ? color.withValues(alpha: 0.2) : Colors.grey.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(8), ), alignment: Alignment.center, child: Text( type.emoji, style: const TextStyle(fontSize: 28), ), ), const SizedBox(width: 16), Expanded( child: Text( type.displayName, style: TextStyle( fontSize: 16, fontWeight: isSelected ? FontWeight.bold : FontWeight.w500, color: isSelected ? color : null, ), ), ), if (isSelected) Icon( Icons.check_circle, color: color, size: 24, ) else Icon( Icons.radio_button_unchecked, color: Colors.grey.withValues(alpha: 0.4), size: 24, ), ], ), ), ); } }