import 'package:flutter/material.dart';
import 'package:geosector_app/core/theme/app_theme.dart';

/// Widget pour la zone de saisie des messages
class ChatInput extends StatefulWidget {
  final Function(String) onMessageSent;

  const ChatInput({
    super.key,
    required this.onMessageSent,
  });

  @override
  State<ChatInput> createState() => _ChatInputState();
}

class _ChatInputState extends State<ChatInput> {
  final TextEditingController _controller = TextEditingController();
  bool _isComposing = false;

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(
        horizontal: AppTheme.spacingM,
        vertical: AppTheme.spacingS,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 5,
            offset: const Offset(0, -2),
          ),
        ],
      ),
      child: Row(
        children: [
          // Bouton pour ajouter des pièces jointes
          IconButton(
            icon: const Icon(Icons.attach_file),
            color: AppTheme.primaryColor,
            onPressed: () {
              // Afficher les options de pièces jointes
              _showAttachmentOptions(context);
            },
          ),

          // Champ de saisie du message
          Expanded(
            child: TextField(
              controller: _controller,
              decoration: InputDecoration(
                hintText: 'Écrivez votre message...',
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(24),
                  borderSide: BorderSide.none,
                ),
                filled: true,
                fillColor: Colors.grey[100],
                contentPadding: const EdgeInsets.symmetric(
                  horizontal: 16,
                  vertical: 8,
                ),
              ),
              textCapitalization: TextCapitalization.sentences,
              maxLines: null,
              keyboardType: TextInputType.multiline,
              textInputAction: TextInputAction.newline,
              onChanged: (text) {
                setState(() {
                  _isComposing = text.isNotEmpty;
                });
              },
            ),
          ),

          // Bouton d'envoi
          IconButton(
            icon: Icon(
              _isComposing ? Icons.send : Icons.mic,
              color: _isComposing ? AppTheme.primaryColor : Colors.grey[600],
            ),
            onPressed: _isComposing
                ? () {
                    final text = _controller.text.trim();
                    if (text.isNotEmpty) {
                      widget.onMessageSent(text);
                      _controller.clear();
                      setState(() {
                        _isComposing = false;
                      });
                    }
                  }
                : () {
                    // Activer la reconnaissance vocale
                  },
          ),
        ],
      ),
    );
  }

  // Afficher les options de pièces jointes
  void _showAttachmentOptions(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (context) => Container(
        padding: const EdgeInsets.symmetric(
          vertical: AppTheme.spacingL,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const Text(
              'Ajouter une pièce jointe',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 16,
              ),
            ),
            const SizedBox(height: AppTheme.spacingL),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                _buildAttachmentOption(
                  context,
                  Icons.photo,
                  'Photo',
                  Colors.green,
                  () {
                    Navigator.pop(context);
                    // Sélectionner une photo
                  },
                ),
                _buildAttachmentOption(
                  context,
                  Icons.camera_alt,
                  'Caméra',
                  Colors.blue,
                  () {
                    Navigator.pop(context);
                    // Prendre une photo
                  },
                ),
                _buildAttachmentOption(
                  context,
                  Icons.insert_drive_file,
                  'Document',
                  Colors.orange,
                  () {
                    Navigator.pop(context);
                    // Sélectionner un document
                  },
                ),
                _buildAttachmentOption(
                  context,
                  Icons.location_on,
                  'Position',
                  Colors.red,
                  () {
                    Navigator.pop(context);
                    // Partager la position
                  },
                ),
              ],
            ),
            const SizedBox(height: AppTheme.spacingL),
          ],
        ),
      ),
    );
  }

  // Construire une option de pièce jointe
  Widget _buildAttachmentOption(
    BuildContext context,
    IconData icon,
    String label,
    Color color,
    VoidCallback onTap,
  ) {
    return InkWell(
      onTap: onTap,
      child: Column(
        children: [
          Container(
            width: 56,
            height: 56,
            decoration: BoxDecoration(
              color: color.withOpacity(0.1),
              shape: BoxShape.circle,
            ),
            child: Icon(
              icon,
              color: color,
              size: 28,
            ),
          ),
          const SizedBox(height: 8),
          Text(
            label,
            style: TextStyle(
              color: Colors.grey[800],
              fontSize: 12,
            ),
          ),
        ],
      ),
    );
  }
}
