import 'dart:io';

import 'package:fluwx/fluwx.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/order_info.dart';
import 'package:huixiang/retrofit/data/placeOrderFirst.dart';
import 'package:huixiang/retrofit/data/wx_pay.dart';
import 'package:huixiang/retrofit/min_api.dart';
import 'package:huixiang/utils/min.dart';

class OrderUtils {

  static carryOnPay(payChannel, MinApiService minService, OrderInfo orderInfo, Function queryDetails) async {
    PlaceOrderFirst placeOrderFirst = PlaceOrderFirst();
    placeOrderFirst.addressId = orderInfo.addressExt == null ? "" : orderInfo.addressExt.addressId;
    placeOrderFirst.isSubscribe = orderInfo.isSubscribe;
    placeOrderFirst.isTakeOut = orderInfo.isTakeOut;
    placeOrderFirst.notes = orderInfo.notes;
    placeOrderFirst.id = orderInfo.id;
    placeOrderFirst.orderProductVOList = orderInfo.orderDetail?.orderProductList;
    placeOrderFirst.numberOfPeople = "0";
    placeOrderFirst.orderSource = orderInfo.orderSource;
    placeOrderFirst.orderType = "${orderInfo.orderType}";
    placeOrderFirst.orderTypeId = 0;
    placeOrderFirst.parentCode = orderInfo.parentCode; // 火锅加菜
    placeOrderFirst.parentId = orderInfo.parentId; // 火锅加菜
    placeOrderFirst.payChannel = payChannel;
    placeOrderFirst.promotionInfoDTO = PromotionInfoDTOBean();
    placeOrderFirst.promotionInfoDTO.promotionId = orderInfo.promotionId;
    placeOrderFirst.promotionInfoDTO.couponId = orderInfo.couponId;
    placeOrderFirst.recMobile = "";
    placeOrderFirst.shoppingCartSkuItemList = [];
    placeOrderFirst.skuItemDTOList = [];
    placeOrderFirst.source = 1;
    placeOrderFirst.storeId = orderInfo.storeId;
    placeOrderFirst.subcribeTime = null;
    placeOrderFirst.tableId = orderInfo.tableId;

    if (placeOrderFirst.payChannel == 1) {
      if(Platform.isAndroid){
        if (!(await Min.isInitialize())) {
          // 小程序的微信支付和app的充值支付使用同一个WXPayEntryActivity回调,
          // 然而充值时小程序未初始化会导致回调内部代码调用getPackage空指针,
          // 故而在此初始化一下
          await Min.initialize();
        }
      }
      BaseData<WxPay> baseData = await minService
          .settlementWx(placeOrderFirst.toJson())
          .catchError((error) {});
      if (baseData != null && baseData.isSuccess) {
        WxPay wxPay = baseData.data;
        await registerWxApi(
          appId: wxPay.appId,
          doOnAndroid: true,
          universalLink: "https://hx.lotus-wallet.com/app/",
        );
        payWithWeChat(
          appId: wxPay.appId,
          partnerId: wxPay.partnerId,
          prepayId: wxPay.prepayId,
          packageValue: wxPay.packageValue,
          nonceStr: wxPay.nonceStr,
          timeStamp: int.tryParse(wxPay.timeStamp),
          sign: wxPay.sign,
        );
        weChatResponseEventHandler.listen((event) async {
          print("payCallback: ${event.errCode}");
          queryDetails(null);
        });
      }
    } else {
      BaseData baseData = await minService
          .settlementApi(placeOrderFirst.toJson())
          .catchError((error) {});
      queryDetails(baseData);
    }
  }



}