package com.red.circle.order.app.command.pay.app.receipt;

import com.google.api.services.androidpublisher.model.ProductPurchase;
import com.red.circle.component.pay.apple.LatestEffectiveBody;
import com.red.circle.component.pay.apple.LatestReceiptInfo;
import com.red.circle.component.pay.google.GooglePayProductParam;
import com.red.circle.component.pay.google.GoogleVerifyService;
import com.red.circle.component.pay.huawei.model.HuaweiInAppPurchaseDetails;
import com.red.circle.framework.core.request.RequestClientEnum;
import com.red.circle.order.app.command.pay.app.model.ExtractInAppPurchaseReceipt;
import com.red.circle.order.app.dto.cmd.ApplePurchaseCmd;
import com.red.circle.order.app.dto.cmd.GooglePurchaseCmd;
import com.red.circle.order.app.dto.cmd.HuaweiPurchaseCmd;
import com.red.circle.order.app.dto.cmd.TelegramPurchaseCmd;
import com.red.circle.order.infra.database.enums.OrderEnvironmentEnum;
import com.red.circle.order.infra.database.rds.entity.order.OrderTelegramPurchase;
import com.red.circle.order.inner.model.enums.inapp.PayPlatform;
import com.red.circle.tool.core.date.TimestampUtils;
import java.sql.Timestamp;
import java.util.Optional;

/**
 * @author pengliang
 * @since 2020/4/5 13:42
 */
public interface AssemblyOrder {

  /**
   * 华为订单.
   */
  static ExtractInAppPurchaseReceipt toHuaweiExtractInAppPurchaseReceipt(HuaweiPurchaseCmd cmd,
      HuaweiInAppPurchaseDetails details) {
    return new ExtractInAppPurchaseReceipt()
        .setEvn(OrderEnvironmentEnum.huaweiEvn(details.getPurchaseType()))
        .setUserId(cmd.requiredReqUserId())
        .setAcceptUserId(cmd.getAcceptUserId())
        .setSysOrigin(cmd.requireReqSysOriginChild())
        .setOriginalOrderId(details.getOrderId())
        .setOrderId(details.getPayOrderId())
        .setProductId(details.getProductId())
        .setPlatform(RequestClientEnum.Android.name())
        .setPayPlatform(PayPlatform.HUAWEI.name())
        .setPurchaseDateMs(new Timestamp(details.getPurchaseTime()))
        .setExpiresDateMs(TimestampUtils.now())
        .setTrialPeriod(false);
  }

  /**
   * 获取apple购买/订阅订单.
   */
  static ExtractInAppPurchaseReceipt toAppleExtractInAppPurchaseReceipt(ApplePurchaseCmd cmd,
      LatestEffectiveBody latestEffectiveBody) {
    LatestReceiptInfo latestReceiptInfo = latestEffectiveBody.getLatestReceiptInfo();
    return new ExtractInAppPurchaseReceipt()
        .setEvn(OrderEnvironmentEnum.appleEvn(latestEffectiveBody.getEnvironment()))
        .setUserId(cmd.requiredReqUserId())
        .setAcceptUserId(cmd.getAcceptUserId())
        .setSysOrigin(cmd.requireReqSysOriginChild())
        .setOriginalOrderId(latestReceiptInfo.getOriginalTransactionId())
        .setOrderId(latestReceiptInfo.getTransactionId())
        .setProductId(latestReceiptInfo.getProductId())
        .setPlatform(RequestClientEnum.iOS.name())
        .setPayPlatform(PayPlatform.APPLE.name())
        .setPurchaseDateMs(Optional.ofNullable(latestReceiptInfo.purchaseDateMsDate())
            .map(TimestampUtils::convert).orElse(null)
        )
        .setExpiresDateMs(Optional.ofNullable(latestReceiptInfo.expiresDateMsDate())
            .map(TimestampUtils::convert).orElse(null)
        )
        .setTrialPeriod(latestReceiptInfo.getIsTrialPeriod());
  }


  /**
   * 获取google购买订单.
   */
  static ExtractInAppPurchaseReceipt toGoogleExtractInAppPurchaseReceipt(
      GooglePurchaseCmd cmd,
      GooglePayProductParam signParam,
      ProductPurchase productPurchase) {
    return new ExtractInAppPurchaseReceipt()
        .setEvn(OrderEnvironmentEnum.googleEvn(productPurchase.getPurchaseType()))
        .setUserId(cmd.requiredReqUserId())
        .setAcceptUserId(cmd.getAcceptUserId())
        .setSysOrigin(cmd.requireReqSysOriginChild())
        .setOriginalOrderId(GoogleVerifyService.toOriginalOrderId(productPurchase.getOrderId()))
        .setOrderId(productPurchase.getOrderId())
        .setProductId(signParam.getProductId())
        .setPlatform(RequestClientEnum.Android.name())
        .setPayPlatform(PayPlatform.GOOGLE.name())
        .setPurchaseDateMs(new Timestamp(productPurchase.getPurchaseTimeMillis()));
  }

  /**
   * 获取google购买订单.
   */
  static ExtractInAppPurchaseReceipt toTelegramExtractInAppPurchaseReceipt(
          TelegramPurchaseCmd cmd, OrderTelegramPurchase orderTelegramPurchase) {
    return new ExtractInAppPurchaseReceipt()
            .setEvn(OrderEnvironmentEnum.PROD.name())
            .setUserId(cmd.requiredReqUserId())
            .setAcceptUserId(cmd.getAcceptUserId())
            .setSysOrigin(cmd.requireReqSysOriginChild())
            .setOriginalOrderId(orderTelegramPurchase.getTelegramPaymentChargeId())
            .setOrderId(orderTelegramPurchase.getTelegramPaymentChargeId())
            .setProductId(String.valueOf(orderTelegramPurchase.getProductId()))
            .setPlatform(RequestClientEnum.H5.name())
            .setPayPlatform(PayPlatform.TELEGRAM.name())
            .setPurchaseDateMs(TimestampUtils.now());
  }

}
