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

import com.red.circle.order.infra.database.mongo.order.entity.assist.InAppPurchaseProduct;
import com.red.circle.order.infra.database.rds.entity.pay.PayCountry;
import com.red.circle.order.infra.database.rds.entity.pay.PayCountryChannelDetails;
import com.red.circle.order.inner.model.enums.inapp.InAppPurchaseReceiptType;
import com.red.circle.other.inner.model.dto.sys.SysCountryCodeDTO;
import com.red.circle.tool.core.collection.MapBuilder;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author pengliang on 2021/12/9
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class PayPlaceAnOrderDetailsV2 implements Serializable {

  @Serial
  private static final long serialVersionUID = 1L;

  /**
   * 订单id.
   */
  private String orderId;

  /**
   * 单据类型.
   */
  private InAppPurchaseReceiptType receiptType;

  /**
   * 应用id.
   */
  private String applicationId;

  /**
   * 来源系统
   */
  private String sysOrigin;

  /**
   * 商品类型.
   */
  private String appCommodityType;

  /**
   * 接收用户.
   */
  private Long acceptUserId;

  /**
   * 创建用户.
   */
  private Long createUserId;

  /**
   * 金额.
   */
  private BigDecimal amount;

  /**
   * 金额,美元.
   */
  private BigDecimal amountUsd;

  /**
   * 购买产品.
   */
  private List<InAppPurchaseProduct> products;

  /**
   * 商品描述.
   */
  private String productDescriptor;

  /**
   * 支付渠道详情.
   */
  private PayCountryChannelDetails channelDetails;

  /**
   * 开通国家信息.
   */
  private PayCountry payCountry;

  /**
   * 国家区域信息.
   */
  private SysCountryCodeDTO country;

  /**
   * 充值页面.
   */
  private String rechargeUrl;

  /**
   * 成功回掉页面.
   */
  private String successRedirectUrl;

  /**
   * 取消回掉页面.
   */
  private String cancelRedirectUrl;

  /**
   * 版本信息.
   */
  private Boolean newVersion;

  /**
   * 请求ip信息.
   */
  private String requestIp;

  public String getFactoryChannel() {
    return channelDetails.getFactoryChannel();
  }

  public String getChannelCode() {
    return channelDetails.getChannelCode();
  }

  public String getFactoryCode() {
    return channelDetails.getFactoryCode();
  }

  public String getStrategyCode() {
    return this.getFactoryCode().concat("_PLACE_AN_ORDER");
  }

  public String getCountryCode() {
    return country.getAlphaTwo();
  }

  public String getCurrency() {
    return payCountry.getCurrency();
  }

  public Map<String, String> createMetadata() {
    return MapBuilder.<String, String>builder()
        .put("userId", Objects.toString(this.getAcceptUserId()))
        .put("sysOrigin", this.getSysOrigin())
        .put("orderId", this.getOrderId())
        .put("trackId", Objects.toString(this.getApplicationId()))
        .put("channelDetailId", Objects.toString(this.getChannelDetails().getId()))
        .put("payCountryId", Objects.toString(this.getCountry().getId()))
        .put("countryId", Objects.toString(this.getCountry().getId()))
        .put("countryCode", this.getCountryCode())
        .put("amount", this.getAmount().toString())
        .put("currency", this.getCurrency())
        .build();
  }

}
