import 'package:get/get.dart';
import 'package:loopin/IM/controller/im_user_info_controller.dart';
import 'package:loopin/IM/im_service.dart';
import 'package:loopin/components/my_toast.dart';
import 'package:tencent_cloud_chat_sdk/enum/group_member_filter_enum.dart';
import 'package:tencent_cloud_chat_sdk/enum/group_type.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_group_info.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_group_member_full_info.dart';

class GroupDetailController extends GetxController {
  // 群ID
  final String groupID;
  GroupDetailController({required this.groupID});
  // 群资料
  Rxn<V2TimGroupInfo> info = Rxn<V2TimGroupInfo>();
  // 群成员列表
  RxList<V2TimGroupMemberFullInfo> memberList = <V2TimGroupMemberFullInfo>[].obs;
  // 自己在群里的信息
  Rxn<V2TimGroupMemberFullInfo> selfInfo = Rxn<V2TimGroupMemberFullInfo>();
  // 是否是群主
  RxBool isOwner = false.obs;

  Future<void> init() async {
    await getSelfInfo();
    await getGroupData();
    await getMemberData(); // 最后在拉取成员信息
    canRemoveMembers();
  }

  // 修改群资料
  Future<void> setGroupInfo({required V2TimGroupInfo changedInfo}) async {
    if (isOwner.value && info.value != null) {
      final res = await ImService.instance.setGroupInfo(info: changedInfo);
      if (!res.success) {
        // 修改失败重新获取info
        MyToast().tip(title: '请稍后再试');
        getGroupData();
      } else {
        // info.value?.faceUrl = changedInfo.faceUrl;
        final current = info.value!;
        if (changedInfo.faceUrl != null) current.faceUrl = changedInfo.faceUrl;
        if (changedInfo.groupName != null) current.groupName = changedInfo.groupName;
        if (changedInfo.introduction != null) current.introduction = changedInfo.introduction;
        if (changedInfo.notification != null) current.notification = changedInfo.notification;
        info.refresh();
      }
    }
  }

  // 获取群资料
  Future<void> getGroupData() async {
    final res = await ImService.instance.getGroupsInfo(groupIDList: [groupID]);
    if (res.success && res.data != null) {
      info.value = res.data!.first.groupInfo ?? V2TimGroupInfo(groupID: groupID, groupType: GroupType.Work);
    }
  }

  //群成员(这里非群主拿14个，群住拿13个)
  Future<void> getMemberData() async {
    final res = await ImService.instance.getGroupMemberList(
      groupID: groupID,
      filter: GroupMemberFilterTypeEnum.V2TIM_GROUP_MEMBER_FILTER_ALL,
      nextSeq: "0",
      count: isOwner.value ? 13 : 14,
    );
    if (res.success && res.data != null) {
      memberList.value = res.data!.memberInfoList ?? [];
    }
  }

  // 自己的群资料
  Future<void> getSelfInfo() async {
    final selfID = Get.find<ImUserInfoController>().userID.value;
    final res = await ImService.instance.getGroupMembersInfo(
      groupID: groupID,
      memberList: [selfID],
    );
    if (res.success && res.data != null) {
      selfInfo.value = res.data!.first;
    }
  }

  // 设置自己的群资料
  Future<void> setSelfInfo({required String nameCard}) async {
    final res = await ImService.instance.setGroupMemberInfo(
      groupID: groupID,
      userID: selfInfo.value?.userID ?? '',
      nameCard: nameCard,
    );
    if (!res.success) {
      MyToast().tip(title: '设置失败', position: 'top');
    } else {
      selfInfo.value?.nameCard = nameCard;
      selfInfo.refresh();
    }
  }

  // 邀请进群 inviteUserToGroup
  Future<void> inviteUserToGroup({required List<String> userList}) async {
    final res = await ImService.instance.inviteUserToGroup(
      groupID: groupID,
      userList: userList,
    );
    if (res.success && res.data != null) {
      refresh();
    }
  }

  // 退出群聊
  Future<void> quitGroup() async {
    final res = await ImService.instance.quitGroup(
      groupID: groupID,
    );
    if (res.success) {
      refresh();
    }
  }

  // 踢人 inviteUserToGroup
  Future<void> kickGroupMember(List<String> userIDs) async {
    await ImService.instance.kickGroupMember(
      groupID: groupID,
      memberList: userIDs,
    );
  }

  // 踢人权限
  void canRemoveMembers() {
    final owner = info.value?.owner;
    final myID = selfInfo.value?.userID;
    final myRole = selfInfo.value?.role;

    if (owner == null || myID == null || myRole == null) {
      isOwner.value = false;
    } else if (owner == myID) {
      isOwner.value = true;
    } else if ([300, 400].contains(myRole)) {
      isOwner.value = true;
    } else {
      isOwner.value = false;
    }
    // role=200普通  300管理 400群主
    // if (owner == myID) result = true;
    // if ([300, 400].contains(myRole)) result = true;
    // isOwner
    // return false;
  }
}
