import 'dart:convert';

import 'package:azlistview/azlistview.dart';
import 'package:chat/configs/app_colors.dart';
import 'package:chat/models/im/custom_message_model.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_friend_info.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_info_result.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';

class ImTools {
  static String parseNicknameFromResult(V2TimFriendInfoResult? infoResult) {
    if (infoResult == null) {
      return '';
    }

    if (infoResult.friendInfo!.friendRemark != null) {
      if (infoResult.friendInfo!.friendRemark! == '') {
        return infoResult.friendInfo!.userProfile!.nickName!;
      } else {
        return infoResult.friendInfo!.friendRemark!;
      }
    }

    return infoResult.friendInfo!.userProfile!.nickName!;
  }

  static String parseNicknameFromInfo(V2TimFriendInfo infoResult) {
    if (infoResult.friendRemark != null &&
        infoResult.friendRemark!.isNotEmpty) {
      return infoResult.friendRemark ?? '';
    }

    return infoResult.userProfile!.nickName!;
  }

  static parseMessage(V2TimMessage? message) {
    String text = '';
    switch (message?.elemType) {
      case MessageElemType.V2TIM_ELEM_TYPE_TEXT:
        text = message!.textElem!.text!;
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_IMAGE:
        text = '[图片]';
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_SOUND:
        text = '[语音]';
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_VIDEO:
        text = '[视频]';
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_FILE:
        text = '[文件]';
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_LOCATION:
        text = '[位置]';
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_FACE:
        text = '[表情]';
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_MERGER:
        text = '[聊天记录]';
        break;
      case MessageElemType.V2TIM_ELEM_TYPE_CUSTOM:
        var data = json.decode(message!.customElem!.data!);

        switch (data['businessID']) {
          case CustomMessageType.CALL:
            text = '通话';
            break;
          case CustomMessageType.DT_TRANSFER:
            text = '[转账]';
            break;
          case CustomMessageType.NAME_CARD:
            text = '[名片]';
            break;
          case CustomMessageType.GROUP_CARD:
            text = '[群名片]';
            break;
          case CustomMessageType.EVALUATION:
            text = '[评价]';
            break;
          default:
            text = '[${data['businessID']}]';
        }

        break;
      case MessageElemType.V2TIM_ELEM_TYPE_GROUP_TIPS:
        switch (message?.groupTipsElem!.type) {
          case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_JOIN:
            text = '加入群聊';
            break;
          case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_INVITE:
            text = '邀请入群';
            break;
          case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_QUIT:
            text = '退出群聊';
            break;
          case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_KICKED:
            text = '踢出群聊';
            break;
          case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_SET_ADMIN:
            text = '设置管理';
            break;
          case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_CANCEL_ADMIN:
            text = '取消管理';
            break;
          case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_GROUP_INFO_CHANGE:
            //  (opMember 修改群资料：groupName & introduction & notification & faceUrl & owner & custom)
            switch (message!.groupTipsElem!.groupChangeInfoList![0]!.type) {
              case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_NAME:
                text = '修改群名称';
                break;
              case GroupChangeInfoType
                  .V2TIM_GROUP_INFO_CHANGE_TYPE_INTRODUCTION:
                text = '群简介修改';
                break;
              case GroupChangeInfoType
                  .V2TIM_GROUP_INFO_CHANGE_TYPE_NOTIFICATION:
                text = '修改群公告';
                break;
              case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_FACE_URL:
                text = '群头像修改';
                break;
              case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_OWNER:
                text = '群主变更';
                break;
              case GroupChangeInfoType.V2TIM_GROUP_INFO_CHANGE_TYPE_CUSTOM:
                text = '群自定义字段变更';
                break;
              default:
                text = message.groupTipsElem!.groupChangeInfoList![0]!.type
                    .toString();
                break;
            }
            break;
          case GroupTipsElemType.V2TIM_GROUP_TIPS_TYPE_MEMBER_INFO_CHANGE:
            text = '群成员资料变更';
            break;
          default:
            text = message!.groupTipsElem!.type.toString();
        }
        break;
      default:
        text = message!.elemType.toString();
    }

    return text;
  }

  /// 右侧索引栏样式
  static const IndexBarOptions indexBarOptions = IndexBarOptions(
    needRebuild: true,
    ignoreDragCancel: true,
    downTextStyle: TextStyle(
      fontSize: 12,
      color: Colors.white,
    ),
    downItemDecoration: BoxDecoration(
      shape: BoxShape.circle,
      color: AppColors.primary,
    ),
    indexHintWidth: 120 / 2,
    indexHintHeight: 100 / 2,
    indexHintDecoration: BoxDecoration(
      image: DecorationImage(
        image: AssetImage(
          'assets/chats/index_bar.png',
        ),
        fit: BoxFit.contain,
      ),
    ),
    indexHintAlignment: Alignment.centerRight,
    indexHintChildAlignment: Alignment(
      -0.25,
      0.0,
    ),
    indexHintOffset: Offset(
      -20,
      0,
    ),
  );

  /// 悬浮导航，显示ABCD~Z
  static Widget susItem(
    BuildContext context,
    String tag, {
    double susHeight = 32,
  }) {
    return Container(
      height: susHeight,
      width: MediaQuery.of(context).size.width,
      padding: const EdgeInsets.only(
        left: 16.0,
      ),
      color: AppColors.page,
      alignment: Alignment.centerLeft,
      child: Text(
        tag,
        style: const TextStyle(
          fontSize: 12,
          color: AppColors.unactive,
        ),
      ),
    );
  }

  static showTrtcMessage(String userID) {
    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(
            //   '语音通话',
            //   onTap: () {
            //     TimService.to.tuiCalling.call(userID, CallingScenes.Audio);
            //     Get.back();
            //   },
            // ),
            // const Divider(height: 0),
            // PopMenuItem(
            //   '视频通话',
            //   onTap: () {
            //     TimService.to.tuiCalling.call(userID, CallingScenes.Video);
            //     Get.back();
            //   },
            // ),
            const Divider(height: 0),
            Container(
              height: 8,
              color: AppColors.page,
            ),
            const Divider(height: 0),
            // PopMenuItem(
            //   '取消',
            //   onTap: () {
            //     Get.back();
            //   },
            // ),
          ],
        );
      },
    );
  }
}
