import 'package:chat/configs/app_colors.dart';
import 'package:chat/routes/conversation_routes.dart';
import 'package:chat/services/tim/conversation_service.dart';
import 'package:chat/utils/convert.dart';
import 'package:chat/views/home/widgets/group_avatar.dart';
import 'package:chat/views/home/widgets/message_preview_widget.dart';
import 'package:chat/views/home/widgets/pop_menu_item.dart';
import 'package:chat/widgets/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/enum/conversation_type.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_conversation.dart';

class ConversationItem extends StatelessWidget {
  final V2TimConversation conversation;
  const ConversationItem(this.conversation, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 68,
      decoration: BoxDecoration(
        color: conversation.isPinned! ? AppColors.page : null,
      ),
      padding: const EdgeInsets.only(
        left: 16,
        right: 16,
        top: 12,
        bottom: 12,
      ),
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          Get.toNamed(
            ConversationRoutes.index,
            arguments: {
              'conversation': conversation,
            },
          );
        },
        onLongPress: () async {
          await _showLongPressMenu();
        },
        child: Row(
          children: [
            Stack(
              clipBehavior: Clip.none,
              children: [
                conversation.type == ConversationType.V2TIM_C2C
                    ? CustomAvatar(
                        conversation.faceUrl,
                      )
                    : GroupAvatar(conversation.groupID!),
                Visibility(
                  visible: conversation.recvOpt == 0 &&
                      conversation.unreadCount! > 0,
                  child: Positioned(
                    right: -5,
                    top: -5,
                    child: Container(
                      width: 18,
                      height: 18,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(18),
                        color: AppColors.red,
                      ),
                      alignment: Alignment.center,
                      child: Center(
                        child: Text(
                          conversation.unreadCount! > 99
                              ? '99+'
                              : conversation.unreadCount.toString(),
                          style: const TextStyle(
                            fontSize: 10,
                            color: AppColors.white,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
                Visibility(
                  visible: conversation.recvOpt == 1 &&
                      conversation.unreadCount! > 0,
                  child: const Positioned(
                    right: -3,
                    top: -3,
                    child: Icon(
                      Icons.circle_rounded,
                      color: AppColors.red,
                      size: 8,
                    ),
                  ),
                )
              ],
            ),
            const SizedBox(width: 16),
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    conversation.showName!,
                    style: const TextStyle(
                      fontSize: 16,
                    ),
                  ),
                  MessagePreviewWidget(conversation.lastMessage),
                ],
              ),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  conversation.lastMessage == null
                      ? ''
                      : Convert.timeFormat(
                          conversation.lastMessage!.timestamp!,
                          format: 'MM/dd HH:mm',
                        ),
                  style: const TextStyle(
                    fontSize: 12,
                    color: Colors.grey,
                  ),
                ),
                if (conversation.recvOpt == 1)
                  const Icon(
                    Icons.notifications_off_outlined,
                    size: 14,
                    color: AppColors.unactive,
                  ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Future<void> _showLongPressMenu() async {
    showModalBottomSheet(
      context: Get.context!,
      isScrollControlled: true,
      backgroundColor: AppColors.white,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(8)),
      ),
      builder: (context) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            PopMenuItem(
              conversation.isPinned! ? '取消置顶' : '聊天置顶',
              onTap: () {
                TimConversationService.to.setOnTop(conversation);
                Get.back();
              },
            ),
            const Divider(height: 0),
            PopMenuItem(
              conversation.recvOpt == 1 ? '取消免打扰' : '消息免打扰',
              onTap: () {
                TimConversationService.to.setReceiveOpt(conversation);
                Get.back();
              },
            ),
            const Divider(height: 0),
            PopMenuItem(
              '清空聊天记录',
              onTap: () {
                TimConversationService.to.clearHistoryMessage(conversation);
                Get.back();
              },
            ),
            const Divider(height: 0),
            PopMenuItem(
              '标为已读',
              onTap: () {
                TimConversationService.to.markAsRead(conversation);
                Get.back();
              },
            ),
            const Divider(height: 0),
            PopMenuItem(
              '删除该聊天',
              onTap: () {
                TimConversationService.to.delete(conversation);
                Get.back();
              },
            ),
            const Divider(height: 0.4),
            Container(
              color: AppColors.page,
              height: 8,
            ),
            const Divider(height: 0.4),
            PopMenuItem(
              '取消',
              onTap: () {
                Get.back();
              },
            ),
          ],
        );
      },
    );
  }
}
