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

import com.google.api.client.util.Maps;
import com.red.circle.framework.core.request.RequestClientEnum;
import com.red.circle.framework.web.props.EnvProperties;
import com.red.circle.order.app.dto.clientobject.pay.PlaceAnOrderResponseCO;
import com.red.circle.order.infra.database.mongo.order.InAppPurchaseDetailsService;
import com.red.circle.order.infra.database.mongo.order.entity.InAppPurchaseDetails;
import com.red.circle.order.infra.database.mongo.order.entity.assist.InAppPurchaseEventNotice;
import com.red.circle.order.infra.database.mongo.order.entity.assist.InAppPurchaseFactory;
import com.red.circle.order.infra.database.mongo.order.entity.assist.InAppPurchaseStatusStep;
import com.red.circle.order.inner.model.enums.inapp.InAppPurchaseStatus;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author pengliang on 2022/11/2
 */
@Service
@RequiredArgsConstructor
public class PayPlaceAnOrderService {

  private final EnvProperties envProperties;
  private final InAppPurchaseDetailsService inAppPurchaseDetailsService;
  private final FactoryPlaceAnOrderStrategyV2 factoryPlaceAnOrderStrategyV2;

  public PlaceAnOrderResponseCO doRequest(PayPlaceAnOrderDetailsV2 param) {
    // 第三方：请求下单
    PlaceAnOrderResponseCO response = factoryPlaceAnOrderStrategyV2.getStrategy(
            param.getStrategyCode())
        .doOperation(param);

    // 创建站内：预支付订单.
    inAppPurchaseDetailsService.create(
        new InAppPurchaseDetails()
            .setId(param.getOrderId())
            .setTrackId(param.getApplicationId())
            .setTrackCommodityType(param.getAppCommodityType())
            .setReceiptType(param.getReceiptType())
            .setSysOrigin(param.getSysOrigin())
            .setAcceptUserId(param.getAcceptUserId())
            .setOrderId(response.getTradeNo())
            .setEnv(env())
            .setFactory(
                new InAppPurchaseFactory()
                    .setPlatform(RequestClientEnum.H5.getClientName())
                    .setFactoryCode(param.getFactoryCode())
                    .setFactoryChannelCode(param.getChannelCode())
            )
            .setProducts(param.getProducts())
            .setProductDescriptor(param.getProductDescriptor())
            .setCountryCode(response.getCountryCode())
            .setCurrency(param.getCurrency())
            .setAmount(param.getAmount())
            .setAmountUsd(param.getAmountUsd())
            .setTrialPeriod(Boolean.FALSE)
            .setStatus(InAppPurchaseStatus.CREATE)
            .setReason("")
            .setPurchaseDateMs(TimestampUtils.now())
            .setCreateTime(TimestampUtils.now())
            .setUpdateTime(TimestampUtils.now())
            .setCreateUser(param.getCreateUserId())
            .setStatusSteps(List.of(
                new InAppPurchaseStatusStep()
                    .setStatus(InAppPurchaseStatus.CREATE)
                    .setCreateTime(TimestampUtils.now())
            ))
            .setPayNotices(List.of(
                new InAppPurchaseEventNotice()
                    .setEventId("custom-request-response-param")
                    .setNoticeType("API-INTERNAL-PARAMETERS")
                    .setNoticeData(response.extra())
                    .setCreateTime(TimestampUtils.now())
            ))
            .setMetadata(Maps.newHashMap())
            .setVersion(0)
    );

    return response;
  }

  private String env() {
    return envProperties.isProd() ? PayEnv.PROD.name() : PayEnv.TEST.name();
  }

}
