import 'package:get/get.dart';
import 'package:injectable/injectable.dart';
import 'package:location/data/api/atmob_api.dart';
import 'package:location/di/get_it.dart';
import 'package:location/utils/http_handler.dart';
import 'package:location/utils/mmkv_util.dart';

import '../api/request/message_request.dart';
import '../api/request/operation_friend_request.dart';
import '../api/request/request_friendlist_request.dart';
import '../api/response/message_response.dart';
import '../api/response/request_friend_list_response.dart';
import '../bean/request_friend_info.dart';

@lazySingleton
class MessageRepository {
  static final String _lastReadMessageId = 'last_read_message_id';

  final AtmobApi atmobApi;

  bool isRefreshing = false;
  Rxn<RequestFriendInfo> latestFriendRequest = Rxn<RequestFriendInfo>();
  RxInt waitingCount = RxInt(0);
  RxBool hasUnreadMessage = RxBool(false);
  int lastReadMessageId = 0;
  int lastMessageId = 0;

  MessageRepository(this.atmobApi) {
    lastReadMessageId = KVUtil.getInt(_lastReadMessageId, 0);
    refreshFriendWaitingCount();
    refreshUnreadMessage();
  }

  static MessageRepository getInstance() {
    return getIt.get<MessageRepository>();
  }

  void refreshFriendWaitingCount() {
    if (isRefreshing) {
      return;
    }
    isRefreshing = true;
    getRequestWaitingList().whenComplete(() {
      isRefreshing = false;
    });
  }

  Future<RequestFriendListResponse> getRequestWaitingList() {
    return atmobApi
        .getRequestWaitingList(RequestFriendListRequest())
        .then(HttpHandler.handle(true))
        .then((data) {
      if (data.list == null || data.list!.isEmpty) {
        latestFriendRequest.value = null;
      } else {
        latestFriendRequest.value = data.list!.first;
      }
      waitingCount.value = data.count;
      _updateHasUnreadMessage();
      return data;
    });
  }

  Future<RequestFriendListResponse> getRequestFriendList() {
    return atmobApi
        .getRequestFriendList(RequestFriendListRequest())
        .then(HttpHandler.handle(true));
  }

  void clearMessage() {
    latestFriendRequest.value = null;
    waitingCount.value = 0;
    hasUnreadMessage.value = false;
  }

  Future<MessageResponse> getMessageList() {
    return atmobApi
        .getMessageList(MessageRequest())
        .then(HttpHandler.handle(true))
        .then((response) {
      if (response.list != null && response.list!.isNotEmpty) {
        lastMessageId = response.list!.first.id;
      }
      _updateHasUnreadMessage();
      return response;
    });
  }

  Future<void> acceptFriendAccept(int friendId) {
    return atmobApi
        .acceptFriendAccept(OperationFriendRequest(friendId))
        .then(HttpHandler.handle(true))
        .then((_) => refreshFriendWaitingCount());
  }

  Future<void> refuseFriendAccept(int friendId) {
    return atmobApi
        .refuseFriendAccept(OperationFriendRequest(friendId))
        .then(HttpHandler.handle(true))
        .then((_) => refreshFriendWaitingCount());
  }

  void updateLastReadMessageId(int messageId) {
    if (messageId == lastReadMessageId || messageId == 0) {
      return;
    }
    lastReadMessageId = messageId;
    KVUtil.putInt(_lastReadMessageId, messageId);

    _updateHasUnreadMessage();
  }

  void refreshUnreadMessage() {
    getMessageList();
  }

  void _updateHasUnreadMessage() {
    if (waitingCount.value > 0 ||
        (lastMessageId != 0 &&
            lastReadMessageId != 0 &&
            lastReadMessageId != lastMessageId)) {
      hasUnreadMessage.value = true;
    } else {
      hasUnreadMessage.value = false;
    }
  }
}
