import 'package:injectable/injectable.dart';
import 'package:keyboard/data/api/atmob_api.dart';
import 'package:keyboard/data/api/response/item_list_response.dart';
import 'package:keyboard/data/api/response/order_pay_response.dart';
import 'package:keyboard/utils/http_handler.dart';

import '../../base/app_base_request.dart';
import '../../utils/payment_status_manager.dart';
import '../api/request/order_pay_request.dart';
import '../api/request/order_status_request.dart';
import '../api/response/item_retention_response.dart';
import 'account_repository.dart';

@lazySingleton
class StoreRepository {
  final tag = 'StoreRepository';

  final AtmobApi atmobApi;
  final AccountRepository accountRepository;

  StoreRepository(this.atmobApi, this.accountRepository);


  Future<ItemListResponse> getGoodsInfoList() async {
    return await atmobApi
        .getGoodsList(AppBaseRequest())
        .then(HttpHandler.handle(true));
  }


  Future<OrderPayResponse> submitAndRequestPay(
      {required int goodsId,
        required int payPlatform,
        required int payMethod}) {
    return atmobApi
        .orderPay(OrderPayRequest(
        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.refreshUserInfo();
          }
      return data.payStatus;
    });
  }

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


}
