import 'package:flutter/material.dart';

/// Zone de saisie de message
///
/// Ce widget permet à l'utilisateur de saisir et envoyer des messages

class ChatInput extends StatefulWidget {
  final Function(String) onSendText;
  final Function(dynamic)? onSendFile;
  final Function(dynamic)? onSendImage;
  final bool enableAttachments;
  final bool enabled;
  final String hintText;
  final String? disabledMessage;
  final int? maxLength;

  const ChatInput({
    super.key,
    required this.onSendText,
    this.onSendFile,
    this.onSendImage,
    this.enableAttachments = true,
    this.enabled = true,
    this.hintText = 'Saisissez votre message...',
    this.disabledMessage = 'Vous ne pouvez pas répondre à cette annonce',
    this.maxLength,
  });

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

class _ChatInputState extends State<ChatInput> {
  final TextEditingController _textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    if (!widget.enabled) {
      return Container(
        padding: const EdgeInsets.all(8),
        color: Colors.grey.shade200,
        child: Text(
          widget.disabledMessage ?? '',
          textAlign: TextAlign.center,
          style: TextStyle(color: Colors.grey.shade600),
        ),
      );
    }

    return Container(
      padding: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(top: BorderSide(color: Colors.grey.shade300)),
      ),
      child: Row(
        children: [
          if (widget.enableAttachments)
            IconButton(
              icon: const Icon(Icons.attach_file),
              onPressed: () {
                // TODO: Gérer les pièces jointes
              },
            ),
          Expanded(
            child: TextField(
              controller: _textController,
              decoration: InputDecoration(
                hintText: widget.hintText,
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(20),
                ),
                contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              ),
              maxLength: widget.maxLength,
              maxLines: null,
            ),
          ),
          IconButton(
            icon: const Icon(Icons.send),
            onPressed: () {
              if (_textController.text.trim().isNotEmpty) {
                widget.onSendText(_textController.text.trim());
                _textController.clear();
              }
            },
          ),
        ],
      ),
    );
  }

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