import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:taafee_mobile/common/const/const.dart';
import 'package:taafee_mobile/common/extensions/widget_extension.dart';
import 'package:taafee_mobile/common/widgets/text.dart';
import 'package:taafee_mobile/core/routing/routing_manager.dart';
import 'package:taafee_mobile/features/chat/business%20logic%20layer/chat_controller.dart';
import 'package:taafee_mobile/features/chat/data_layer/model/message.dart';
import 'package:taafee_mobile/features/chat/presentation_layer/widgets/circle_avatar.dart';

import '../../data_layer/model/room.dart';

class ChatWidget extends StatelessWidget {
  ChatWidget({
    super.key,
    required this.room,
    this.onBack,
  });
  final Room room;
  final void Function()? onBack;
  final ChatController chatController = Get.find<ChatController>();
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: Get.width,
      height: 83,
      child: Row(
        children: [
          CircleAvatarWidget(
            isUserAvatar: false,
            avatarImageLink: room.user?.avatar,
          ).expanded(Responsive.isTablet() ? 0 : 2),
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              BoldTextWidget(
                room.user?.name ?? ("support".tr),
                fontSize: 14,
                overflow: TextOverflow.ellipsis,
                color: AppColors.textColor,
              ),
              if (room.lastMessage != null &&
                  room.lastMessage!.type == MessageType.text)
                SizedBox(
                    height: 20,
                    child: RegularTextWidget(
                      room.lastMessage!.content,
                      overflow: TextOverflow.ellipsis,
                    )),
              if (room.lastMessage != null &&
                  room.lastMessage!.type == MessageType.voice)
                RegularTextWidget('voice'.tr),
              if (room.lastMessage != null &&
                  room.lastMessage!.type == MessageType.image)
                RegularTextWidget('photo'.tr),
            ],
          ).paddingSymmetric(vertical: 13, horizontal: 20).expanded(6),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              RegularTextWidget(
                  room.lastMessage?.createdAt.toString().substring(11, 16) ??
                      ""),
            ],
          ).paddingSymmetric(horizontal: 20).expanded(4),
        ],
      ),
    ).onTap(() {
      chatController.setCurrentRoom(room);
      RoutingManager.to(RouteName.chatDetails,
          arguments: {'room': room, 'onBack': onBack});
    });
  }
}
