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

import com.red.circle.component.pay.PayProperties;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.order.app.command.pay.web.strategy.PayPlaceAnOrderDetailsV2;
import com.red.circle.order.app.command.pay.web.strategy.PayPlaceAnOrderService;
import com.red.circle.order.app.convertor.PayAppConvertor;
import com.red.circle.order.app.dto.clientobject.pay.PlaceAnOrderResponseCO;
import com.red.circle.order.app.dto.cmd.PayPlaceAnOrderReceiptCmd;
import com.red.circle.order.infra.database.mongo.order.InAppPurchaseCollectionReceiptService;
import com.red.circle.order.infra.database.mongo.order.entity.InAppPurchaseCollectionReceipt;
import com.red.circle.order.infra.database.mongo.order.entity.assist.InAppPurchaseCountryAssociatedChannels;
import com.red.circle.order.infra.database.mongo.order.entity.assist.InAppPurchasePayCountryChannelDetails;
import com.red.circle.order.infra.database.rds.entity.pay.PayCountry;
import com.red.circle.order.infra.database.rds.service.pay.PayCountryService;
import com.red.circle.order.inner.asserts.PayErrorCode;
import com.red.circle.order.inner.model.enums.inapp.InAppPurchaseReceiptType;
import com.red.circle.other.inner.endpoint.sys.SysCountryCodeClient;
import com.red.circle.other.inner.model.dto.sys.SysCountryCodeDTO;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * web 收单.
 *
 * @author pengliang on 2022/11/2
 */
@Deprecated
@Component
@RequiredArgsConstructor
public class ReceiptPayWebPlaceAnOrderReceiptCmdExe {

  private final PayProperties payProperties;
  private final PayAppConvertor payAppConvertor;
  private final PayCountryService payCountryService;
  private final SysCountryCodeClient sysCountryCodeClient;
  private final PayPlaceAnOrderService payPlaceAnOrderService;
  private final InAppPurchaseCollectionReceiptService inAppPurchaseCollectionReceiptService;

  public PlaceAnOrderResponseCO execute(PayPlaceAnOrderReceiptCmd cmd) {
    // 获取单据信息.
    InAppPurchaseCollectionReceipt receipt = inAppPurchaseCollectionReceiptService.getById(
        cmd.getReceiptId());
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, receipt);

    InAppPurchaseCountryAssociatedChannels channel = receipt.getCustomChannels().stream()
        .filter(item ->
            Objects.equals(item.getCountryChannel().getPayFactory().getId(), cmd.getFactoryId())
                && Objects.equals(item.getChannel().getId(), cmd.getChannelId())).findFirst()
        .orElse(null);
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, channel);
    InAppPurchasePayCountryChannelDetails cDetails = channel.getCountryChannel()
        .getCountryChannelDetails();
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, cDetails);
    PlaceAnOrderResponseCO response = payPlaceAnOrderService.doRequest(
        getPayPlaceAnOrderDetails(receipt, cDetails));

    // 收据上锁5分钟
    inAppPurchaseCollectionReceiptService.lockTime(receipt.getId(), 5);
    return response;
  }


  private PayPlaceAnOrderDetailsV2 getPayPlaceAnOrderDetails(
      InAppPurchaseCollectionReceipt receipt, InAppPurchasePayCountryChannelDetails cDetails) {
    PayCountry payCountry = payCountryService.getById(cDetails.getPayCountryId());
    // 不支持地区
    ResponseAssert.notNull(PayErrorCode.REGION_NOT_SUPPORTED, payCountry);
    SysCountryCodeDTO sysCountryCode = ResponseAssert.requiredSuccess(
        sysCountryCodeClient.getById(payCountry.getCountryId())
    );
    // 不支持地区
    ResponseAssert.notNull(PayErrorCode.REGION_NOT_SUPPORTED, sysCountryCode);
    PayPlaceAnOrderDetailsV2 details = new PayPlaceAnOrderDetailsV2();
    details.setOrderId(IdWorkerUtils.getIdStr());
    details.setReceiptType(InAppPurchaseReceiptType.RECEIPT);
    details.setApplicationId(receipt.getId());
    details.setSysOrigin(receipt.getSysOrigin());
    details.setAcceptUserId(receipt.getAcceptUserId());
    details.setCreateUserId(receipt.getCreateUser());
    details.setAmount(receipt.getAmount());
    details.setAmountUsd(receipt.getAmountUsd());
    details.setProducts(receipt.getProducts());
    details.setProductDescriptor(receipt.getDescribe());
    details.setChannelDetails(payAppConvertor.toPayCountryChannelDetails(cDetails));
    details.setCountry(sysCountryCode);
    details.setPayCountry(payCountry);
    details.setRechargeUrl(payProperties.getWebPayCollectionPageUrl()
        + "/" + receipt.getId());
    details.setSuccessRedirectUrl(payProperties.getWebPayResultPageUrl()
        + "?redirect=" + payProperties.getWebPayCollectionPage()
        + "&appId=" + receipt.getId()
        + "&appStatus=0"
        + "&appType=COLLECTION");
    details.setCancelRedirectUrl(details.getRechargeUrl());
    return details;
  }

}
