import 'dart:convert';

import 'package:loopin/models/summary_type.dart';
import 'package:tencent_cloud_chat_sdk/enum/message_elem_type.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_message.dart';

String parseMessageSummary(V2TimMessage msg) {
  switch (msg.elemType) {
    case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
      return msg.textElem?.text ?? '[文本消息]';
    case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
      return '[图片]';
    case MessageElemType.V2TIM_ELEM_TYPE_SOUND:
      return '[语音]';
    case MessageElemType.V2TIM_ELEM_TYPE_VIDEO:
      return '[视频]';
    case MessageElemType.V2TIM_ELEM_TYPE_FILE:
      return '[文件]'; // 先不做
    case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
      return '[位置]'; // 先不做
    case MessageElemType.V2TIM_ELEM_TYPE_FACE:
      return '[表情]';
    case MessageElemType.V2TIM_ELEM_TYPE_CUSTOM:
      return _parseCustomMessage(msg);
    case MessageElemType.V2TIM_ELEM_TYPE_MERGER:
      return '[合并转发消息]';
    case MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS:
      return '[群提示]';
    default:
      return '[未知消息类型1]';
  }
}

String _parseCustomMessage(V2TimMessage? msg) {
  if (msg == null) return '[null]';
  final sum = msg.cloudCustomData;
  try {
    switch (sum) {
      case SummaryType.hongbao:
        final hbData = jsonDecode(msg.customElem!.data!);
        return '[红包]${hbData['remark']}';
      case SummaryType.shareTuangou:
        return '[分享商品]';
      case SummaryType.shareVideo:
        return '[分享视频]';
      default:
        return '[未知消息类型2]';
    }
  } catch (_) {
    return '[未知消息类型3]';
  }
}
