import 'package:get/get.dart';
import 'package:loopin/IM/im_service.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation.dart';

class NotifyNoFriendController extends GetxController {
  final RxList<V2TimConversation> convList = <V2TimConversation>[].obs;

  void updateUnread({required String conversationID}) {
    final index = convList.indexWhere((c) => c.conversationID == conversationID);
    if (index != -1) {
      final conv = convList[index];
      convList[index] = conv..unreadCount = 0;
    } else {
      logger.e('会话不存在,更新未读失败');
    }
  }

  void updateLastMsg({required V2TimConversation conversation}) {
    final index = convList.indexWhere((c) => c.conversationID == conversation.conversationID);
    if (index != -1) {
      convList[index] = conversation;
      convList.refresh();
      // convList.value = List.from(convList)..[index] = conversation;
    } else {
      //  根据会话id查询会话检测是否存在
      logger.e('会话不存在，更新会话失败');
      // 如果存在，说明在后面的分页中，先insert进去 convlist做去重处理
    }
  }

  void insertData({required List<V2TimConversation> conversationList}) {
    convList.insertAll(0, conversationList);
  }
}
