import 'package:injectable/injectable.dart';
import 'package:location/base/app_base_request.dart';
import 'package:location/data/api/atmob_api.dart';
import 'package:location/data/api/request/subscription_check_request.dart';
import 'package:location/data/repositories/account_repository.dart';
import 'package:location/utils/http_handler.dart';

import '../../di/get_it.dart';
import '../../utils/payment_status_manager.dart';
import '../api/request/member_list_request.dart';
import '../api/request/order_status_request.dart';
import '../api/request/submit_and_request_pay_request.dart';
import '../api/request/subscription_resume_request.dart';
import '../api/response/item_list_response.dart';
import '../api/response/member_trial_info_response.dart';
import '../api/response/order_first_check_response.dart';
import '../api/response/request_pay_response.dart';
import '../api/response/subscription_check_response.dart';

@lazySingleton
class MemberRepository {
  final AtmobApi atmobApi;
  final AccountRepository accountRepository;

  MemberRepository(this.atmobApi, this.accountRepository);

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

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

  Future<ItemListResponse> getMemberList({int? itemListType}) {
    return atmobApi
        .getMemberList(MemberListRequest(itemListType))
        .then(HttpHandler.handle(true));
  }

  Future<RequestPayResponse> submitAndRequestPay(
      {required int goodsId,
      required int payPlatform,
      required int payMethod}) {
    return atmobApi
        .submitAndRequestPay(SubmitAndRequestPayRequest(
            goodsId: goodsId, payPlatform: payPlatform, payMethod: payMethod))
        .then(HttpHandler.handle(false));
  }

  Future<int> orderStatus(String outTradeNo, {String? receiptData}) {
    return atmobApi
        .orderStatus(OrderStatusRequest(outTradeNo, receiptData))
        .then(HttpHandler.handle(false))
        .then((data) {
      if (data.payStatus == PaymentStatus.payStatusSuccess) {
        accountRepository.refreshMemberStatus();
      }
      return data.payStatus;
    });
  }

  //查询订阅状态
  Future<SubscriptionCheckResponse>subscriptionCheck(int payMethod,String receiptData) {
    return atmobApi.subscriptionCheck(SubscriptionCheckRequest(payMethod, receiptData))
    .then(HttpHandler.handle(false))
    .then((SubscriptionCheckResponse data){
      return  data;
    });
  }

  //恢复订阅
  Future<void>subscriptionResume(int payMethod,String receiptData) {
    return atmobApi.subscriptionresume(SubscriptionResumeRequest(payMethod, receiptData))
        .then(HttpHandler.handle(true));
  }

  //试用结束查看试用信息
  Future<MemberTrialInfoResponse>memberTrailInfo() {
    return atmobApi.memberTrailInfo(AppBaseRequest())
        .then(HttpHandler.handle(true))
        .then((MemberTrialInfoResponse trialInfoResponse) {
          return trialInfoResponse;
    });
  }

  //试用结束查看试用信息
  Future<OrderFirstCheckResponse>orderFirstCheck() {
    return atmobApi.orderFirstCheck(AppBaseRequest())
        .then(HttpHandler.handle(true))
        .then((OrderFirstCheckResponse firstCheckResponse) {
      return firstCheckResponse;
    });
  }

  ///好评领会员-中台
  Future<void>memberEvaluate() {
    return atmobApi.memberEvaluate(AppBaseRequest())
        .then(HttpHandler.handle(true)).then((_) {
          //刷新会员状态
      accountRepository.refreshMemberStatus();
    }).catchError((erro) {
    });
  }
}
