import 'package:didvan/config/theme_data.dart';
import 'package:didvan/constants/app_icons.dart';
import 'package:didvan/models/chat_room/chat_room.dart';
import 'package:didvan/routes/routes.dart';
import 'package:didvan/utils/date_time.dart';
import 'package:didvan/views/widgets/didvan/badge.dart';
import 'package:didvan/views/widgets/didvan/divider.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';

class ChatRoomItem extends StatelessWidget {
  final ChatRoom chatRoom;
  const ChatRoomItem({Key? key, required this.chatRoom}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).pushNamed(
          Routes.direct,
          arguments: {'type': chatRoom.type},
        );
        chatRoom.unread = 0;
      },
      child: Container(
        color: Colors.transparent,
        child: Column(
          children: [
            Row(
              children: [
                const Icon(
                  DidvanIcons.avatar_light,
                  size: 32,
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: DidvanText(
                    chatRoom.type,
                    style: Theme.of(context).textTheme.bodyText1,
                  ),
                ),
                if (chatRoom.unread != 0)
                  DidvanBadge(text: chatRoom.unread.toString()),
              ],
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const SizedBox(width: 40),
                Icon(
                  chatRoom.lastMessage.readed
                      ? DidvanIcons.check_double_light
                      : DidvanIcons.check_light,
                  size: 16,
                ),
                const SizedBox(width: 4),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          if (chatRoom.lastMessage.text == null)
                            const Icon(
                              DidvanIcons.mic_light,
                              size: 18,
                            ),
                          Expanded(
                            child: DidvanText(
                              chatRoom.lastMessage.text ?? 'پیام صوتی',
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      ),
                      DidvanText(
                        DateTimeUtils.momentGenerator(chatRoom.updatedAt),
                        style: Theme.of(context).textTheme.caption,
                        color: Theme.of(context).colorScheme.caption,
                      )
                    ],
                  ),
                ),
              ],
            ),
            const DidvanDivider(),
          ],
        ),
      ),
    );
  }
}
