import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:injectable/injectable.dart';
import 'package:location/base/base_controller.dart';
import 'package:location/data/repositories/account_repository.dart';
import 'package:location/data/repositories/urgent_contact_repository.dart';
import 'package:location/handler/error_handler.dart';
import 'package:location/module/member/member_page.dart';
import 'package:location/module/urgent_contact/urgent_contact_click_help_dialog.dart';
import 'package:location/popup/urgent_contact_more_action_popup.dart';
import 'package:location/resource/string.gen.dart';
import 'package:location/utils/toast_util.dart';

import '../../data/bean/contact_info.dart';
import '../../data/bean/member_status_info.dart';
import '../../dialog/common_alert_dialog_impl.dart';
import '../../utils/mmkv_util.dart';
import 'add_contact/add_urgent_contact_view.dart';

///求助提示框记录
const String _kChickHelpAlertKey = "urgent_chick_help_alder_key";

@injectable
class UrgentContactController extends BaseController {
  final UrgentContactRepository _urgentContactRepository;
  final AccountRepository accountRepository;

  Rxn<MemberStatusInfo> get memberStatusInfo =>
      accountRepository.memberStatusInfo;

  RxList<ContactInfo> get contactList => _urgentContactRepository.contactList;

  UrgentContactController(
      this._urgentContactRepository, this.accountRepository);

  @override
  void onReady() {
    super.onReady();
    _onShowRequestHelpTip();
  }

  void back() {
    Get.back();
  }

  void addContactClick() async {
    AddUrgentContactView.show();
  }

  void moreClick(BuildContext context, ContactInfo e) {
    UrgentContactMoreActionPopup.show(context, e.favor == true,
        onSetDefault: () {
      _setDefaultContact(e);
    }, onDelete: () {
      showDeleteUrgentContactDialog(e.phone,
          confirmOnTap: () => _deleteContact(e));
    });
  }

  //弹出求助提示
  void _onShowRequestHelpTip() {
    if (Get.parameters != null) {
      String isShowRequestTip = Get.parameters["isShowRequest"] ?? "0";
      if (isShowRequestTip == "1") {
        String? memberPageKeyStr = KVUtil.getString(_kChickHelpAlertKey, '');
        if ((memberStatusInfo.value?.expired == true || memberStatusInfo.value?.expired == null) &&
            (memberPageKeyStr ?? '').length == 0) {
          ///永久化存储
          KVUtil.putString(_kChickHelpAlertKey, _kChickHelpAlertKey);
          UrgentContactClickHelpDialog.show(confirmOnTap: () {
            MemberPage.start();
          });
        }
      }
    }
  }

  void _setDefaultContact(ContactInfo contactInfo) {
    _urgentContactRepository
        .contactFavor(
            contactInfo.phone, contactInfo.favor == true ? false : true)
        .then((value) {
      if (contactInfo.favor == true) {
        ToastUtil.show(StringName.urgentContactCancelSuccess);
      } else {
        ToastUtil.show(StringName.urgentContactSetSuccess);
      }
    }).catchError((e) {
      ErrorHandler.toastError(e);
    });
  }

  void _deleteContact(ContactInfo e) {
    _urgentContactRepository.contactDelete(e.phone).then((value) {
      ToastUtil.show(StringName.urgentContactDeleteSuccess);
    }).catchError((e) {
      ErrorHandler.toastError(e);
    });
  }

  sendHelpClick(String phone) {
    if (accountRepository.memberIsExpired()) {
      MemberPage.start();
      return;
    }
    sendUrgentContactDialog(phone, confirmOnTap: () {
      _urgentContactRepository.contactMayDay(phone).then((value) {
        ToastUtil.show(StringName.urgentContactHelpSendSuccess);
      }).catchError((e) {
        ErrorHandler.toastError(e);
      });
    });
  }

  void sendAllHelpClick() {
    if (accountRepository.memberIsExpired()) {
      MemberPage.start();
      return;
    }
    sendAllUrgentContactDialog(confirmOnTap: () {
      _urgentContactRepository.contactMayDayAll().then((response) {
        if (response.fail == null || response.fail!.isEmpty) {
          ToastUtil.show(StringName.urgentContactHelpSendSuccess);
        } else {
          sendUrgentContactPartErrorDialog(response.fail!, confirmOnTap: () {});
        }
      }).catchError((e) {
        ErrorHandler.toastError(e);
      });
    });
  }
}
