import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/features/chat/business%20logic%20layer/chat_controller.dart';
import 'package:taafee_mobile/features/chat/data_layer/model/room.dart';
import 'package:taafee_mobile/features/chat/presentation_layer/widgets/recieved_message_widget.dart';
import 'package:taafee_mobile/features/chat/presentation_layer/widgets/replied_message.dart';
import 'package:taafee_mobile/features/chat/presentation_layer/widgets/sent_message_widget.dart';
import 'package:taafee_mobile/features/home/business_logic_layer/home_controller.dart';
import '../../data_layer/model/message.dart';
import 'image_message.dart';
import 'new_voice_message.dart';

class MessageWidget extends StatelessWidget {
  MessageWidget(
      {super.key,
      required this.messageModel,
      required this.textFieldFocusNode});
  final MessageModel messageModel;
  final FocusNode textFieldFocusNode;
  final ChatController chatController = Get.find<ChatController>();
  final HomeController homeController = Get.find<HomeController>();

  @override
  Widget build(BuildContext context) {
    messageModel.userId ??= messageModel.user.id;

    if (messageModel.userId != chatController.chatUser.id) {
      messageModel.direction = MessageDirection.received;
    }
    if (messageModel.direction == MessageDirection.received) {
      if ((messageModel.readBy?.isReaded() != null) &&
          (!messageModel.readBy!.isReaded())) {
        chatController.readMessage(messageId: messageModel.id!);
      }
      return Align(
        alignment: Alignment.centerLeft,
        child: Builder(builder: (context) {
          if (messageModel.repliedMessage != null) {
            return RepliedMessage(
                messageModel: messageModel,
                repliedMessageModel: messageModel.repliedMessage!,
                textFieldFocusNode: textFieldFocusNode);
          }
          if (messageModel.type == MessageType.text) {
            return RecievedMessageWidget(
                textFieldFocusNode: textFieldFocusNode,
                messageModel: messageModel);
          } else if (messageModel.type == MessageType.image) {
            return ImageMessage(
                textFieldFocusNode: textFieldFocusNode,
                messageModel: messageModel);
          } else {
            return NewVoiceMessage(
              messageModel: messageModel,
              //key: Key(Utils.randomString()),
              textFieldFocusNode: textFieldFocusNode,
            ).align(alignment: Alignment.centerLeft);
          }
        }),
      );
    } else {
      return Stack(
        children: [
          Align(
            alignment: Alignment.centerRight,
            child: Builder(builder: (context) {
              if (messageModel.repliedMessage != null) {
                return RepliedMessage(
                    messageModel: messageModel,
                    repliedMessageModel: messageModel.repliedMessage!,
                    textFieldFocusNode: textFieldFocusNode);
              }
              if (messageModel.type == MessageType.text) {
                return SentMessageWidget(
                  messageModel: messageModel,
                  textFieldFocusNode: textFieldFocusNode,
                );
              } else if (messageModel.type == MessageType.image) {
                return ImageMessage(
                    textFieldFocusNode: textFieldFocusNode,
                    messageModel: messageModel);
              } else {
                return NewVoiceMessage(
                  //key: Key(Utils.randomString()),
                  messageModel: messageModel,
                  textFieldFocusNode: textFieldFocusNode,
                );
              }
            }),
          ),
          if (messageModel.sendingStateId != null ||
              (messageModel.sendingStateId != null &&
                  messageModel.type != MessageType.text &&
                  chatController.uploadFileState.loading))
            Align(
              alignment: Alignment.centerRight,
              child: const SizedBox(
                width: 12,
                height: 12,
                child: CircularProgressIndicator(
                  color: Colors.white,
                ),
              ).paddingOnly(top: 12, right: 20),
            ),
          if ((messageModel.sendingStateId == null) &&
              (!messageModel.readBy!.isReaded()) &&
              ((messageModel.sendingStateId == null)))
            Align(
              alignment: Alignment.centerRight,
              child: Icon(
                Icons.done,
                color: messageModel.type == MessageType.image
                    ? Colors.grey
                    : Colors.white,
                size: 15,
              ).paddingOnly(
                top: 4,
                right: 16,
              ),
            ),
          if ((chatController.currentRoom.value!.type != RoomType.support) &&
                  messageModel.sendingStateId == null &&
                  (messageModel.readBy != null)
              ? messageModel.readBy!.isReaded()
              : false)
            Align(
              alignment: Alignment.centerRight,
              child: Icon(
                Icons.done_all,
                color: messageModel.type == MessageType.image
                    ? Colors.grey
                    : Colors.white,
                size: 15,
              ).paddingOnly(
                top: 4,
                right: 16,
              ),
            ),
        ],
      ).paddingSymmetric(
          vertical: (messageModel.type == MessageType.text) ? 4 : 0);
    }
  }
}
