import 'package:chat/configs/app_colors.dart';
import 'package:chat/routes/contact_routes.dart';
import 'package:chat/routes/user_routes.dart';
import 'package:chat/services/auth_service.dart';
import 'package:chat/views/conversation/widgets/show_custom_message.dart';
import 'package:chat/views/conversation/widgets/show_face_message.dart';
import 'package:chat/views/conversation/widgets/show_file_message.dart';
import 'package:chat/views/conversation/widgets/show_image_message.dart';
import 'package:chat/views/conversation/widgets/show_location_message.dart';
import 'package:chat/views/conversation/widgets/show_merger_message.dart';
import 'package:chat/views/conversation/widgets/show_sound_message.dart';
import 'package:chat/views/conversation/widgets/show_text_message.dart';
import 'package:chat/views/conversation/widgets/show_video_message.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/group_change_info_type.dart';
import 'package:tencent_im_sdk_plugin/enum/group_tips_elem_type.dart';
import 'package:tencent_im_sdk_plugin/enum/message_elem_type.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';

class MessageWidget extends StatelessWidget {
  final V2TimMessage message;

  const MessageWidget(this.message, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    switch (message.elemType) {
      case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
        return _buildMessage(ShowTextMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
        return _buildMessage(ShowImageMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_SOUND:
        return _buildMessage(ShowSoundMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_VIDEO:
        return _buildMessage(ShowVideoMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_FILE:
        return _buildMessage(ShowFileMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
        return _buildMessage(ShowLocationMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_FACE:
        return _buildMessage(ShowFaceMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_MERGER:
        return _buildMessage(ShowMergerMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_CUSTOM:
        return _buildMessage(ShowCustomMessage(message));
      case MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS:
        return _buildGroupTipMessage();
      default:
        return Text('未识别的消息类型 ${message.elemType}');
    }
  }

  /// 构造群提示消息
  Widget _buildGroupTipMessage() {
    switch (message.groupTipsElem!.type) {
      case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_GROUP_INFO_CHANGE:
        return _buildGroupInfoChangeMessage();
      case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_JOIN:
        return _buildGroupTypeJoinMessage();
      case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_INVITE:
        return _buildGroupTypeInviteMessage();
      case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_QUIT:
        return _buildGroupTypeQuitMessage();
      case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_KICKED:
        return _buildGroupTypeKickedMessage();
      case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_SET_ADMIN:
        return _buildGroupTypeSetAdminMessage();
      case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_CANCEL_ADMIN:
        return _buildGroupTypeCancelAdminMessage();
      case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_MEMBER_INFO_CHANGE:
        return _buildGroupTypeMemberInfoChangeMessage();
      default:
        return Text(message.groupTipsElem!.type.toString());
    }
  }

  Widget _buildGroupTypeMemberInfoChangeMessage() {
    return Center(
      child: Text(
        '${message.groupTipsElem!.opMember.nickName}修改了群成员资料',
        style: const TextStyle(
          color: AppColors.unactive,
          fontSize: 12,
        ),
      ),
    );
  }

  Widget _buildGroupTypeCancelAdminMessage() {
    String op = message.groupTipsElem!.opMember.nickName ?? '';
    String text = '';
    for (var e in message.groupTipsElem!.memberList!) {
      text += e?.nickName ?? '\t';
    }
    text = '$op取消了$text的管理员身份';
    return Center(
      child: Text(
        text,
        style: const TextStyle(
          color: AppColors.unactive,
          fontSize: 12,
        ),
      ),
    );
  }

  Widget _buildGroupTypeSetAdminMessage() {
    String op = message.groupTipsElem!.opMember.nickName ?? '';
    String text = '';
    for (var e in message.groupTipsElem!.memberList!) {
      text += e?.nickName ?? '\t';
    }
    text = '$op将$text设置为管理员';
    return Center(
      child: Text(
        text,
        style: const TextStyle(
          color: AppColors.unactive,
          fontSize: 12,
        ),
      ),
    );
  }

  Widget _buildGroupTypeKickedMessage() {
    String op = message.groupTipsElem!.opMember.nickName ?? '';
    String text = '';
    for (var e in message.groupTipsElem!.memberList!) {
      text += e?.nickName ?? '\t';
    }
    text = '$op将$text踢出群组';
    return Center(
      child: Text(
        text,
        style: const TextStyle(
          color: AppColors.unactive,
          fontSize: 12,
        ),
      ),
    );
  }

  /// 退出群组的消息解析
  Widget _buildGroupTypeQuitMessage() {
    if (message.groupTipsElem!.memberList!.isEmpty) {
      return Container();
    }
    return Center(
      child: Text(
        '${message.groupTipsElem!.memberList!.first?.nickName}退出了群组',
        style: const TextStyle(
          color: AppColors.unactive,
          fontSize: 12,
        ),
      ),
    );
  }

  Widget _buildGroupTypeInviteMessage() {
    String op = message.groupTipsElem!.opMember.nickName ?? '';
    String text = '';
    for (var e in message.groupTipsElem!.memberList!) {
      text += e?.nickName ?? '\t';
    }
    text = '$op邀请$text加入群组';
    return Center(
      child: Text(
        text,
        style: const TextStyle(
          color: AppColors.unactive,
          fontSize: 12,
        ),
      ),
    );
  }

  Widget _buildGroupTypeJoinMessage() {
    String text = '';
    for (var e in message.groupTipsElem!.memberList!) {
      text += e?.nickName ?? '\t';
    }
    text = '$text加入群组';
    return Center(
      child: Text(
        text,
        style: const TextStyle(
          color: AppColors.unactive,
          fontSize: 12,
        ),
      ),
    );
  }

  /// 解析群资料修改的内容
  Widget _buildGroupInfoChangeMessage() {
    String text = '';
    switch (message.groupTipsElem!.groupChangeInfoList!.first!.type) {
      case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_NAME:
        text =
            '${message.groupTipsElem!.opMember.nickName} 修改群名称 ${message.groupTipsElem!.groupChangeInfoList!.first!.value}';
        break;
      case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_INTRODUCTION:
        text = '群简介修改';
        break;
      case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_NOTIFICATION:
        text = '${message.groupTipsElem!.opMember.nickName} 更新了群公告';
        break;
      case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_FACE_URL:
        text = '群头像修改';
        break;
      case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_OWNER:
        text = '${message.groupTipsElem!.memberList!.first!.nickName} 成为新群主';
        break;
      case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_CUSTOM:
        text = '群自定义字段变更';
        break;
      default:
        text = message.groupTipsElem!.groupChangeInfoList![0]!.type.toString();
        break;
    }

    return Center(
      child: Text(
        text,
        style: const TextStyle(
          color: AppColors.unactive,
          fontSize: 12,
        ),
      ),
    );
  }

  /// 构造普通内容的消息
  Widget _buildMessage(Widget child) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      textDirection: message.isSelf! ? TextDirection.rtl : TextDirection.ltr,
      children: [
        InkWell(
          onTap: () {
            if (message.isSelf!) {
              Get.toNamed(UserRoutes.info);
            } else {
              Get.toNamed(
                ContactRoutes.friendProfile,
              );
            }
          },
          child: message.isSelf!
              ? GetX<AuthService>(builder: (_) {
                  return CustomAvatar(
                    _.userInfo.value.avatar ?? '',
                    size: 43,
                    radius: 4,
                  );
                })
              : CustomAvatar(
                  message.faceUrl,
                  size: 43,
                  radius: 4,
                ),
        ),
        const SizedBox(width: 8),
        GestureDetector(
          onLongPress: () {},
          child: child,
        ),
      ],
    );
  }
}
