import 'package:chat/models/im/private_conversation_model.dart';
import 'package:chat/services/tim/conversation_service.dart';
import 'package:chat/services/tim/friend_service.dart';
import 'package:chat/services/tim_service.dart';
import 'package:chat/utils/ui_tools.dart';
import 'package:get/get.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_info_result.dart';

class PrivateController extends GetxController {
  static PrivateController get to => Get.find<PrivateController>();

  Rx<PrivateConversationModel> currentFriend =
      PrivateConversationModel(userID: '').obs;

  @override
  void onClose() {
    currentFriend.value = PrivateConversationModel(userID: '');
    TimService.to.currentConversationId.value = '';
    super.onClose();
  }

  /// 设置当前好友
  Future<void> setCurrentFriend(String userID) async {
    V2TimFriendInfoResult? info = await TimFriendService.to.friendInfo(userID);

    if (info != null) {
      TimService.to.currentConversationId.value = 'c2c_' + userID;

      currentFriend.value.userID = userID;
      currentFriend.value.isFriend =
          info.relation == UserRelationEnum.V2TIM_FRIEND_RELATION_TYPE_BOTH_WAY;

      currentFriend.value.friendRemark = info.friendInfo!.friendRemark ?? '';
      currentFriend.value.userProfile = info.friendInfo!.userProfile;

      /// 通过自定义Staffer字段，判断是否是客服，属于哪个店铺
      if (info.friendInfo!.userProfile?.customInfo!['Staffer']?.isNotEmpty ==
          true) {
        currentFriend.value.shopId =
            info.friendInfo!.userProfile?.customInfo!['Staffer'];
      }

      /// 通过用户自定义字段，判断是否允许陌生人消息
      if (info.friendInfo!.userProfile?.customInfo!['Stranger']?.isNotEmpty ==
          true) {
        currentFriend.value.allowStranger = false;
      }

      /// 设置会话
      currentFriend.value.conversation =
          await TimConversationService.to.getById('c2c_' + userID);

      currentFriend.refresh();
    }
  }

  Future<void> togglePinned() async {
    var res = await TimConversationService.to.setOnTop(
      currentFriend.value.conversation!,
    );

    if (res) {
      currentFriend.value.conversation =
          await TimConversationService.to.getById(
        'c2c_' + currentFriend.value.userID,
      );

      currentFriend.refresh();
      UiTools.toast('修改成功');
    }
  }

  Future<void> changeReceiveOpt() async {
    var res = await TimConversationService.to.setReceiveOpt(
      currentFriend.value.conversation!,
    );

    if (res) {
      currentFriend.value.conversation =
          await TimConversationService.to.getById(
        'c2c_' + currentFriend.value.userID,
      );
      currentFriend.refresh();

      UiTools.toast('修改成功');
    }
  }

  Future<void> setRemark(String remark) async {
    var result = await TimFriendService.to.setFriendRemark(
      currentFriend.value.userID,
      remark,
    );

    if (result) {
      currentFriend.value.friendRemark = remark;

      currentFriend.value.conversation =
          await TimConversationService.to.getById(
        'c2c_' + currentFriend.value.userID,
      );

      currentFriend.refresh();

      TimConversationService.to.fetchList();
      UiTools.toast('备注修改成功');
      Get.back();
    }
  }
}
