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/repositories/account_repository.dart';
import 'package:location/utils/http_handler.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/response/item_list_response.dart';
import '../api/response/request_pay_response.dart';

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

  MemberRepository(this.atmobApi, this.accountRepository);

  Future<int> memberTrial() {
    return atmobApi
        .memberTrial(AppBaseRequest())
        .then(HttpHandler.handle(false))
        .then((response) => response.freeMemberMinutes);
  }

  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;
    });
  }
}
