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();
    } else {
      logger.e('当前会话数据未拉取到，执行插入逻辑');
      // 先insert进去 convlist做去重处理
      convList.insert(0, conversation);
      convList.refresh();
    }
  }

  void del({required V2TimConversation conversation}) {
    final index = convList.indexWhere((c) => c.conversationID == conversation.conversationID);
    if (index != -1) {
      convList.removeAt(index);
      convList.refresh();
    }
  }

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