import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:injectable/injectable.dart';
import 'package:location/data/api/atmob_api.dart';
import 'package:location/data/consts/error_code.dart';
import 'package:location/utils/async_util.dart';
import 'package:location/utils/http_handler.dart';

import '../../base/app_base_request.dart';
import '../../di/get_it.dart';
import '../api/request/contact_request.dart';
import '../api/response/contact_list_response.dart';
import '../api/response/contact_may_day_all_response.dart';
import '../bean/contact_info.dart';

@lazySingleton
class UrgentContactRepository {
  final AtmobApi atmobApi;
  final RxList<ContactInfo> contactList = RxList<ContactInfo>();
  CancelableFuture? _cancelableFuture;

  UrgentContactRepository(this.atmobApi) {
    requestUrgentContactList();
  }

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

  void requestUrgentContactList() {
    _cancelableFuture?.cancel();
    _cancelableFuture = AsyncUtil.retryWithExponentialBackoff(
        () => getContactList(), 6, predicate: (error) {
      if (error is ServerErrorException &&
          error.code == ErrorCode.noLoginError) {
        return false;
      }
      return true;
    });
  }

  Future<ContactListResponse> getContactList() {
    return atmobApi
        .getContactList(AppBaseRequest())
        .then(HttpHandler.handle(true))
        .then((value) {
      contactList.clear();
      if (value.list != null) {
        contactList.addAll(value.list!);
      }
      return value;
    });
  }

  Future<void> addContact(String contactPhone) {
    return atmobApi
        .contactCreate(ContactRequest(phone: contactPhone))
        .then(HttpHandler.handle(true))
        .then((_) => requestUrgentContactList());
  }

  Future<void> contactFavor(String phone, bool favor) {
    return atmobApi
        .contactFavor(ContactRequest(phone: phone, favor: favor))
        .then(HttpHandler.handle(true))
        .then((_) => requestUrgentContactList());
  }

  Future<void> contactMayDay(String phone) {
    return atmobApi
        .contactMayDay(ContactRequest(phone: phone))
        .then(HttpHandler.handle(true));
  }

  Future<void> contactMayDayFavor() {
    return atmobApi
        .contactMayDayFavor(AppBaseRequest())
        .then(HttpHandler.handle(true));
  }

  Future<ContactMayDayAllResponse> contactMayDayAll() {
    return atmobApi
        .contactMayDayAll(AppBaseRequest())
        .then(HttpHandler.handle(true));
  }

  Future<void> contactDelete(String phone) {
    return atmobApi
        .contactDelete(ContactRequest(phone: phone))
        .then(HttpHandler.handle(true))
        .then((_) => requestUrgentContactList());
  }

  void clearContactList() {
    _cancelableFuture?.cancel();
    contactList.clear();
  }

  ContactInfo? getDefaultContact() {
    if (contactList.isEmpty) {
      return null;
    }
    for (var value in contactList) {
      if (value.favor == true) {
        return value;
      }
    }
    return null;
  }
}
